-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
executable file
·216 lines (165 loc) · 6.63 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env python3
import re
import time
from collections import namedtuple
from datetime import datetime
from typing import Any, Dict, Iterator, List, Optional
import config
import feedparser
import structlog
from telegram import Bot, ParseMode, TelegramError
def add_timestamp_logproc(_logger: Any, _method: str,
event_dict: Dict[str, Any]) -> Dict[str, Any]:
"""
Structlog processor, adding current timstamp to the log entry.
"""
event_dict['when'] = datetime.utcnow().isoformat()
return event_dict
def get_log_renderer(key: str) -> Any:
if key == 'json':
return structlog.processors.JSONRenderer(
sort_keys=True, ensure_ascii=False)
elif key == 'console':
return structlog.dev.ConsoleRenderer()
else:
raise ValueError("Unexpected logs renderer " + key)
logger = structlog.wrap_logger(
structlog.PrintLogger(),
processors=[
add_timestamp_logproc,
get_log_renderer(config.LOGS_RENDERER),
])
HTML_TAG_RE = re.compile(r"<[^>]+>")
def strip_html(html: str) -> str:
return HTML_TAG_RE.sub("", html)
Entry = namedtuple('Entry', ['title', 'pubdate', 'description', 'link'])
def from_feed_entry(e: feedparser.FeedParserDict) -> Entry:
return Entry(
strip_html(e.title.strip()),
datetime.fromtimestamp(time.mktime(e.published_parsed)),
strip_html(e.description.strip()), e.link)
class FeedPoller:
def __init__(self, url: str, blacklist: List[str]) -> None:
self.url = url
self.entries = [] # type: List[Entry]
self.last_pubdate = None # type: Optional[datetime]
self.blacklist = blacklist
def is_blacklist(self, e: Entry) -> bool:
title = e.title.lower()
description = e.description.lower()
for word in self.blacklist:
if word in title or word in description:
return True
return False
def update(self) -> List[Entry]:
upd_logger = logger.bind(
method=FeedPoller.update.__qualname__,
rss_url=self.url,
last_pubdate=self.last_pubdate)
upd_logger.info("Retrieving RSS entries")
feed = feedparser.parse(self.url)
if feed.get('bozo_exception'):
upd_logger.error(
"Error while retrieving RSS",
exception=str(feed.bozo_exception))
return []
self.entries = [from_feed_entry(e) for e in feed.entries]
new_entries = []
for entry in self.entries:
if not self.last_pubdate or entry.pubdate <= self.last_pubdate:
continue
if self.is_blacklist(entry):
logger.info("Project was blocked", title=entry.title)
continue
new_entries.append(entry)
upd_logger.info(
"Retrieved entries", new=len(new_entries), total=len(self.entries))
self.last_pubdate = max(
[e.pubdate for e in self.entries], default=None)
upd_logger.info("New pubdate", new_pubdate=self.last_pubdate)
return new_entries
def poll_packs(self, interval: int) -> Iterator[List[Entry]]:
pp_logger = logger.bind(
method=FeedPoller.poll_packs.__qualname__,
rss_url=self.url,
poll_interval=interval)
pp_logger.info("Started polling")
while True:
pp_logger.info("Poll iteration started")
started = datetime.now()
news = self.update()
pp_logger.info(
"Entries update finished",
elapsed=(datetime.now() - started).total_seconds())
if news:
yield news
ended = datetime.now()
delta = (ended - started).total_seconds()
pp_logger.info("Poll iteration finished", elapsed=delta)
if delta < interval:
time.sleep(interval - delta)
def poll(self, interval: int) -> Iterator[Entry]:
for pack in self.poll_packs(interval):
for entry in pack:
yield entry
class TelegramSender:
def __init__(self, bot_token: str, limit_desc: int = None) -> None:
self.bot_token = bot_token
self.bot = Bot(self.bot_token)
self.limit_desc = limit_desc
def format_entry_msg(self, entry: Entry) -> str:
template = "\n".join([
"<a href=\"{link}\">{title}</a>",
"{description}",
])
description = entry.description
if self.limit_desc and len(description) > self.limit_desc:
description = description[:self.limit_desc] + "..."
entry_msg = template.format(
link=entry.link, title=entry.title,
description=description).strip()
return entry_msg
def format_pack_msg(self, pack: List[Entry]) -> str:
messages = []
for entry in pack:
messages.append(self.format_entry_msg(entry))
pack_msg = "\n\n".join(messages)
return pack_msg
def format_error_msg(self, err: Exception, msg: str = None) -> str:
error_msg = "{}: {}".format(type(err).__qualname__, str(err))
if msg:
error_msg += "; Attempted to send:\n" + msg
return error_msg
def send_pack(self, _poller: FeedPoller, pack: List[Entry]) -> None:
sp_logger = logger.bind(
method=TelegramSender.send_pack.__qualname__, pack_size=len(pack))
msg = self.format_pack_msg(pack)
sp_logger.info("Formatted pack message", msg_size=len(msg))
try:
self.bot.send_message(
config.TARGET_CHAT_ID,
msg,
parse_mode=ParseMode.HTML,
disable_web_page_preview=True)
sp_logger.info("Message sent")
except TelegramError as error:
sp_logger.error("Sending failed", exception=str(error))
self.bot.send_message(config.TARGET_CHAT_ID,
self.format_error_msg(error, msg))
def send_single(self, poller: FeedPoller, entry: Entry) -> None:
self.send_pack(poller, [entry])
def main():
sender = TelegramSender(
config.BOT_TOKEN, limit_desc=config.LIMIT_DESCRIPTION)
poller = FeedPoller(url=config.RSS_URL, blacklist=config.BLOCKED_KEYWORDS)
try:
if config.SEND_BY_PACKS:
for pack in poller.poll_packs(config.POLL_INTERVAL):
sender.send_pack(poller, pack)
else:
for entry in poller.poll(config.POLL_INTERVAL):
sender.send_single(poller, entry)
except KeyboardInterrupt as e:
logger.info("Polling was interrupted by user")
if __name__ == '__main__':
main()