-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrss2slack.py
executable file
·257 lines (226 loc) · 6.86 KB
/
rss2slack.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
#!/usr/bin/env python3
"""Fetch RSS and post it to Slack channel.
2017/Sep/03 @ Zdenek Styblik <[email protected]>
"""
import argparse
import logging
import os
import sys
import time
import traceback
from typing import Dict
from typing import List
from typing import Tuple
from slack import WebClient
import rss2irc # noqa: I100, I202
from lib import config_options # noqa: I100, I202
SLACK_BASE_URL = WebClient.BASE_URL
def format_message(
url: str, msg_attrs: Tuple[str, str], handle: str = ""
) -> Dict:
"""Return formatted message as Slack's BlockKit section.
:param url: URL of news item.
:param msg_attrs: tuple of title and category.
:param handle: Handle of given feed.
"""
if handle:
if len(msg_attrs) > 1 and msg_attrs[1]:
tag = "[{:s}-{:s}] ".format(handle, msg_attrs[1])
else:
tag = "[{:s}] ".format(handle)
else:
tag = ""
return {
"type": "section",
"text": {
"type": "mrkdwn",
"text": "{}<{}|{}>".format(tag, url, msg_attrs[0]),
},
}
def get_slack_token() -> str:
"""Get Slack token from ENV variable.
:raises: `ValueError`
"""
slack_token = os.environ.get("SLACK_TOKEN", None)
if slack_token:
return slack_token
raise ValueError("SLACK_TOKEN must be set.")
def get_slack_web_client(token: str, base_url: str, timeout: int) -> WebClient:
"""Return instance of Slack Web Client."""
return WebClient(token, base_url=base_url, timeout=timeout)
def main():
"""Fetch RSS feed and post RSS news to Slack."""
logging.basicConfig(stream=sys.stdout, level=logging.ERROR)
logger = logging.getLogger("rss2slack")
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 = get_slack_token()
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 = rss2irc.parse_news(rsp.text)
if not news:
logger.info("No news?")
sys.exit(0)
source.extract_caching_headers(rsp.headers)
rss2irc.prune_news(logger, cache, news, args.cache_expiration)
rss2irc.scrub_items(logger, cache)
slack_client = get_slack_web_client(
slack_token,
base_url=args.slack_base_url,
timeout=args.slack_timeout,
)
if not args.cache_init:
for url in list(news.keys()):
msg_blocks = [format_message(url, news[url], args.handle)]
try:
post_to_slack(
logger,
msg_blocks,
slack_client,
args.slack_channel,
)
except ValueError:
news.pop(url)
finally:
time.sleep(args.sleep)
rss2irc.update_items_expiration(cache, news, args.cache_expiration)
cache.scrub_data_sources()
rss2irc.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(
"--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(
"--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=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 post_to_slack(
logger: logging.Logger,
msg_blocks: List,
slack_client: WebClient,
slack_channel: str,
) -> None:
"""Post news to Slack channel."""
try:
logger.debug("Will post %s", repr(msg_blocks))
rsp = slack_client.chat_postMessage(
channel=slack_channel, blocks=msg_blocks
)
logger.debug("Response from Slack: %s", rsp)
if not rsp or rsp["ok"] is False:
raise ValueError("Slack response is not OK.")
except ValueError:
logger.debug("%s", traceback.format_exc())
raise
if __name__ == "__main__":
main()