-
Notifications
You must be signed in to change notification settings - Fork 1
/
rss2irc.py
executable file
·351 lines (299 loc) · 9.63 KB
/
rss2irc.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#!/usr/bin/env python3
"""Fetch RSS and pipe it into IRC bot.
2015/Jul/5 @ Zdenek Styblik <[email protected]>
"""
import argparse
import logging
import os
import pickle
import signal
import stat
import sys
import time
import traceback
from typing import BinaryIO
from typing import Dict
from typing import Tuple
import feedparser
import requests
from lib import CachedData # noqa: I202
from lib import config_options # noqa: I202
def format_message(
url: str, msg_attrs: Tuple[str, str], handle: str = ""
) -> str:
"""Return pre-formatted message.
:param url: URL of news item.
:param msg_attrs: tuple of title and category.
:param handle: Handle of given feed.
"""
if not handle:
return "{:s}\n".format(url)
if msg_attrs[1]:
tag = "{:s}-{:s}".format(handle, msg_attrs[1])
else:
tag = "{:s}".format(handle)
return "[{:s}] {:s} | {:s}\n".format(tag, msg_attrs[0], url)
def get_rss(
logger: logging.Logger,
url: str,
timeout: int = config_options.HTTP_TIMEOUT,
extra_headers: Dict = None,
) -> requests.models.Response:
"""Return body of given URL as a string."""
# Randomize user agent, because CF likes to block for no apparent reason.
user_agent = "rss2irc_{:d}".format(int(time.time()))
headers = {"User-Agent": user_agent}
if extra_headers:
for key, value in extra_headers.items():
headers[key] = value
logger.debug("Get %s", url)
rsp = requests.get(url, timeout=timeout, headers=headers)
logger.debug("Got HTTP Status Code: %i", rsp.status_code)
rsp.raise_for_status()
return rsp
def main():
"""Fetch RSS feed and post RSS news to IRC."""
logging.basicConfig(stream=sys.stdout)
logger = logging.getLogger("rss2irc")
args = parse_args()
if args.verbosity:
logger.setLevel(logging.DEBUG)
if args.cache_expiration < 0:
logger.error("Cache expiration can't be less than 0.")
sys.exit(1)
if not os.path.exists(args.output):
logger.error("Ouput '%s' doesn't exist.", args.output)
sys.exit(1)
try:
cache = read_cache(logger, args.cache)
source = cache.get_source_by_url(args.rss_url)
rsp = get_rss(
logger,
args.rss_url,
args.rss_http_timeout,
source.make_caching_headers(),
)
if rsp.status_code == 304:
logger.debug("No new RSS data since the last run")
write_cache(cache, args.cache)
sys.exit(0)
if not rsp.text:
logger.error("Failed to get RSS from %s", args.rss_url)
sys.exit(1)
news = parse_news(rsp.text)
if not news:
logger.info("No news?")
write_cache(cache, args.cache)
sys.exit(0)
source.extract_caching_headers(rsp.headers)
prune_news(logger, cache, news, args.cache_expiration)
scrub_items(logger, cache)
if not args.cache_init:
write_data(logger, news, args.output, args.handle, args.sleep)
update_items_expiration(cache, news, args.cache_expiration)
cache.scrub_data_sources()
write_cache(cache, args.cache)
# TODO(zstyblik): remove error file
except Exception:
logger.debug("%s", traceback.format_exc())
# TODO(zstyblik):
# 1. touch error file
# 2. send error message to the channel
finally:
sys.exit(0)
def parse_args() -> argparse.Namespace:
"""Return parsed CLI args."""
parser = argparse.ArgumentParser()
parser.add_argument(
"-v",
"--verbose",
dest="verbosity",
action="store_true",
default=False,
help="Increase logging verbosity.",
)
parser.add_argument(
"--rss-url",
dest="rss_url",
type=str,
required=True,
help="URL of RSS Feed.",
)
parser.add_argument(
"--rss-http-timeout",
dest="rss_http_timeout",
type=int,
default=config_options.HTTP_TIMEOUT,
help="HTTP Timeout. Defaults to {:d} seconds.".format(
config_options.HTTP_TIMEOUT
),
)
parser.add_argument(
"--handle",
dest="handle",
type=str,
default=None,
help="IRC handle of this feed.",
)
parser.add_argument(
"--output",
dest="output",
type=str,
required=True,
help="Where to output formatted news.",
)
parser.add_argument(
"--cache",
dest="cache",
type=str,
default=None,
help="File which contains cache.",
)
parser.add_argument(
"--cache-expiration",
dest="cache_expiration",
type=int,
default=config_options.CACHE_EXPIRATION,
help="Time, in seconds, for how long to keep items in cache.",
)
parser.add_argument(
"--cache-init",
dest="cache_init",
action="store_true",
default=False,
help=(
"Prevents posting news to IRC. This is useful "
"when bootstrapping new RSS feed."
),
)
parser.add_argument(
"--sleep",
dest="sleep",
type=int,
default=2,
help="Sleep between messages in order to avoid Excess Flood at IRC.",
)
return parser.parse_args()
def parse_news(data: str) -> Dict[str, Tuple[str, str]]:
"""Parse-out link and title out of XML."""
news = {}
feed = feedparser.parse(data)
for entry in feed["entries"]:
link = entry.pop("link", "")
if not link:
# If we don't have a link, there is nothing we can do.
continue
title = entry.pop("title", "No title")
category = entry.pop("category", "")
news[link] = (title, category)
return news
def prune_news(
logger: logging.Logger,
cache: CachedData,
news: Dict[str, Tuple[str, str]],
expiration: int = config_options.CACHE_EXPIRATION,
) -> None:
"""Prune news which already are in cache."""
item_expiration = int(time.time()) + expiration
for key in list(news.keys()):
if key in cache.items:
logger.debug("Key %s found in cache", key)
cache.items[key] = item_expiration
news.pop(key)
def read_cache(logger: logging.Logger, cache_file: str) -> CachedData:
"""Read file with Py pickle in it."""
if not cache_file:
return CachedData()
try:
with open(cache_file, "rb") as fhandle:
cache = pickle.load(fhandle)
except FileNotFoundError:
cache = CachedData()
logger.warning("Cache file '%s' doesn't exist.", cache_file)
except EOFError:
# Note: occurred with empty file.
cache = CachedData()
logger.debug(
"Cache file '%s' is probably empty: %s",
cache_file,
traceback.format_exc(),
)
logger.debug("%s", cache)
return cache
def signal_handler(signum, frame):
"""Handle SIGALRM signal."""
raise TimeoutError
def scrub_items(logger: logging.Logger, cache: CachedData) -> None:
"""Scrub cache and remove expired items."""
time_now = time.time()
for key in list(cache.items.keys()):
try:
expiration = int(cache.items[key])
except ValueError:
logger.error("%s", traceback.format_exc())
logger.error(
"Invalid cache entry will be removed: '%s'", cache.items[key]
)
cache.items.pop(key)
continue
if expiration < time_now:
logger.debug("URL %s has expired.", key)
cache.items.pop(key)
def update_items_expiration(
cache: CachedData,
news: Dict[str, Tuple[str, str]],
expiration: int = config_options.CACHE_EXPIRATION,
) -> None:
"""Update expiration of items in cache based on news dict."""
item_expiration = int(time.time()) + expiration
for key in list(news.keys()):
cache.items[key] = item_expiration
def write_cache(data: CachedData, cache_file: str) -> None:
"""Dump data into file as a pickle."""
if not cache_file:
return
with open(cache_file, "wb") as fhandle:
pickle.dump(data, fhandle, pickle.HIGHEST_PROTOCOL)
def write_data(
logger: logging.Logger,
data: Dict,
output: str,
handle: str = None,
sleep: int = 2,
) -> None:
"""Write data into file."""
signal.signal(signal.SIGALRM, signal_handler)
signal.alarm(config_options.PIPE_OPEN_TIMEOUT)
with open(output, "wb") as fhandle:
signal.alarm(0)
for url in list(data.keys()):
message = format_message(url, data[url], handle)
try:
write_message(logger, fhandle, message)
time.sleep(sleep)
except (TimeoutError, ValueError):
logger.debug("%s", traceback.format_exc())
logger.debug("Failed to write %s, %s", url, data[url])
data.pop(url)
def write_message(
logger: logging.Logger, fhandle: BinaryIO, message: str
) -> None:
"""Write message into file handle.
Sets up SIGALRM and raises `TimeoutError` if alarm is due.
"""
signal.signal(signal.SIGALRM, signal_handler)
signal.alarm(config_options.PIPE_WRITE_TIMEOUT)
try:
fhandle_stat = os.fstat(fhandle.fileno())
is_fifo = stat.S_ISFIFO(fhandle_stat.st_mode)
if not is_fifo:
raise ValueError("fhandle is expected to be a FIFO pipe")
logger.debug("Will write %s", repr(message))
fhandle.write(message.encode("utf-8"))
signal.alarm(0)
except Exception as exception:
raise exception
finally:
signal.alarm(0)
if __name__ == "__main__":
main()