-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
77 lines (62 loc) · 1.99 KB
/
main.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
75
76
77
#!/usr/bin/env python3
import os
import sys
import logging
import requests
import feedparser
from rssFilter import RssFilter
from bot import bot
logging.basicConfig(
format="%(asctime)s - %(name)s - [%(levelname)s] %(message)s",
level=logging.INFO
)
from config import config
logger = logging.getLogger(__name__)
if sys.version_info[0] < 3:
logger.warn("NOTICE: Your python version is lower than 3!")
if config.get('debug'):
logging.getLogger('root').setLevel(logging.DEBUG)
logger.setLevel(logging.DEBUG)
def getFeedEntries(url):
proxy = config.get('proxy')
if proxy:
proxies = {
'https': proxy
}
resp = requests.get(url, proxies=proxies)
feed = feedparser.parse(resp.text)
else:
feed = feedparser.parse(url)
entries = feed.get('entries', [])
if len(entries) == 0:
logger.warn('empty entries!')
return entries
def main():
subscriptionList = config.get('subscriptions', [])
for subInfo in subscriptionList:
logger.debug('checking {}...'.format(subInfo['name']))
try:
entries = getFeedEntries(subInfo['url'])
# filter
rssFilter = RssFilter(entries)
interval = config.get('interval')
if interval:
rssFilter = rssFilter.filterTime(config.get('interval'))
else:
logger.warn('config.interval not set!')
for key, value in subInfo.items():
if key.startswith('filter_') and value:
rssFilter = getattr(rssFilter, key)(value)
entries = rssFilter.feedEntries[::-1] # reverse
# send message
for item in entries:
bot.sendRssUpdateMessage(subInfo, item)
except Exception:
logger.exception('Exception in {}'.format(
subInfo['name']))
logger.info('done!')
if __name__ == '__main__':
try:
main()
except Exception as e:
logger.exception(e)