From f3e08fcbcf8d052ac57188b3d31ab7bb38f322d5 Mon Sep 17 00:00:00 2001 From: Eric Anderson Date: Fri, 13 Aug 2010 16:57:56 -0500 Subject: Begin using config file and begin to organize like a real application --- feed-transmission.example.ini | 16 ++++++ feed-transmission.py | 121 +++++++++++++++++++++++++++++++++--------- 2 files changed, 112 insertions(+), 25 deletions(-) create mode 100644 feed-transmission.example.ini 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() -- cgit v1.2.3-54-g00ecf