This repository has been archived by the owner on Mar 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
394 lines (365 loc) · 17.6 KB
/
main.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import sys
from datetime import timedelta
from itertools import repeat
from json import loads as loadsJSON
from os import getenv, popen, WEXITSTATUS
from os.path import abspath, dirname
from pprint import pformat
from threading import Thread
from time import gmtime, sleep, strftime, time
from flask import Flask, Response
from flask_httpauth import HTTPBasicAuth
from requests import post
from werkzeug.security import check_password_hash
codes = loadsJSON(getenv('CODES'))
out = list(repeat([None, 0], len(codes)))
up = time()
app = Flask(__name__)
auth = HTTPBasicAuth()
users = loadsJSON(getenv("PASSWD"))
admins = loadsJSON(getenv("ADMINS"))
skip = loadsJSON(getenv("SKIP"))
scriptdir = dirname(abspath(__file__)).rstrip('/') + '/'
def runwarning(parsed):
meetcode = parsed.get('meetcode')
if meetcode:
meetcode = meetcode.split('-')
meetcode[1] = ''.join(repeat('\\*', len(meetcode[1])))
meetcode = '-'.join(meetcode)
meetcode = str(meetcode)
meetingspace = str(parsed.get('spacecode').split('/')[1])
meetingspacelen = len(meetingspace)
meetingspace = meetingspace[0:meetingspacelen // 2] + \
''.join(repeat('\\*', meetingspacelen - (meetingspacelen // 2)))
# TODO: Use cards for more data + visually pleasing format
if getenv('GCHATWEBHOOK', False):
print("https://chat.googleapis.com/v1/%s/messages?%s" % (getenv('GCHATSPACE'), getenv('GCHATWEBHOOK')))
print(post(
"https://chat.googleapis.com/v1/%s/messages?%s" % (getenv('GCHATSPACE'), getenv('GCHATWEBHOOK')),
json={
'thread': {
'name': '/'.join([getenv('GCHATSPACE'), getenv('GCHATTHREAD')])
},
'text': """<users/all> *{classname}*
A <https://meet.google.com/{meetpath}|Google Meet> has resolved! This usually means a teacher joined the meet.
You can also check the the <https://classroom.google.com/c/{classcode}|Google Classroom for this course>.
""".format(
classname=parsed['classname'],
classcode=parsed['classcode'],
meetpath='/'.join(['lookup' if '-' not in parsed['input'] else '_meet', parsed['input']])
),
'cards': [
{
'header': {
'title': "GMDA - Chat Alerts",
'subtitle': "by Oliver Chen EPSB/GVH/G9",
'imageStyle': "AVATAR",
'imageUrl': "https://www.gstatic.com/images/branding/product/2x/hh_chat_64dp.png"
},
'sections': [
{
'header': 'Google Meet',
'widgets': [
{
'keyValue': {
'topLabel': 'Meet ID',
'content': '/'.join(['lookup' if '-' not in parsed['input'] else '_meet', parsed['input']]),
'contentMultiline': False,
'iconUrl': 'https://www.gstatic.com/images/branding/product/2x/hh_meet_64dp.png'
},
'buttons': [
{
'textButton': {
'text': "OPEN MEET",
'onClick': {
'openLink': {
'url': '/'.join([
'https://meet.google.com',
'lookup' if '-' not in parsed['input'] else '_meet',
parsed['input']
])
}
}
}
}
]
}
]
},
{
'header': 'Google Classroom',
'widgets': [
{
'keyValue': {
'topLabel': 'Classroom Subject',
'content': parsed['classname'],
'contentMultiline': False,
'iconUrl': 'https://www.gstatic.com/images/branding/product/2x/classroom_64dp.png'
},
'buttons': [
{
'textButton': {
'text': "OPEN CLASSROOM",
'onClick': {
'openLink': {
'url': '/'.join(['https://classroom.google.com/c',
parsed['classcode']])
}
}
}
}
]
}
]
},
{
'widgets': [
{
'keyValue': {
'topLabel': 'Timestamp',
'content': strftime("%Y-%m-%d %H:%M:%S GMT", gmtime()),
'contentMultiline': False,
'onClick': {
'openLink': {
'url': 'https://time.is/UTC'
}
},
'icon': 'CLOCK'
}
}
]
},
{
'header': 'Join us on Discord!',
'widgets': [
{
'buttons': [
{
'imageButton': {
'onClick': {
'openLink': {
'url': getenv('DISCORDINV', 'https://discord.gg/')
}
},
'name': 'Discord Invite',
'iconUrl': 'https://discord.com/assets/2c21aeda16de354ba5334551a883b481.png'
}
},
{
'textButton': {
'text': 'GET INVITE',
'onClick': {
'openLink': {
'url': getenv('DISCORDINV', 'https://discord.gg/')
}
}
}
},
{
'textButton': {
'text': 'REGISTER',
'onClick': {
'openLink': {
'url': 'https://discord.com/register'
}
}
}
}
],
'textParagraph': {
'text': "Google Meet end alerts, additional Meet verification, dev tools, "
"and fault explanations are only available on the Discord server."
}
}
]
},
{
'widgets': [
{
'textParagraph': {
'text': "<font color=\"#888888\">This service provided by Oliver Chen is "
"provided \"AS IS\". Oliver makes no other warranties, "
"expressive or implied, and hereby disclaims all implied "
"warranties, including any warranty of merchantability and "
"warranty of fitness for a particular purpose. v9.6.0</font>"
}
}
]
}
],
'cardActions': []
}
]
}
).text)
post(
getenv('WEBHOOK'),
json={
'content': "<@&%s>\nDisclaimer: Google Meet Discord Alerter by Oliver Chen is provided \"AS IS\". Oliver "
"makes no other warranties, expressive or implied, and hereby disclaims all implied "
"warranties, including any warranty of merchantability and warranty of fitness for a "
"particular purpose." % getenv("EVENT_PING_ID"),
"embeds": [{
"title": "Google Meet started!",
"description": "A Google Meet for your class `%s` resolved!\nThis usually means a teacher has joined the meet!\nHurry and join in ASAP!" %
parsed['classname'],
"color": 8977942,
"timestamp": strftime("%Y-%m-%dT%H:%M:%SZ", gmtime()),
"footer": {
"icon_url": "https://cdn.discordapp.com/avatars/694234706430001222/65e0958748317bbf206260acd6dba539.png",
"text": "idontknow#8950"
},
"author": {
"name": "Google Meet Alerter Bot - Reminder",
"url": "https://github.com/ochen1/google-meet-discord-alerter",
"icon_url": "https://cdn.discordapp.com/avatars/755506478483374230/115ee7a0c067f5b6f2490207f44af318.png"
},
"fields": [
{
"name": "<:gclassroom:836629105213571134> Google Classroom",
"value": "Click [<:gclassroom:836629105213571134> here](https://classroom.google.com/c/%s) to go to the event Classroom." %
parsed['classcode'],
"inline": False
},
{
"name": "<:gmeet:836629017774915625> Google Meet",
"value": "Click [<:gmeet:836629017774915625> here](https://meet.google.com/%s%s) to enter the meet." %
('lookup/' if '-' not in parsed['input'] else '_meet/', parsed['input']),
"inline": False
},
{
"name": ":school: Organization",
"value": parsed.get('organization', 'none'),
"inline": True
},
{
"name": ":1234: Max Meet Size",
"value": parsed.get('maxmeetsize', 'none'),
"inline": True
},
{
"name": "\u200B",
"value": "\u200B",
"inline": False
},
{
"name": ":technologist: Developer Info",
"value": "Fields below this one should be used with caution, if used at all.\n"
"The intention of these fields is to help developers debug their code.",
"inline": False
},
{
"name": ":spy: Verify Your Meet",
"value": "The meeting code we've resolved for this code is: %s" % meetcode,
"inline": True
},
{
"name": ":spy: Verify Your Meeting Space",
"value": "The meeting space we've resolved for this code is: spaces/%s" % meetingspace,
"inline": True
},
{
"name": "Want to contribute?",
"value": "[Report an issue/suggestion](https://ochen1.github.io/google-meet-discord-alerter/ticket.html) or email o.chen1@share.epsb.ca to help develop!",
"inline": False
}
]
}]
}
)
def check():
hours = (time() / 3600) % 24
if skip[0] < hours < skip[1]:
return
global out
for i, code in enumerate(codes):
print('Testing:', code, sep='\t')
rf = popen(' '.join(["python3", scriptdir + "test2.py", code[1]]))
rc = rf.read()
print(rc)
ret = rf.close()
ret = 0 if not ret else WEXITSTATUS(ret)
if getenv("TEST2_DEBUG_LOG_ENDPOINT") is not None:
post(
getenv("TEST2_DEBUG_LOG_ENDPOINT"),
json={
"content": "`{}`\n```\n{}\n```".format(
' - '.join([str(i), "{0} ({2}) - {1}".format(*code), str(ret)]), rc)
}
)
serialized = dict(map(lambda line: tuple(line.split(':\t', 1)), rc.strip().split('\n')))
serialized['retcode'] = ret
serialized['classname'] = code[0]
serialized['classcode'] = code[2]
serialized['input'] = code[1]
print(repr(serialized))
if ret == 0 and ret != out[i][0] and serialized.get('organization') is not None:
runwarning(serialized)
if ret != 0 and out[i][0] == 0 and ret != out[i][0]:
post(
getenv('WEBHOOK'),
json={
'content': "It looks like the **<:gmeet:836629017774915625> Google Meet** just ended! :tada:"
}
)
out[i] = [6 if ret == 0 and serialized.get('organization') is None else ret, time()]
@auth.verify_password
def verify_password(username, password):
if username in users and check_password_hash(users.get(username), password):
return username
@app.route('/index')
def locateIndex():
return Response('\n'.join([
'/index',
'/',
'/globals',
'/force',
]), content_type="text/plain")
@app.route('/')
@auth.login_required
def index():
s = f"""{sys.version}
Program is running.
Current Time: {time()}
Current Time: {strftime('%Y-%m-%d %H:%M:%S %Z', gmtime())}
Up since: {strftime('%Y-%m-%d %H:%M:%S %Z', gmtime(up))} ({up})
Uptime: {str(timedelta(seconds=round(time() - up, 2)))}
Codes: {repr(codes)}
Output: {', '.join(map(repr, out))}
"""
return Response(s, content_type="text/plain")
@app.route('/globals')
@auth.login_required
def globalsIndex():
s = f"Globals:\n{pformat(dict(filter(lambda li: li[0] != 'users' and li[0] != 'admins', globals().items())))}\n\
Note: global variables \"users\" and \"admins\" have been excluded from the list for security reasons.\
"
return Response(s, content_type="text/plain")
@app.route('/force')
@auth.login_required
def forceCheck():
if auth.current_user() in admins:
check()
return "OK!"
else:
return "Denied."
@app.route('/ping')
def ping():
return "Pong!"
if __name__ == "__main__":
print("Start.")
webThread = Thread(target=app.run, kwargs={
'host': "0.0.0.0",
'port': getenv('PORT')
})
webThread.daemon = True
webThread.start()
try:
while True:
sleep(60 - (time() % 60))
check()
except KeyboardInterrupt:
print()
finally:
print("Stop.")
sys.exit()