forked from jordsti/rsswatcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RssWatcher.py
69 lines (43 loc) · 1.53 KB
/
RssWatcher.py
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
'''
Created on 2013-02-04
@author: JordSti
'''
import urllib2
from xml.dom.minidom import parseString
import re
import TorrentItem
import RulesFile
class RssWatcher:
def __init__(self, rssUrl):
self.rssUrl = rssUrl
self.torrents = []
self.xmlAvailable = False
def fetchRss(self):
try:
f = urllib2.urlopen(self.rssUrl)
self.xmlRss = f.read()
f.close()
self.xmlAvailable = True
except:
print "Request time out !"
self.xmlRss = ""
def parseXml(self):
if self.xmlAvailable:
dom = parseString(self.xmlRss)
self.items = dom.getElementsByTagName('item')
for i in enumerate(self.items):
title = self.getText(i[1], 'title')
link = self.getText(i[1], 'link')
id = self.parseId(link)
torrent = TorrentItem.TorrentItem(title,link,id)
self.torrents.append(torrent)
else:
print "Unable to parse, xml feed wasn't downloaded..."
def getText(self, node, tagname):
textnode = node.getElementsByTagName(tagname)[0].childNodes[0]
return textnode.data
def parseId(self, link):
regex = '^.+id=([0-9]+)$'
p = re.compile(regex)
m = p.match(link)
return m.group(1)