public
Description: Download NZBs from an RSS feed
Homepage: http://www.neohippie.net/blog/tags/rssnzb/
Clone URL: git://github.com/synack/rssnzb.git
rssnzb / rssnzb.py
100755 86 lines (71 sloc) 2.28 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/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()