-
Notifications
You must be signed in to change notification settings - Fork 1
/
Chellz_IITM_Bunker_v1.py
360 lines (311 loc) · 12.8 KB
/
Chellz_IITM_Bunker_v1.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
# Chellz Mini Project - IITM BUNKER
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
import time
import datetime
import logging
import os
# set logger configuration
logging.basicConfig(filename='history.log', level=logging.DEBUG,
format='%(asctime)s:%(levelname)s:%(message)s'
)
# constants
# use chrome browser
CHROME_PATH = os.path.join(os.getcwd(), 'chromedriver')
MEET_URL = 'https://meet.google.com/'
# Specify start and start time to open and end the meet
# START_TIME = '13:00'
# END_TIME = '13:47'
def start_end_datetime(start_time, end_time):
# return start and end time in datetime format
current_datetime = datetime.datetime.today()
current_date = current_datetime.strftime('%d-%B-%Y')
start_datetime = current_date+'-'+start_time
end_datetime = current_date+'-'+end_time
start_dt = datetime.datetime.strptime(start_datetime, '%d-%B-%Y-%H:%M')
end_dt = datetime.datetime.strptime(end_datetime, '%d-%B-%Y-%H:%M')
return start_dt, end_dt
# data-strcture for the data of each class
class google_meet_class():
def __init__(self, start_time, end_time, username, password, meet_link):
self.start_time, self.end_time = start_end_datetime(
start_time, end_time)
self.call_type = 'meet'
self.username = username
self.password = password
self.meet_link = meet_link
class webex_class():
def __init__(self, start_time, end_time, username, email_id, meet_link):
self.start_time, self.end_time = start_end_datetime(
start_time, end_time)
self.call_type = 'webex'
self.username = username
self.email_id = email_id
self.meet_link = meet_link
# # use firefox browser
# FIREFOX_PATH = '/home/chella/Desktop/Coding/Python Mini Modules/selenium/firefoxdriver/geckodriver'
# browser = webdriver.Firefox(executable_path=FIREFOX_PATH)
def open_browser():
# set notification pop up settings in chrome browser
# allow camera and mic in pop ups
opt = Options()
opt.add_argument("--disable-infobars")
# opt.add_argument("start-maximized")
# opt.add_argument('--headless')
opt.add_argument("--disable-extensions")
opt.add_argument("--mute-audio")
# Pass the argument 1 to allow and 2 to block
opt.add_experimental_option("prefs", {
"profile.default_content_setting_values.media_stream_mic": 1,
"profile.default_content_setting_values.media_stream_camera": 1,
"profile.default_content_setting_values.notifications": 2
})
browser = webdriver.Chrome(options=opt, executable_path=CHROME_PATH)
return browser
def google_meet(browser, start_datetime, end_datetime, username, user_password, meet_link):
# Google meet stuff
browser.get(MEET_URL)
# click on the sign in button
try:
sign_in = WebDriverWait(browser, 20).until(
EC.presence_of_element_located(
(By.LINK_TEXT, "Sign in"))
)
sign_in.click()
finally:
logging.info('clicking sign in executing...')
# enter signin emailid and move to next page
try:
mail_id = WebDriverWait(browser, 20).until(
EC.presence_of_element_located(
(By.ID, "identifierId"))
)
mail_id.send_keys(username)
mail_id.send_keys(Keys.RETURN)
finally:
logging.info('username filling executing...')
time.sleep(5)
# enter password and move to next page
try:
password = WebDriverWait(browser, 20).until(
EC.presence_of_element_located(
(By.NAME, "password"))
)
password.send_keys(user_password)
password.send_keys(Keys.RETURN)
finally:
logging.info('password filling executing...')
# open tab
# https://python-forum.io/Thread-Need-Help-Opening-A-New-Tab-in-Selenium
browser.execute_script("window.open('');")
# implicit wait
time.sleep(10)
# swith browser object to new window
browser.switch_to.window(browser.window_handles[1])
# open the google meet link in this new tab we created
browser.get('https://' + meet_link)
time.sleep(10)
# mute my mic
try:
mic = WebDriverWait(browser, 20).until(
EC.presence_of_element_located(
(By.CLASS_NAME, "DPvwYc.JnDFsc.dMzo5"))
)
mic.click()
finally:
logging.info('mute is executing...')
# block the camera
try:
camera = WebDriverWait(browser, 20).until(
EC.presence_of_element_located(
(By.CLASS_NAME, "GOH7Zb"))
)
camera.click()
finally:
logging.info('camera blocking is executing...')
# join the meeting finally
try:
join = WebDriverWait(browser, 20).until(
EC.presence_of_element_located(
(By.CLASS_NAME, "NPEfkd.RveJvd.snByac"))
)
join.click()
finally:
logging.info('join is executing...')
# print confidence
print('google meet class is successfully logged in')
logging.info('google meet class is successfully logged in')
# wait till the class ends
time_delta = end_datetime - datetime.datetime.now()
time.sleep(time_delta.seconds)
# close the meet
try:
hang_up = WebDriverWait(browser, 20).until(
EC.presence_of_element_located(
(By.CLASS_NAME, "s1GInc.zCbbgf"))
)
hang_up.click()
finally:
logging.info('hang up is executing...')
# quit the browser
browser.quit()
def webex_meet(browser, start_datetime, end_datetime, username, email_id, meet_link):
# Webex stuff
browser.get(meet_link)
# click join-meeting button, ID: smartJoinButton
try:
join_meeting_btn = WebDriverWait(browser, 20).until(
EC.presence_of_element_located(
(By.ID, "smartJoinButton"))
)
join_meeting_btn.click()
finally:
logging.info('clicking join meeting button is executing...')
# wait for 5 seconds for next page to load
time.sleep(5)
# fill in username and smail id, CLASS: style-input-2nuAk undefined
# Here is the tricky part, webex clever guys tried to hide every html important elements into some shit called iframe tags
# we need to first navigate to the iframe and then search for the element
# browser.switch_to.frame(
# "pbui_iframe")
WebDriverWait(browser, 10).until(
EC.frame_to_be_available_and_switch_to_it((
By.ID, "pbui_iframe")))
try:
form_inputs = WebDriverWait(browser, 20).until(
EC.presence_of_all_elements_located(
(By.CLASS_NAME, "style-input-2nuAk.undefined"))
)
form_inputs[0].send_keys(username)
form_inputs[1].send_keys(email_id)
finally:
logging.info('username and email id filling is executing...')
# click next button, ID: guest_next-btn
try:
next_btn = WebDriverWait(browser, 20).until(
EC.presence_of_element_located(
(By.ID, "guest_next-btn"))
)
next_btn.click()
finally:
logging.info('clicking next button is executing...')
# wait for 10 seconds for next page to load
time.sleep(10)
# when we move on to the next task, We need to come out of the iframe and go to the main frame back
browser.switch_to.default_content()
# Again webex clever guys tried to hide inportant html elements in this page inside iframe
# so we need to navigate to the appropriate iframe again
# browser.switch_to.frame(
# "pbui_iframe")
WebDriverWait(browser, 10).until(
EC.frame_to_be_available_and_switch_to_it((
By.ID, "pbui_iframe")))
# Mute and off the camera
try:
mic_cam_btns = WebDriverWait(browser, 20).until(
EC.presence_of_all_elements_located(
(By.CLASS_NAME, "style-text-vqI8d"))
)
mic_cam_btns[0].click()
mic_cam_btns[1].click()
finally:
logging.info('clicking mute and off camera button is executing...')
# click join meeting button, ID: interstitial_join_btn
try:
join_btn = WebDriverWait(browser, 20).until(
EC.presence_of_element_located(
(By.ID, "interstitial_join_btn"))
)
join_btn.click()
finally:
logging.info('clicking final join button is executing...')
# print confidence
print('webex class is successfully logged in')
logging.info('webex class is successfully logged in')
# wait till the class ends
time_delta = end_datetime - datetime.datetime.now()
time.sleep(time_delta.seconds)
# quit the browser
browser.quit()
# Main Part
n = int(input('Total number of classes today = '))
classes = []
for slot in range(n):
print('Answer the following questions for class'+' '+str(slot+1))
class_mode = int(
input('press 1 for google-meet, press 2 for webex-meet = '))
if class_mode == 1:
start_time = input(
'Give start time of this class in 24hr format, eg., 13:00. UR INPUT = ')
end_time = input(
'Give end time of this class in 24hr format, eg., 13:50. UR INPUT = ')
username = input('Provide your smail id = ')
password = input('Provide your smail id password = ')
meet_link = input('Provide the full google meet link = ')
classes.append(google_meet_class(
start_time, end_time, username, password, meet_link))
elif class_mode == 2:
start_time = input(
'Give start time of this class in 24hr format, eg., 13:00. UR INPUT = ')
end_time = input(
'Give end time of this class in 24hr format, eg., 13:50. UR INPUT = ')
username = input(
'Provide your name and rollnumber - format("Ram ME17B000") = ')
email_id = input('Provide your smail id = ')
meet_link = input('Provide the full webex meet link = ')
classes.append(webex_class(start_time, end_time,
username, email_id, meet_link))
else:
print('Wrong Class Mode')
print('do it again')
for i in range(len(classes)):
# find the time deltas for all classes
time_deltas = []
current_datetime = datetime.datetime.now()
for slot in classes:
time_deltas.append(slot.start_time - current_datetime)
# sleep till first class starts
first_class_index = time_deltas.index(min(time_deltas))
print('We will now wait for ' +
str(time_deltas[first_class_index].seconds)+'seconds for the next class')
logging.info('We will now wait for ' +
str(time_deltas[first_class_index].seconds)+'seconds for the next class')
time.sleep(time_deltas[first_class_index].seconds)
# execute the web automation part
class_now = classes[first_class_index]
while True:
current_datetime = datetime.datetime.now()
if (current_datetime >= class_now.start_time) and ((current_datetime-class_now.start_time).seconds <= 420):
try:
browser = open_browser()
# minimize the browser
browser.minimize_window()
except Exception as e:
logging.error('Error with browser opening: '+str(e))
logging.warning('browser opening failed - trying again')
print('browser opening failed - trying again')
else:
logging.info('open browser code is successfully executed')
try:
if class_now.call_type == 'meet':
google_meet(browser, class_now.start_time, class_now.end_time,
class_now.username, class_now.password, class_now.meet_link)
elif class_now.call_type == 'webex':
webex_meet(browser, class_now.start_time, class_now.end_time,
class_now.username, class_now.email_id, class_now.meet_link)
except Exception as e:
logging.error('Error with browser navigations: '+str(e))
browser.quit()
print('online class is unable to open - trying again')
logging.warning(
'online class is unable to open - trying again')
else:
print('class '+str(i+1)+' with slot ' +
str(class_now.start_time.strftime('%H:%M')) + ' is successfully bunked')
logging.info('class '+str(i+1)+' with slot ' +
str(class_now.start_time.strftime('%H:%M')) + ' is successfully bunked')
break