summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Anderson <ejona86@gmail.com>2010-08-13 16:57:56 -0500
committerEric Anderson <ejona86@gmail.com>2010-08-13 16:57:56 -0500
commitf3e08fcbcf8d052ac57188b3d31ab7bb38f322d5 (patch)
tree709ca84a905b8ce35cbb9b08a8e6bce778d4dd7e
parent775247e1ea09a18d47046f847e0aee4c733a7182 (diff)
downloadfeed-transmission-f3e08fcbcf8d052ac57188b3d31ab7bb38f322d5.tar.gz
feed-transmission-f3e08fcbcf8d052ac57188b3d31ab7bb38f322d5.zip
Begin using config file and begin to organize like a real application
-rw-r--r--feed-transmission.example.ini16
-rwxr-xr-xfeed-transmission.py121
2 files changed, 112 insertions, 25 deletions
diff --git a/feed-transmission.example.ini b/feed-transmission.example.ini
new file mode 100644
index 0000000..857f177
--- /dev/null
+++ b/feed-transmission.example.ini
@@ -0,0 +1,16 @@
+; 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.py b/feed-transmission.py
index 07fb620..ff30502 100755
--- a/feed-transmission.py
+++ b/feed-transmission.py
@@ -4,38 +4,109 @@ import feedparser
import re
import time
import subprocess
+import urllib2
+import tempfile
+import shelve
+import ConfigParser
POLL_DELAY = 60 * 60
threads = [
- "Some thread",
+ ".*Thread: Some thread",
]
-downloadables = re.compile(r".*Thread: (?:%s)" % "|".join(threads))
-
-def find_relavant():
- d = feedparser.parse('https://example.com/rss.php')
- found = []
- for i in d['items']:
- if not downloadables.match(i.title):
- continue
- found.append(i.link)
- return found
-
-def main():
- downloaded = []
- while True:
- downloaded = downloaded[-100:]
- found = find_relavant()
- for i in found:
- if i in downloaded:
+downloadables = re.compile("|".join(threads))
+
+class App(object):
+ def __init__(self):
+ self.seen = []
+ self.downloaded = []
+
+class Feed(object):
+ def __init__(self, url, poll_delay, match):
+ self.url = url
+ self.poll_delay = poll_delay
+ self.match = match
+
+ def find_relavant(self, seen):
+ f = urllib2.urlopen('https://example.com/rss.php')
+ d = feedparser.parse(f.read())
+ f.close()
+ found = []
+ print "New RSS Items:"
+ for i in d['items']:
+ if i.title in seen:
+ continue
+ seen.append(i.title)
+ print " ", i.title
+ if not downloadables.match(i.title):
continue
- downloaded.append(i)
- print i
- ret = subprocess.call("transmissioncli", "localhost", "-a", i)
- if ret:
- print "Error adding torrent"
- time.sleep(POLL_DELAY)
+ print " Matched"
+ found.append(i.link)
+ return found
+
+def load_config():
+ config = ConfigParser.RawConfigParser({})
+ config.readfp(file('.feed-transmission.ini'))
+ d = {"feeds": []}
+ feed_names = config.get("config", "feeds")
+ feed_names.split(",")
+ for i in feed_names:
+ i = i.trim()
+ f = {}
+ f["url"] = config.get(i, "url")
+ f["poll_delay"] = config.getint(i, "poll_delay")
+ match = []
+ for m in config.items(i):
+ if not m[0].startswith("match")
+ continue
+ try:
+ re.compile(m[1])
+ except re.error:
+ print "Invalid regular expression at %s, %s: %s" % (i, m[0], m[1])
+ match.append(m[1])
+ match = "|".join(match)
+ f["match"] = re.compile(match)
+ d["feeds"].append(f)
+ return d
+
+def main():
+ opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
+ urllib2.install_opener(opener)
+
+ config = load_config()
+ stor = shelve.open(".feed-transmission.data")
+
+ if not "downloaded" in stor:
+ stor["downloaded"] = []
+ if not "seen" in stor:
+ stor["seen"] = []
+ downloaded = stor["downloaded"]
+ seen = stor["seen"]
+ try:
+ while True:
+ found = find_relavant(seen)
+ seen = seen[-500:]
+ stor["seen"] = seen
+ for i in found:
+ if i in downloaded:
+ continue
+ downloaded.append(i)
+ f = urllib2.urlopen(i)
+ temp = tempfile.NamedTemporaryFile()
+ temp.write(f.read())
+ f.close()
+ temp.flush()
+ ret = subprocess.call(["transmission-remote", "localhost", "-a", temp.name])
+ temp.close()
+ if ret:
+ print "Error adding torrent"
+ downloaded = downloaded[-500:]
+ stor["downloaded"] = downloaded
+ time.sleep(POLL_DELAY)
+ except KeyboardInterrupt:
+ pass
+ stor.close()
if __name__ == "__main__":
main()