Alright, after some work I've come up with a more generic version. I also cleaned up the code some. Besides adding the feed to the file with all your others, the only thing you should have to do is provide an action for new types of feeds (i.e. youtube, tvrss, etc...). I added a basic one for youtube videos using youtube-dl. There's still some more modifications I want to make, but this is a pretty good start.
Code:
#!/usr/bin/env python
"""rssdl.py - Performs acctions on new content linked to from rss feeds"""
import feedparser, os, time
# Retrieves value from dictionary with partial match key
def getValue(key, dict):
for k in dict.keys():
if key.startswith(k):
return actions[k]
return None
# Actions are dependent on the type of feed
actions = { \
'tvRSS': 'wget --quiet --tries=1 --read-timeout=90 %(link)s', \
'YouTube': 'youtube-dl --output="%(title)s.flv" %(link)s', \
}
path = '%s/downloads' % os.getenv('HOME')
name = '%s/feeds' % path
# Load every line of the file to a list element
file = open(name)
lines = [line for line in file.readlines() if line.strip()]
file.close()
# Last line of the file should be the previous time we
# checked the feeds, if not make it the current time.
if lines[-1][0] == '(':
# Force it to the proper format
oldtime = lines[-1].strip()[1:-1]
oldtime = (oldtime.split(', '))
oldtime = tuple([int(val) for val in oldtime])
lines.pop()
else:
oldtime = time.gmtime()
# Generate a list of rss urls by removing comments
urls = [line.strip() for line in lines if line[0] != '#']
# Get the current time (GMT because feedparser uses it) to
# replace old time value at bottom of file
newtime = time.gmtime()
#TODO: This could really be sped up if each feed was handled
# in it's own thread from here on
# Grab all the rss feeds
feeds = [feedparser.parse(url.strip()) for url in urls]
# Make sure torrents get dowloaded to proper directory
os.chdir(path)
# Check all your feeds for new torrents
for feed in feeds:
# Ignore feeds we don't know what to do with
action = getValue(feed['feed']['title'], actions)
if action is None:
continue
for entry in feed['entries']:
# Only download new torrents
if oldtime < entry['updated_parsed']:
# Check that action was taken
if os.system(action % entry) != 0:
# TODO: Error handling
pass
# Overwrite the old file with the new time value
file = open(name, 'w')
file.writelines(lines)
newtime = [str(val) for val in newtime]
file.write("(%s)\n" % ", ".join(newtime))
file.close()
# End of file rssdl.py
Bookmarks