-
Notifications
You must be signed in to change notification settings - Fork 0
/
FeedReader.py
49 lines (41 loc) · 1.44 KB
/
FeedReader.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
""" Threaded feed parsing """
import feedparser
from pprint import pprint
import Queue
import threading
entries = []
class FeedReader(threading.Thread):
""" Reads through a list of uri's (rss feeds in theory) """
def __init__(self):
threading.Thread.__init__(self)
self.queue = Queue.Queue()
self.entries = []
def parse(self, links):
global entries
entries = []
for i in range(5): # 5 threads
t = ThreadedParser(self.queue)
t.setDaemon(True)
t.start()
for link in links:
self.queue.put(link)
self.queue.join()
self.entries = entries
class ThreadedParser(threading.Thread):
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue
self.entries = []
def run(self):
while True:
link = self.queue.get()
entry = feedparser.parse(link)
global entries
for j in entry['entries']:
entries.append(j)
self.queue.task_done()
if __name__=='__main__':
FR = FeedReader()
FR.parse(['http://feeds.feedburner.com/RockPaperShotgun?format=xml', 'http://rss.slashdot.org/Slashdot/slashdot', 'http://www.1up.com/rss?x=1'])
#, 'http://www.reddit.com/r/python/.rss', 'http://rss.slashdot.org/Slashdot/slashdot', 'http://www.1up.com/rss?x=1'
pprint(FR.entries)