-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
make_and_send_alert.py
276 lines (219 loc) · 9.5 KB
/
make_and_send_alert.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
import asyncio
import logging
import os
import re
from typing import Tuple
import aiohttp
COMMIT_SHA = os.environ['COMMIT_SHA']
# commits for test alert builder
# COMMIT_SHA = '4015bd9c48b45910727569fff5e770000d85d207' # all clients + server and test server + web
# COMMIT_SHA = '9cc3f0fb7c390c8cb8b789e9377f10ed5e80a089' # web and web res together
# COMMIT_SHA = '4efaf918af43054ba3ff76068e83d135a9a2535d' # web
# COMMIT_SHA = 'e2d725c2b3813d7c170f50b0ab21424a71466f6d' # web res
TELEGRAM_BOT_TOKEN = os.environ['TELEGRAM_BOT_TOKEN']
DISCORD_BOT_TOKEN = os.environ['DISCORD_BOT_TOKEN']
GITHUB_PAT = os.environ['GITHUB_PAT']
REPOSITORY = os.environ.get('REPOSITORY', 'MarshalX/telegram-crawler')
ROOT_TREE_DIR = os.environ.get('ROOT_TREE_DIR', 'data')
CHAT_ID = os.environ.get('CHAT_ID', '@tgcrawl')
DISCORD_CHANNEL_ID = os.environ.get('DISCORD_CHANNEL_ID', '1116390634249523283')
BASE_GITHUB_API = 'https://api.github.com/'
GITHUB_LAST_COMMITS = 'repos/{repo}/commits/{sha}'
BASE_TELEGRAM_API = 'https://api.telegram.org/bot{token}/'
TELEGRAM_SEND_MESSAGE = 'sendMessage'
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
STATUS_TO_EMOJI = {
'added': '✅',
'modified': '📝',
'removed': '❌',
'renamed': '🔄',
'copied': '📋',
'changed': '📝',
'unchanged': '📝',
}
AVAILABLE_HASHTAGS = {
'web_tr', 'web_res', 'web', 'server', 'test_server', 'client',
'ios', 'macos', 'android', 'android_dl', 'mini_app', 'wallet'
}
HASHTAGS_PATTERNS = {
# regex will be more flexible. for example, in issue with double hashtag '#web #web_res' when data/res not changed
'web_tr': os.path.join(ROOT_TREE_DIR, 'web_tr'),
'web_res': os.path.join(ROOT_TREE_DIR, 'web_res'),
'web': os.path.join(ROOT_TREE_DIR, 'web'),
'server': os.path.join(ROOT_TREE_DIR, 'server'),
'test_server': os.path.join(ROOT_TREE_DIR, 'server', 'test'),
'client': os.path.join(ROOT_TREE_DIR, 'client'),
'ios': os.path.join(ROOT_TREE_DIR, 'client', 'ios-beta'),
'macos': os.path.join(ROOT_TREE_DIR, 'client', 'macos-beta'),
'android': os.path.join(ROOT_TREE_DIR, 'client', 'android-beta'),
'android_dl': os.path.join(ROOT_TREE_DIR, 'client', 'android-stable-dl'),
'mini_app': os.path.join(ROOT_TREE_DIR, 'mini_app'),
'wallet': os.path.join(ROOT_TREE_DIR, 'mini_app', 'wallet'),
}
# order is important!
PATHS_TO_REMOVE_FROM_ALERT = [
os.path.join(ROOT_TREE_DIR, 'web_tr'),
os.path.join(ROOT_TREE_DIR, 'web_res'),
os.path.join(ROOT_TREE_DIR, 'web'),
os.path.join(ROOT_TREE_DIR, 'server'),
os.path.join(ROOT_TREE_DIR, 'client'),
os.path.join(ROOT_TREE_DIR, 'mini_app'),
]
FORUM_CHAT_ID = '@tfcrawl'
HASHTAG_TO_TOPIC = {
'web': '2200',
'web_tr': '2202',
'web_res': '2206',
'server': '2317',
'ios': '2194',
'macos': '2187',
'android': '2190',
'android_dl': '12235',
'wallet': '5685',
}
GITHUB_API_LIMIT_PER_HOUR = 5_000
COUNT_OF_RUNNING_WORKFLOW_AT_SAME_TIME = 5 # just random number ;d
ROW_PER_STATUS = 5
LAST_PAGE_NUMBER_REGEX = r'page=(\d+)>; rel="last"'
async def send_req_until_success(session: aiohttp.ClientSession, **kwargs) -> Tuple[dict, int]:
delay = 5 # in sec
count_of_retries = int(GITHUB_API_LIMIT_PER_HOUR / COUNT_OF_RUNNING_WORKFLOW_AT_SAME_TIME / delay)
last_page_number = 1
retry_number = 1
while retry_number <= count_of_retries:
retry_number += 1
res = await session.get(**kwargs)
if res.status != 200:
await asyncio.sleep(delay)
continue
json = await res.json()
pagination_data = res.headers.get('Link', '')
matches = re.findall(LAST_PAGE_NUMBER_REGEX, pagination_data)
if matches:
last_page_number = int(matches[0])
return json, last_page_number
raise RuntimeError('Surprise. Time is over')
async def send_telegram_alert(session: aiohttp.ClientSession, text: str, thread_id=None) -> aiohttp.ClientResponse:
params = {
'chat_id': CHAT_ID,
'parse_mode': 'HTML',
'text': text,
'disable_web_page_preview': 1,
}
if thread_id:
params['chat_id'] = FORUM_CHAT_ID
params['message_thread_id'] = thread_id
return await session.get(
url=f'{BASE_TELEGRAM_API}{TELEGRAM_SEND_MESSAGE}'.format(token=TELEGRAM_BOT_TOKEN), params=params
)
async def send_discord_alert(
session: aiohttp.ClientSession, commit_hash: str, commit_url: str, fields: list, hashtags: str
) -> aiohttp.ClientResponse:
url = f'https://discord.com/api/channels/{DISCORD_CHANNEL_ID}/messages'
headers = {
'Authorization': f'Bot {DISCORD_BOT_TOKEN}',
}
embed_data = {
'title': f'New changes in Telegram ({commit_hash})',
'color': 0xe685cc,
'url': commit_url,
'fields': fields,
'author': {
'name': 'Marshal',
'url': 'https://github.com/MarshalX',
'icon_url': 'https://avatars.githubusercontent.com/u/15520314?v=4',
},
'footer': {
'text': hashtags,
}
}
payload = {
'embed': embed_data
}
return await session.post(url=url, headers=headers, json=payload)
async def main() -> None:
async with aiohttp.ClientSession() as session:
commit_data, last_page = await send_req_until_success(
session=session,
url=f'{BASE_GITHUB_API}{GITHUB_LAST_COMMITS}'.format(repo=REPOSITORY, sha=COMMIT_SHA),
headers={
'Authorization': f'token {GITHUB_PAT}'
}
)
commit_files = commit_data['files']
coroutine_list = list()
for current_page in range(2, last_page + 1):
coroutine_list.append(send_req_until_success(
session=session,
url=f'{BASE_GITHUB_API}{GITHUB_LAST_COMMITS}?page={current_page}'.format(
repo=REPOSITORY, sha=COMMIT_SHA
),
headers={
'Authorization': f'token {GITHUB_PAT}'
}
))
paginated_responses = await asyncio.gather(*coroutine_list)
for json_response, _ in paginated_responses:
commit_files.extend(json_response['files'])
commit_files = [file for file in commit_files if 'translations.telegram.org/' not in file['filename']]
if not commit_files:
return
commit_hash = commit_data['sha'][:7]
html_url = commit_data['html_url']
alert_text = f'<b>New changes of Telegram</b>\n\n'
alert_hashtags = set()
global AVAILABLE_HASHTAGS
available_hashtags = AVAILABLE_HASHTAGS.copy()
changes = {k: [] for k in STATUS_TO_EMOJI.keys()}
changes_md = {k: [] for k in STATUS_TO_EMOJI.keys()}
for file in commit_files:
for available_hashtag in available_hashtags:
pattern = HASHTAGS_PATTERNS[available_hashtag]
if pattern in file['filename']:
alert_hashtags.add(available_hashtag)
# optimize substring search
available_hashtags -= alert_hashtags
changed_url = file['filename'].replace('.html', '')
for path_to_remove in PATHS_TO_REMOVE_FROM_ALERT:
if changed_url.startswith(path_to_remove):
changed_url = changed_url[len(path_to_remove) + 1:]
break # can't occur more than one time
status = STATUS_TO_EMOJI[file['status']]
changes[file['status']].append(f'{status} <code>{changed_url}</code>')
changes_md[file['status']].append(f'- {changed_url}')
discord_embed_fields = []
for i, [status, text_list] in enumerate(changes.items()):
if not text_list:
continue
alert_text += '\n'.join(text_list[:ROW_PER_STATUS]) + '\n'
discord_field_value = '\n'.join(changes_md[status][:ROW_PER_STATUS]) + '\n'
if len(text_list) > ROW_PER_STATUS:
count = len(text_list) - ROW_PER_STATUS
alert_text += f'And <b>{count}</b> {status} actions more..\n'
discord_field_value += f'And **{count}** {status} actions more..\n'
discord_embed_fields.append({
'name': f'{STATUS_TO_EMOJI[status]} {status.capitalize()}',
'value': discord_field_value,
'inline': False
})
alert_text += '\n'
link_text = f'GitHub · MarshalX/telegram-crawler@{commit_hash}'
alert_text += f'<a href="{html_url}">{link_text}</a>'
logger.info(alert_text)
if 'web_tr' in alert_hashtags or 'web_res' in alert_hashtags:
alert_hashtags.remove('web')
for hashtag, topic_thread_id in HASHTAG_TO_TOPIC.items():
if hashtag in alert_hashtags:
logger.info(f'Sending alert to the forum. Topic: {topic_thread_id}')
telegram_response = await send_telegram_alert(session, alert_text, topic_thread_id)
logger.debug(await telegram_response.read())
hashtags = ' '.join([f'#{hashtag}' for hashtag in sorted(alert_hashtags)])
if alert_hashtags:
alert_text += '\n\n' + hashtags
telegram_response = await send_telegram_alert(session, alert_text)
logger.debug(await telegram_response.read())
discord_response = await send_discord_alert(session, commit_hash, html_url, discord_embed_fields, hashtags)
logger.debug(await discord_response.read())
if __name__ == '__main__':
asyncio.get_event_loop().run_until_complete(main())