Created
November 18, 2012 19:34
-
-
Save adamwg/4107061 to your computer and use it in GitHub Desktop.
torrentdl.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import cPickle | |
import feedparser | |
import os | |
import os.path | |
import re | |
import subprocess | |
import socket | |
class Show: | |
name = None | |
minSeason = None | |
minEpisode = None | |
directory = None | |
re = None | |
def __init__(self, n, ms, me, d): | |
self.name = n | |
self.minSeason = ms | |
self.minEpisode = me | |
self.directory = d | |
self.re = re.compile('^' + n.replace(' ', r'\W+'), re.I) | |
def __cmp__(self, other): | |
return cmp(self.name, other.name) | |
def __str__(self): | |
return "%s %d %d %s" % (self.name, self.minSeason, self.minEpisode, | |
self.directory) | |
def before(self, season, episode): | |
if season is None or episode is None: | |
return False | |
if season > self.minSeason: | |
return True | |
if season == self.minSeason and episode > self.minEpisode: | |
return True | |
return False | |
shows = [ Show("community", 4, 0, "/home/awg/media/TV/Community/Season %d"), | |
Show("mythbusters", 11, 0, "/home/awg/media/TV/Mythbusters/Season %d"), | |
Show("fringe", 6, 0, "/home/awg/media/TV/Fringe/Season %d"), | |
Show("(the)? big bang theory", 6, 0, | |
"/home/awg/media/TV/Big Bang Theory/Season %d"), | |
Show("greys anatomy", 9, 0, "/home/awg/media/TV/Grey's Anatomy/Season %d"), | |
Show("top gear", 19, 0, "/home/awg/media/TV/Top Gear/Season %d"), | |
Show("how i met your mother", 8, 0, | |
"/home/awg/media/TV/How I Met Your Mother/Season %d"), | |
Show("mad men", 6, 0, "/home/awg/media/TV/Mad Men/Season %d"), | |
Show("boardwalk empire", 3, 0, | |
"/home/awg/media/TV/Boardwalk Empire/Season %d"), | |
Show("the office", 9, 0, "/home/awg/media/TV/The Office/Season %d"), | |
] | |
excludes = re.compile(r'.*(?:720p|1080p)', re.I) | |
sere = [ re.compile(r'.*\W+S(\d+)E(\d+)', re.I), | |
re.compile(r'.*\W+(\d+)x(\d+)', re.I), | |
] | |
feeds = [ "http://www.ezrss.it/feed/", | |
"http://showrss.karmorra.info/feeds/all.rss", | |
] | |
def sefor(item): | |
season = None | |
episode = None | |
for ser in sere: | |
se = ser.match(item['title']) | |
if se is not None: | |
season = int(se.group(1)) | |
episode = int(se.group(2)) | |
break | |
return (season, episode) | |
def showfor(shows, item): | |
show = None | |
for s in shows: | |
if s.re.match(item['title']) is not None: | |
show = s | |
break | |
if show is None: | |
return None | |
if excludes.match(item['title']) is not None: | |
return None | |
season, episode = sefor(item) | |
if show.before(season, episode): | |
return show | |
return None | |
if __name__ == "__main__": | |
os.umask(022) | |
theshows = [] | |
pklfname = os.path.expanduser('~/.torrentdl.pkl') | |
if os.path.exists(pklfname): | |
with open(pklfname, "r") as pklFile: | |
theshows = cPickle.load(pklFile) | |
theshows = filter(lambda x: x in shows, theshows) | |
theshows += filter(lambda x: x not in theshows, shows) | |
print "Fetching feeds" | |
socket.setdefaulttimeout(20) | |
feedobjs = [feedparser.parse(feed) for feed in feeds] | |
items = [] | |
for f in feedobjs: | |
items += f['items'] | |
print "Analyzing feeds" | |
for item in items: | |
show = showfor(theshows, item) | |
if show is not None: | |
season, episode = sefor(item) | |
dirname = show.directory % season | |
if not os.path.exists(dirname): | |
os.makedirs(dirname) | |
print "Downloading %s to %s" % (item['link'], dirname) | |
ret = subprocess.call([ "/home/awg/.local/bin/transmission-add", | |
item['link'], dirname ]) | |
show.minSeason, show.minEpisode = season, episode | |
with open(pklfname, "w") as pklFile: | |
cPickle.dump(theshows, pklFile) | |
print "Done" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment