From d3d34b73a427973c450d705e9a6acf496c5e9384 Mon Sep 17 00:00:00 2001
From: Eric Anderson <ejona86@gmail.com>
Date: Sat, 14 Aug 2010 12:28:32 -0500
Subject: Use JSON configuration format instead of INI based

---
 feed-transmission.example.ini  | 16 ----------------
 feed-transmission.example.json | 14 ++++++++++++++
 feed-transmission.py           | 37 +++++++++++++++----------------------
 3 files changed, 29 insertions(+), 38 deletions(-)
 delete mode 100644 feed-transmission.example.ini
 create mode 100644 feed-transmission.example.json

diff --git a/feed-transmission.example.ini b/feed-transmission.example.ini
deleted file mode 100644
index 857f177..0000000
--- a/feed-transmission.example.ini
+++ /dev/null
@@ -1,16 +0,0 @@
-; main section
-[config]
-; comma-delimited list of feeds. Name is used to look up section.
-feeds: example.com
-
-; name is not used except by 'feeds' key in 'config' section
-[example.com]
-; RSS/Atom feed url to fetch
-url: https://example.com/rss.php
-; number of seconds to delay between fetches of feed
-poll_delay: 3600
-; 'match'-prefixed items are OR'ed together to form a whitelist of items to
-; download. The value should be a regular expression
-match1: .*Thread: Some thread
-match2: .*Thread: Some other thread
-
diff --git a/feed-transmission.example.json b/feed-transmission.example.json
new file mode 100644
index 0000000..d934246
--- /dev/null
+++ b/feed-transmission.example.json
@@ -0,0 +1,14 @@
+{
+    "feeds": [
+        {
+            "matches": [
+                ".*Thread: Some thread",
+                ".*Thread: Some other thread"
+            ],
+            "name": "example.com",
+            "poll_delay": 3600,
+            "url": "https://example.com/rss.php"
+        }
+    ]
+}
+
diff --git a/feed-transmission.py b/feed-transmission.py
index dbb9a8f..521fecf 100755
--- a/feed-transmission.py
+++ b/feed-transmission.py
@@ -7,7 +7,7 @@ import subprocess
 import urllib2
 import tempfile
 import shelve
-import ConfigParser
+import json
 
 class App(object):
     def __init__(self):
@@ -16,14 +16,10 @@ class App(object):
         self.feeds = []
 
     def load_config(self, ini):
-        config = ConfigParser.RawConfigParser({})
-        config.readfp(file(ini))
-        feed_names = config.get("config", "feeds")
-        feed_names = feed_names.split(",")
-        for i in feed_names:
-            i = i.strip()
+        config = json.load(file(ini))
+        for i in config["feeds"]:
             feed = Feed()
-            feed.load_config(config, i)
+            feed.load_config(i)
             self.add_feed(feed)
 
     def load_stor(self, data):
@@ -129,28 +125,25 @@ class Feed(object):
             found.append(i.link)
         return found
 
-    def load_config(self, config, section):
-        url = config.get(section, "url")
-        poll_delay = config.getint(section, "poll_delay")
-        match = []
-        for m in config.items(section):
-            if not m[0].startswith("match"):
-                continue
+    def load_config(self, config):
+        url = str(config["url"])
+        poll_delay = int(config["poll_delay"])
+        matches = list(config["matches"])
+        for i, m in enumerate(matches):
             try:
-                re.compile(m[1])
+                re.compile(m)
             except re.error:
-                print "Invalid regular expression at %s, %s: %s" % (section, m[0], m[1])
-            match.append(m[1])
-        match = "|".join(match)
-        match = re.compile(match)
+                print "Invalid regular expression in matches list with index %d: %s" % (i, m)
+        matches = "|".join(matches)
+        matches = re.compile(matches)
         self.url = url
         if poll_delay:
             self.poll_delay = poll_delay
-        self.match = match
+        self.match = matches
 
 if __name__ == "__main__":
     app = App()
-    app.load_config('.feed-transmission.ini')
+    app.load_config('.feed-transmission.json')
     app.load_stor(".feed-transmission.data")
     app.setup_env()
     app.main()
-- 
cgit v1.2.3-70-g09d2