-
Notifications
You must be signed in to change notification settings - Fork 22
/
autoJoin.py
253 lines (214 loc) · 8.79 KB
/
autoJoin.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
import sys
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from time import sleep
from datetime import datetime
import json
from twilio.rest import Client
check = 2 # variable used for sending msg
meeting_name = "meeting_name"
try:
with open('config.json', 'r') as f:
config = json.load(f)
except:
print('config file not found. Exiting')
sys.exit(1)
client = Client(config['account_sid'], config['auth_token'])
TEAMS_URL = 'https://teams.microsoft.com/_#/calendarv2'
sleepDelay = 2 # increase if you have a slow internet connection
timeOutDelay = 30 # increase if you have a slow internet connection
curParticipants = 0
minParticipants = 10
def get_driver_options():
opt = Options()
if 'headless' in config and config['headless']:
opt.add_argument("--headless")
opt.add_argument("--window-size=1920,1080")
opt.add_argument("--disable-infobars")
opt.add_argument("start-maximized")
opt.add_argument("--disable-extensions")
# 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": 1
})
return opt
browser = webdriver.Chrome(ChromeDriverManager().install(), options=get_driver_options())
def wait_and_find_ele_by_id(html_id, timeout=timeOutDelay):
sleep(sleepDelay)
for i in range(timeout):
try:
ele = browser.find_element_by_id(html_id)
except:
sleep(sleepDelay)
else:
return ele
def wait_and_find_ele_by_link_text(text, timeout=timeOutDelay):
sleep(sleepDelay)
for i in range(timeout):
try:
ele = browser.find_element_by_link_text(text)
except:
sleep(sleepDelay)
else:
return ele
def wait_and_find_element_by_xpath(xpath, timeout=timeOutDelay):
sleep(sleepDelay)
for i in range(timeout):
try:
ele = browser.find_element_by_xpath(xpath)
except:
sleep(sleepDelay)
else:
return ele
def wait_and_find_elements_by_xpath(xpath, timeout=timeOutDelay):
sleep(sleepDelay)
for i in range(timeout):
try:
ele = browser.find_elements_by_xpath(xpath)
except:
sleep(sleepDelay)
else:
return ele
def login():
browser.get(TEAMS_URL) # open calendar tab in teams
sleep(sleepDelay)
wait_and_find_ele_by_id('i0116').send_keys(config['username']) # enter username
wait_and_find_ele_by_id('idSIButton9').click() # click next
wait_and_find_ele_by_id('i0118').send_keys(config['password']) # enter password
wait_and_find_ele_by_id('idSIButton9').click() # click next
wait_and_find_ele_by_id('idSIButton9').click() # click yes to stay signed in
web_ele = wait_and_find_ele_by_link_text('Use the web app instead', 5)
if web_ele is not None:
web_ele.click()
def check_and_join_meeting():
global curParticipants
joins = wait_and_find_elements_by_xpath('//button[.="Join"]', 3)
if len(joins) == 0: # no meeting scheduled
return
joins[-1].click() # join the latest meeting scheduled i.e if join buttons for 9, 10 A.M available, will join 10 A.M
elem = wait_and_find_element_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]/div/button')
if elem.get_attribute('aria-pressed') == 'true': # turn off camera
elem.click()
elem = wait_and_find_element_by_xpath('//*[@id="preJoinAudioButton"]/div/button')
if elem.get_attribute('aria-pressed') == 'true': # turn off microphone
elem.click()
wait_and_find_element_by_xpath('//button[.="Join now"]').click() # join meeting
if(config['use_twilio']==True):
global check
global meeting_name
meeting_name=browser.title
check = 0
try:
client.messages.create(
to=config['your_no'],
from_=config['twilio_no'],
body=f"Hi {config['nickname']}, I joined the meeting named {meeting_name} at {datetime.now()}. Hope you're doing well"
)
except:
print('Twilio service failed')
print('Joined the meeting at {}'.format(datetime.now()))
sleep(60 * 5)
browser.execute_script("document.getElementById('roster-button').click()")
sleep(sleepDelay)
num_str = wait_and_find_elements_by_xpath(
'//span[@class="toggle-number"][@ng-if="::ctrl.enableRosterParticipantsLimit"]')
if len(num_str) >= 2:
if num_str[1].text[1:-1] != '':
curParticipants = int(num_str[1].text[1:-1])
else:
browser.execute_script("document.getElementById('roster-button').click()")
def check_and_end_or_leave_or_join_meeting():
global curParticipants, minParticipants
hangup_btn = wait_and_find_element_by_xpath('//button[@id="hangup-button"]', 2)
if hangup_btn is not None: # currently in meeting
num_str = wait_and_find_elements_by_xpath(
'//span[@class="toggle-number"][@ng-if="::ctrl.enableRosterParticipantsLimit"]')
if len(num_str) >= 2:
if num_str[1].text[1:-1] != '':
curParticipants = int(num_str[1].text[1:-1])
else:
browser.execute_script("document.getElementById('roster-button').click()")
if curParticipants <= minParticipants and curParticipants != 0: # leaves meeting for given condition
if(config['use_twilio']==True):
#twilio message Sending
try:
client.messages.create(
to=config['your_no'],
from_=config['twilio_no'],
body=f"Hi {config['nickname']}, I left the meeting named {meeting_name} as attendees were less than {config['minimumParticipants']} at {datetime.now()}"
)
except:
print('Twilio service failed')
browser.execute_script("document.getElementById('hangup-button').click()")
print('Left meeting at {}'.format(datetime.now()))
browser.get(TEAMS_URL) # open calendar tab
browser.refresh()
sleep(5)
else:
return
else:
curParticipants = 0
#twilio message Sending
global check
if (check==0 and config['use_twilio']==True):
check=1
try:
client.messages.create(
to=config['your_no'],
from_=config['twilio_no'],
body=f"Hi {config['nickname']}, host ended the meeting named {meeting_name} at {datetime.now()}"
)
except:
print('Twilio service failed')
browser.get(TEAMS_URL)
browser.refresh()
sleep(5)
check_and_join_meeting()
def init():
global minParticipants
minParticipants = config['minimumParticipants']
browser.get(TEAMS_URL) # open calendar tab in teams
sleep(sleepDelay)
while wait_and_find_element_by_xpath('//button[@title="Switch your calendar view"]') is None:
sleep(5) # wait for calendar tab to completely load
while wait_and_find_element_by_xpath('//button[@title="Switch your calendar view"]').get_attribute('name') != "Day":
wait_and_find_element_by_xpath('//button[@title="Switch your calendar view"]').click()
wait_and_find_element_by_xpath('//button[@name="Day"]').click() # change calender work-week view to day view
if(config['use_twilio']==True):
#twilio message Sending
try:
client.messages.create(
to=config['your_no'],
from_=config['twilio_no'],
body=f"Hi {config['nickname']}, we finished initialization at {datetime.now()}"
)
except:
print('Twilio service failed')
print('Initialized Successfully at {}'.format(datetime.now()))
check_and_join_meeting()
def main():
global browser
try:
login()
except:
print('Login failed. Please try again')
sys.exit(1)
try:
init()
except:
print('Failed to open calendar')
sys.exit(1)
while True:
try:
check_and_end_or_leave_or_join_meeting()
except:
print('join meeting failed, trying again')
browser.get(TEAMS_URL) # open calendar tab in teams
else:
sleep(10)
if __name__ == "__main__":
main()