-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.py
executable file
·38 lines (31 loc) · 1.05 KB
/
update.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
#!/usr/bin/env python3
import sqlite3
import collections
import utils
def update_channel(name : str, codes : list):
header_parts = []
for code in codes:
res = utils.toss_api('/courses/' + code)
if res.status_code == 200:
# TODO: ignore outdated courses with same channel name but different course code
header_parts.append(utils.channel_header(res.json()))
else:
print('TOSS did not find', code)
header = ' | '.join(header_parts)
res = utils.mm_api(f'/teams/{utils.VOWI_TEAMID}/channels/name/{name}')
if res.status_code == 200:
channel = res.json()
utils.mm_api(method='put', path=f'/channels/{channel["id"]}/patch',
json={'header': header})
else:
print('Mattermost did not find', name)
def update_all(cur):
cur.execute('SELECT name, code FROM code_to_name')
name2codes = collections.defaultdict(list)
for result in cur.fetchall():
name2codes[result[0]].append(result[1])
for name, codes in name2codes.items():
update_channel(name, codes)
if __name__ == '__main__':
conn = sqlite3.connect(utils.DBFILE)
update_all(conn.cursor())