-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
436 lines (406 loc) · 17.2 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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
import platform
import sys
import threading
import asyncio
from httpx import CloseError
from playsound import playsound
import selenium
from datetime import datetime, time
from libroosx import keychain_proxy_login, SynAClient
from libroosx.librocache import SQLiteCacheConnector, CacheBridge
from libroosx.utils import fetch_timetable
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import (
NoSuchElementException,
TimeoutException,
ElementNotInteractableException,
ElementClickInterceptedException, StaleElementReferenceException,
)
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium import webdriver
import chromedriver_autoinstaller
import os
import yaml
import os
def resource_path(relative_path):
try:
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
config = yaml.safe_load(open("config.yml", encoding='utf-8'))
if platform.system() == 'windows':
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
chromedriver_autoinstaller.install(cwd=True)
driver_options = Options()
# driver_options.add_argument("start-minimized")
driver_options.add_experimental_option("useAutomationExtension", False)
driver_options.add_experimental_option("excludeSwitches", ["enable-automation"])
driver_options.add_argument("user-data-dir=./profile")
driver_options.add_argument("--disable-gpu")
close_msg = "Unable to evaluate script: disconnected: not connected to DevTools\n"
async def log(event):
event = str(event)
log_message = datetime.now().strftime("[%H:%M:%S] ") + event
print(log_message)
with open("bot.log", "a", encoding="utf-8") as log_file:
log_file.write(log_message + "\n")
async def teams_login(config):
if not config["logged_in"]:
driver = webdriver.Chrome(options=driver_options)
driver.get('https://teams.microsoft.com/')
WebDriverWait(driver, 50).until(
expected_conditions.presence_of_element_located(
(
By.XPATH,
'//*[@id="download-desktop-page"]/div/a',
)
)
).click()
print(
"Log in to your Microsoft Teams account"
)
driver.close()
await log("Logged in [Classroom]")
async def classroom_login(config):
if not config["logged_in"]:
driver = webdriver.Chrome(options=driver_options)
driver.get(
"https://accounts.google.com/o/oauth2/v2/auth/oauthchooseaccount?redirect_uri=https%3A%2F%2F"
"developers.google.com%2Foauthplayground&prompt=select_account&response_type=code&"
"client_id=407408718192"
".apps.googleusercontent.com&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fclassroom.announcements"
"&access_type=offline&flowName=GeneralOAuthFlow"
)
print(
"Log in to your google account"
)
WebDriverWait(driver, 50).until(
expected_conditions.presence_of_element_located(
(
By.XPATH,
'//*[@id="submit_deny_access"]/div/button/div[2]',
)
)
).click()
config["logged_in"] = True
with open("config.yml", "w", encoding="utf-8") as f:
yaml.dump(config, f, allow_unicode=True)
driver.close()
await log("Logged in [Classroom]")
async def join_meet(link, driver):
# Waiting for Camera Button to appear and joining
while True:
try:
if config["language"] == "pl":
mic_button = WebDriverWait(driver, 5).until(
expected_conditions.presence_of_element_located(
(
By.CSS_SELECTOR,
'div[aria-label="Wyłącz mikrofon (Ctrl + D)"]',
)
)
).click()
camera_button = WebDriverWait(driver, 5).until(
expected_conditions.presence_of_element_located(
(
By.CSS_SELECTOR,
'div[aria-label="Wyłącz kamerę (Ctrl + E)"]',
)
)
).click()
# Joining
join_btns = driver.find_elements_by_css_selector('[role="button"]')
while True:
for btn in join_btns:
if btn.text == 'Dołącz':
btn.click()
break
elif btn.text == 'Chcę dołączyć':
btn.click()
break
else:
await asyncio.sleep(0.5)
continue
break
elif config["language"] == "en":
mic_button = WebDriverWait(driver, 5).until(
expected_conditions.presence_of_element_located(
(
By.CSS_SELECTOR,
'div[aria-label="Turn off microphone (ctrl + d)"]',
)
)
).click()
camera_button = WebDriverWait(driver, 5).until(
expected_conditions.presence_of_element_located(
(
By.CSS_SELECTOR,
'div[aria-label="Turn off camera (ctrl + e)"]',
)
)
).click()
# Joining
join_btns = driver.find_elements_by_css_selector('div[role="button"]')
while True:
for btn in join_btns:
if btn.text == 'Join now':
btn.click()
break
elif btn.text == 'Ask to join':
btn.click()
break
else:
await asyncio.sleep(0.5)
continue
break
else:
continue
break
except (
NoSuchElementException,
ElementNotInteractableException,
ElementClickInterceptedException,
StaleElementReferenceException,
TimeoutException
):
driver.get(link)
try:
error_message = WebDriverWait(driver, 5).until(
expected_conditions.presence_of_element_located(
(
By.XPATH,
'//*[@id="yDmH0d"]/c-wiz/div/div[2]/div[1]',
)
)
).text
await log(error_message)
except TimeoutException:
pass
await log("Reloading")
return driver
async def join_classroom(classroom):
if "teams:" in classroom:
print("Teams")
link = classroom.replace('teams:', '')
driver = webdriver.Chrome(options=driver_options)
return driver
driver.get(link)
while True:
try:
join_button = WebDriverWait(driver, 5).until(
expected_conditions.presence_of_element_located(
(
By.CSS_SELECTOR,
'button[track-summary="Join an ongoing meetup from the channel ongoing meeting object"]',
)
)
).click()
mic_button = WebDriverWait(driver, 5).until(
expected_conditions.presence_of_element_located(
(
By.XPATH,
'//*[@id="preJoinAudioButton"]',
)
)
)
except TimeoutException:
print(mic_button.get_attribute("title"))
break
return True
# camera_button = WebDriverWait(driver, 5).until(
# expected_conditions.presence_of_element_located(
# (
# By.XPATH,
# '//*[@id="page-content-wrapper"]/div[1]/div/calling-pre-join-screen/div/div/div[2]/div[1]/div[2]/div/div/section/div[2]/toggle-button[1]',
# )
# )
# ).click()
# join_now_button = '//*[@id="page-content-wrapper"]/div[1]/div/calling-pre-join-screen/div/div/div[2]/div[1]/div[2]/div/div/section/div[1]/div/div/button'
elif "link:" in classroom:
print("Link")
link = classroom.replace('link:', '')
driver = webdriver.Chrome(options=driver_options)
await join_meet(link, driver)
return driver
else:
driver = webdriver.Chrome(options=driver_options)
driver.get("https://classroom.google.com")
while True:
try:
element = WebDriverWait(driver, 30).until(
expected_conditions.presence_of_element_located(
(By.PARTIAL_LINK_TEXT, classroom)
)
)
await log(f"Class {classroom} found")
element.click()
break
except TimeoutException:
await log(f"Class {classroom} not found")
# Getting Link
while True:
try:
link = (
WebDriverWait(driver, 10)
.until(
expected_conditions.presence_of_element_located(
(
By.PARTIAL_LINK_TEXT,
"https://meet.google.com/"
)
)
)
).text
await log(f"Link found: {link}")
break
except TimeoutException:
await log("Link not found")
await join_meet(link, driver)
return driver
async def leave_class(driver):
await log("Class ended")
driver.close()
async def librus_login():
config = yaml.safe_load(open("config.yml", encoding="utf-8"))
user = await keychain_proxy_login(
config["librus"]["email"], config["librus"]["password"]
)
return SynAClient(user, cache=CacheBridge(SQLiteCacheConnector(":memory:")))
# --------------------------------------------------------------------------
# Functions end
# --------------------------------------------------------------------------
if __name__ == '__main__':
async def main():
# await bot.wait_until_ready()
try:
os.rename("bot.log", "bot.old.log")
except WindowsError:
try:
os.remove("bot.old.log")
os.rename("bot.log", "bot.old.log")
except FileNotFoundError:
pass
await log("Started Bot")
while True:
# try:
# First time setup
# If you want to log in again change logged_in to False in the config.yml
config = yaml.safe_load(open("config.yml", encoding="utf-8"))
client = await librus_login()
if not config["logged_in"]:
await classroom_login(config)
if config["classroom"] is None:
subjects = {}
a = client
for i in a:
subjects[str(i)] = ""
config["classroom"] = subjects
for i in config:
print(i)
with open("config.yml", "w", encoding="utf-8") as f:
yaml.dump(config, f, allow_unicode=True)
print("set the classroom names manually")
exit()
active_lesson_old = None
lesson_running = False
while True:
tt = None
i = 0
today_datetime = datetime.now()
today_date = today_datetime.date()
today_weekday = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'][
today_date.weekday()]
config = yaml.safe_load(open("config.yml", encoding="utf-8"))
try:
tt_overrides = []
for override in config['overrides'][today_weekday]:
override_hour_start = datetime.strptime(str(override['hour_start']), '%H:%M').time()
override_hour_end = datetime.strptime(str(override['hour_end']), '%H:%M').time()
tt_overrides.append((override_hour_start, override_hour_end, override['lesson']))
active_lesson = tuple(
filter(
lambda x: x[1] > today_datetime.time() > x[0],
tt_overrides,
)
)[0][2]
except (AttributeError, IndexError, TypeError):
pass
active_lesson = None
if active_lesson is None or active_lesson == ():
while tt is None and i <= 1:
try:
tt = await fetch_timetable(client)
active_lesson = tuple(
filter(
lambda x: x.hour_end > today_datetime.time() > x.hour_start,
tt.auto.get(today_date.strftime('%Y-%m-%d')),
)
)[0].subject.name
i = +1
except (AttributeError, IndexError, CloseError):
pass
active_lesson = None
if lesson_running:
try:
if driver.get_log('driver')[-1]['message'] == close_msg:
print('Browser window closed by user')
lesson_running = False
except (IndexError, TypeError):
pass
except UnboundLocalError:
print('Browser window closed by user')
lesson_running = False
if lesson_running:
try:
member_count = int(
WebDriverWait(driver, 5).until(
expected_conditions.presence_of_element_located(
(
By.XPATH,
'//*[@id="ow3"]/div[1]/div/div[9]/div[3]/div[1]/div[3]/div/div[2]/div[1]/span/span/div/div/span[2]',
)
)
).text
)
if member_count > highest_member_count:
highest_member_count = member_count
if highest_member_count * 0.75 > member_count:
await leave_class(driver)
lesson_running = False
print(
f"{member_count} people on the lesson, {highest_member_count} was the maximum, {highest_member_count * 0.75} at most are required to leave")
except (
selenium.common.exceptions.TimeoutException,
ValueError,
UnboundLocalError,
AttributeError
):
pass
if active_lesson != active_lesson_old and active_lesson != () and active_lesson is not None and not lesson_running:
await log(f"current:{active_lesson} old:{active_lesson_old}")
await log(f"{active_lesson} is the current lesson")
if config["classroom"][active_lesson] != "":
if active_lesson != 'cancelled':
# This code runs if the lesson has changed
# Starting classroom
lesson_running = True
threading.Thread(target=playsound, args=(resource_path('notification.mp3'),),
daemon=True).start()
try:
driver = await join_classroom(config["classroom"][active_lesson])
except Exception as e:
await log(e)
highest_member_count = 0
else:
await log("Lesson cancelled")
else:
await log("It doesn't have a name/link in the config!")
if active_lesson is not None:
active_lesson_old = active_lesson
await asyncio.sleep(config["wait_time"])
# except Exception as e:
# await log(e)
asyncio.run(main())