-
Notifications
You must be signed in to change notification settings - Fork 1
/
phpbb2slack.py
executable file
·328 lines (288 loc) · 9.2 KB
/
phpbb2slack.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
#!/usr/bin/env python3
"""Fetch RSS feed from phpBB forum and post it to Slack channel.
2017/Nov/15 @ Zdenek Styblik <[email protected]>
"""
import argparse
import logging
import sys
import time
import traceback
from typing import Dict
from typing import List
import feedparser
import rss2irc # noqa: I202
import rss2slack # noqa: I202
from lib import CachedData # noqa: I202
from lib import config_options # noqa: I202
def format_message(
url: str, msg_attrs: Dict[str, str], handle: str = ""
) -> Dict:
"""Return formatted message as Slack's BlockKit section.
:raises: `KeyError`
"""
if handle:
if "category" in msg_attrs and msg_attrs["category"]:
tag = "[{:s}-{:s}] ".format(handle, msg_attrs["category"])
else:
tag = "[{:s}] ".format(handle)
else:
tag = ""
return {
"type": "section",
"text": {
"type": "mrkdwn",
"text": "{:s}<{:s}|{:s}> ({:d})".format(
tag, url, msg_attrs["title"], msg_attrs["comments_cnt"]
),
},
}
def get_authors_from_file(logger: logging.Logger, fname: str) -> List[str]:
"""Return list of authors of interest from given file."""
if not fname:
return []
try:
with open(fname, "rb") as fhandle:
authors = [
line.decode("utf-8").strip()
for line in fhandle.readlines()
if line.decode("utf-8").strip() != ""
]
except Exception:
logger.error("%s", traceback.format_exc())
authors = []
return authors
def main():
"""Fetch phpBB RSS feed and post RSS news to Slack."""
logging.basicConfig(stream=sys.stdout, level=logging.ERROR)
logger = logging.getLogger("phpbb2slack")
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)
try:
slack_token = rss2slack.get_slack_token()
authors = get_authors_from_file(logger, args.authors_file)
cache = rss2irc.read_cache(logger, args.cache)
source = cache.get_source_by_url(args.rss_url)
rsp = rss2irc.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")
rss2irc.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, authors)
if not news:
logger.info("No news?")
sys.exit(0)
source.extract_caching_headers(rsp.headers)
prune_news(logger, cache, news, args.cache_expiration)
scrub_items(logger, cache)
slack_client = rss2slack.get_slack_web_client(
slack_token, args.slack_base_url, args.slack_timeout
)
if not args.cache_init:
for url in list(news.keys()):
msg_blocks = [format_message(url, news[url], args.handle)]
try:
rss2slack.post_to_slack(
logger,
msg_blocks,
slack_client,
args.slack_channel,
)
except ValueError:
news.pop(url)
finally:
time.sleep(args.sleep)
update_items_expiration(cache, news, args.cache_expiration)
cache.scrub_data_sources()
rss2irc.write_cache(cache, args.cache)
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(
"--authors-of-interest",
dest="authors_file",
type=str,
default=None,
help=(
"Path to file which contains list of authors, one per line. "
"Only threads which are started by one of the authors on the "
"list will be pushed."
),
)
parser.add_argument(
"--cache",
dest="cache",
type=str,
default=None,
help="Path to cache file.",
)
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(
"--handle",
dest="handle",
type=str,
default=None,
help="Handle/callsign of this feed.",
)
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(
"--slack-base-url",
dest="slack_base_url",
type=str,
default=rss2slack.SLACK_BASE_URL,
help="Base URL for Slack client.",
)
parser.add_argument(
"--slack-channel",
dest="slack_channel",
type=str,
required=True,
help="Name of Slack channel to send formatted news to.",
)
parser.add_argument(
"--slack-timeout",
dest="slack_timeout",
type=int,
default=config_options.HTTP_TIMEOUT,
help="Slack API Timeout. Defaults to {:d} seconds.".format(
config_options.HTTP_TIMEOUT
),
)
parser.add_argument(
"--sleep",
dest="sleep",
type=int,
default=2,
help=(
"Sleep between messages in order to avoid "
"possible excess flood/API call rate limit."
),
)
parser.add_argument(
"-v",
"--verbose",
dest="verbosity",
action="store_true",
default=False,
help="Increase logging verbosity.",
)
return parser.parse_args()
def parse_news(data: str, authors: List[str]) -> Dict:
"""Parse-out link and title out of XML."""
news = {}
feed = feedparser.parse(data)
for entry in feed["entries"]:
link = entry.pop("link", None)
if not link:
# If we don't have a link, there is nothing we can do.
continue
author_detail = entry.pop("author_detail", {"name": None})
if authors and author_detail["name"] not in authors:
continue
title = entry.pop("title", "No title")
category = entry.pop("category", None)
comments_cnt = entry.pop("slash_comments", 0)
try:
comments_cnt = int(comments_cnt)
except ValueError:
comments_cnt = 0
news[link] = {
"title": title,
"category": category,
"comments_cnt": int(comments_cnt),
}
return news
def prune_news(
logger: logging.Logger,
cache: CachedData,
news: Dict[str, Dict],
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 not in cache.items:
continue
logger.debug("Key %s found in cache", key)
comments_cached = int(cache.items[key]["comments_cnt"])
comments_actual = int(news[key]["comments_cnt"])
if comments_cached == comments_actual:
cache.items[key]["expiration"] = item_expiration
news.pop(key)
def scrub_items(logger: logging.Logger, cache: CachedData) -> None:
"""Scrub cache and remove expired items."""
time_now = int(time.time())
for key in list(cache.items.keys()):
try:
expiration = int(cache.items[key]["expiration"])
except (KeyError, 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, expiration: int
) -> None:
"""Update cache contents."""
item_expiration = int(time.time()) + expiration
for key in list(news.keys()):
cache.items[key] = {
"expiration": item_expiration,
"comments_cnt": int(news[key]["comments_cnt"]),
}
if __name__ == "__main__":
main()