-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRSS_prefix_getter.py
74 lines (57 loc) · 1.66 KB
/
RSS_prefix_getter.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
68
69
70
71
72
73
74
from prefix_getter import PrefixGetter
import feedparser
import random
import time
import logging
import sys
import requests
from io import BytesIO
class RSSPrefixGetter(PrefixGetter):
def __init__(self, feeds):
self.rss_feeds = feeds
self.logger = logging.getLogger("PrefixGetter")
self.feed_responses = {}
self.index = 0
self.posts = []
self.num_entries = 0
def getFeed(self, source):
if source in self.feed_responses:
self.logger.info("getFeed: " + source + " already exists")
return self.feed_responses[source]
# Do request using requests library and timeout
resp = requests.get(source, timeout=20.0)
content = BytesIO(resp.content)
print(resp.content)
self.logger.info("got response!")
# Parse content
feed = feedparser.parse(content)
feed_num_entries = len(feed.entries)
self.num_entries += feed_num_entries
self.feed_responses[source] = feed
return self.feed_responses[source]
def getPost(self):
source = random.choice(self.rss_feeds)
self.logger.info("getPost: " + source)
try:
NewsFeed = self.getFeed(source)
entry = random.choice(NewsFeed.entries)
if self.num_entries != 0 and len(self.posts) >= self.num_entries:
self.feed_responses = {}
self.posts = []
self.num_entries = 0
if(entry['title'] in self.posts):
return self.getPost()
self.posts.append(entry['title'])
return entry
except KeyboardInterrupt:
e = sys.exc_info()[0]
raise e
except: # catch all
e = sys.exc_info()[0]
self.logger.error(e)
time.sleep(10)
return self.getPost()
def getPrefix(self, bot, last_message) -> str:
entry = self.getPost()
bot.talk(entry['link'])
return entry['title']