#!/usr/bin/env python
# Copyright (c) 2008, Jeremy Grosser
# Open source under New BSD License. See LICENSE file for details
# Where the patterns and downloaded files are stored
RSSNZB_DIR = '/etc/rssnzb'
# Where to put downloaded NZB files
DOWNLOAD_DIR = '/mnt/media/upload'
# NZB RSS feed URL
RSS_URLS = [
'http://www.tvnzb.com/tvnzb.rss',
]
# Send an email listing newly downloaded files
MAIL_ENABLED = False
MAIL_FROM = "hella@localhost"
MAIL_TO = "myuser@localhost"
MAIL_SERVER = "localhost"
#### Modify anything beyond this point at your own risk ####
from urllib import urlretrieve
from xmlrpclib import ServerProxy
import feedparser
import re
patterns = open('%s/patterns' % RSSNZB_DIR, 'r')
dl = open('%s/downloaded' % RSSNZB_DIR, 'r')
downloaded = []
for line in dl.readlines():
downloaded.append(line[:-1])
dl.close()
messages = []
def download_nzbs(url):
feed = feedparser.parse(url)
for p in patterns.readlines():
#print 'Pattern:', repr(p)
regex = re.compile(p[:-1], re.IGNORECASE)
for entry in feed['entries']:
#print '\t', repr(entry.title)
if regex.match(entry.title) != None and not (entry.title in downloaded):
print 'Downloading %s (%s)' % (entry.title, entry.link)
urlretrieve(entry.link, '%s/%s.nzb' % (DOWNLOAD_DIR, entry.title))
messages.append('%s (%s)' % (entry.title, entry.link))
downloaded.append(entry.title)
patterns.close()
dl = open('%s/downloaded' % RSSNZB_DIR, 'w')
for t in downloaded:
dl.write("%s\n" % t)
dl.close()
return messages
def main():
messages = []
for url in RSS_URLS:
messages += download_nzbs(url)
#if len(messages) > 0:
# from xmlrpclib import ServerProxy
# server = ServerProxy('http://caliburn.csh.rit.edu:9611/')
# for msg in messages:
# server.message('hella: %s' %msg)
if MAIL_ENABLED and len(messages) > 0:
from email.Message import Message
#print 'Sending email to %s' % EMAIL_TO
email = Message()
email.set_unixfrom(MAIL_FROM)
email.add_header('Subject', '[hella] Queued %s files' % len(messages))
content = ''
for msg in messages:
content += '%s\r\n' % msg
email.set_payload(content)
email = email.as_string()
server = SMTP(MAIL_SERVER)
server.sendmail(MAIL_FROM, MAIL_TO, email)
server.quit()
if __name__ == '__main__':
main()