/
epresence.py
478 lines (356 loc) · 12.2 KB
/
epresence.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# coding: utf-8
#
# notes:
# user/pass are hardcoded!
# my id = 30 is hardcoded!
# me is always set to H323
# todo: login is only for external account! login via shibboleth if in sso_domain ...
my_email = 'XXX@YYY.ZZZ'
my_password = '********'
def start_session():
"""
Starts a session with e:presence
user/pass and filters are hardcoded
return a session and the token
"""
import requests
from bs4 import BeautifulSoup
import json
LOGIN_URL = "https://new.epresence.grnet.gr/auth/login"
with requests.Session() as s:
# Read the source
r = s.get(LOGIN_URL)
soup = BeautifulSoup(r.text, 'html5lib')
# Find the token
token = soup.find("input", {"name":"_token"} )["value"]
authinfo = {
'email': my_email,
'password': my_password,
'_token': token
}
# Login:
rl = s.post(LOGIN_URL, data=authinfo)
return s, token
def fetch_my_id():
s, _ = start_session()
e, _ = my_id = check_emails([my_email], s)
my_id = e[my_email]
return my_id
def read_emails():
"""
read multiple lines, keep only emails
terminate with .
returns a list of emails
"""
import re
lines = []
print("Give the emails: (end with . on a single line)")
while True:
line = input()
line = line.strip()
if ( line == '.' ):
break
else:
fields = line.split(',')
for field in fields:
if( re.findall(".*@.*", field) ):
lines.append(field.strip())
return lines
def get_sso_domains():
"""
return a list of shibbolethized domains (stripped to 2nd level domains)
"""
import requests
import re
import os
from datetime import datetime
URL = "https://wayf.grnet.gr/"
OL_FILE = 'internal_domains'
with requests.Session() as s:
# Read the source
try:
r = s.get(URL)
r.raise_for_status()
domains = [ ".".join([d for d in r.split('.')[-2:]]) for r in re.findall('.*option value=.*//([^\/]+)/.*', r.text) ]
# save an offline backup
f = open(OL_FILE, "w")
for domain in domains:
f.write(domain+'\n')
f.close()
except Exception as err:
print(err)
print('Falling back to the backup file from ' + str(datetime.fromtimestamp(os.stat(OL_FILE).st_ctime)))
f = open(OL_FILE)
domains = f.read().splitlines()
f.close()
return domains
def fetch_conf_list():
"""
Fetch all conferences
return them in json format
for use with ical exporter
"""
from bs4 import BeautifulSoup
import json
CONFS_URL = "https://new.epresence.grnet.gr/conferences/all?&limit=1000"
s, _ = start_session()
# Now fetch the data
rc = s.get(CONFS_URL)
soup = BeautifulSoup(rc.text, 'html5lib')
data = []
table = soup.find('table', attrs={'id':'conferenceTable'})
table_body = table.find('tbody')
rows = table_body.find_all('tr')
cellnames = [ "cellID", "cellDesc", "cellStartDate", "cellStartTime", "cellEndTime" ]
for row in rows:
d = {}
for cl in cellnames:
td = row.find('td', attrs={"class":cl})
if td:
d[cl] = td.text.strip()
if d:
data.append(d)
return json.dumps(data)
def fetch_future_confs():
"""
fetch future conference ids
return a list of future conference ids
for use with send_conf_emails for example
"""
from bs4 import BeautifulSoup
import json
CONFS_URL = "https://new.epresence.grnet.gr/conferences"
s, _ = start_session()
# Now fetch the data
rc = s.get(CONFS_URL)
soup = BeautifulSoup(rc.text, 'html5lib')
data = []
table = soup.find('table', attrs={'class':'futureConferences'})
for a in table.find_all('a'):
href = a['href'].strip().split('/')[2]
data.append(href)
return data
def check_emails(emails, s):
"""
find if given emails are present
emails is a list
s is the session (must be supplied, use start_session() e.g.)
return a dict of { email: id } and a list of missing emails (you can feed it to add_missing_users() )
"""
import re
URL = "https://new.epresence.grnet.gr/conferences/conferenceAddUserEmail"
userids = {}
missing = []
for email_item in emails:
email = email_item.strip()
data = {
'email': email,
'page': "1"
}
r = s.post(URL, data)
if (len(r.json())==0):
missing.append(email)
else:
# find the id:
SEARCH_USER_URL = "https://new.epresence.grnet.gr/conferences/requestParticipant/"+email
r = s.get(SEARCH_USER_URL)
user_id = re.findall('assignUserID\(([0-9]+)\)', r.text, re.M)[0]
userids[email] = user_id
return userids, missing
def add_missing_users(emails, s, token, conf_id):
"""
add the missing users to the epresence system
checks whether they are internal or external based on their domain
emails is a list, e.g returned value missing from check_emails()
"""
ADD_MISSING_USER_URL='https://new.epresence.grnet.gr/users'
ADD_MISSING_USER_DATA = {
'_token' : token,
'lastname' : '',
'firstname' : '',
'telephone' : '',
'institution_id' : '',
'department_id' : '',
'new_institution' : '',
'new_department' : '',
'status' : '1',
'creator_id' : '30',
'conference_id' : conf_id,
'specialUser' : 'conferenceUser',
'role' : 'EndUser',
'from' : 'https://new.epresence.grnet.gr/conferences/'+str(conf_id)+'/edit',
'conferenceAddNewUser' : 'Αποθήκευση+χρήστη+και+εισαγωγή+στους+συμμετέχοντες',
}
sso_domains = get_sso_domains()
for email in emails:
ADD_MISSING_USER_DATA['email'] = email
domain = '.'.join(d for d in email.split('@')[1].split('.')[-2:])
if domain.lower() in sso_domains:
ADD_MISSING_USER_DATA['state'] = 'sso'
else:
ADD_MISSING_USER_DATA['state'] = 'local'
print(email, ADD_MISSING_USER_DATA['state'])
response = s.post(ADD_MISSING_USER_URL, ADD_MISSING_USER_DATA)
print(response)
def add_conference(confname, start_date, start_time, end_time, emails='', early=False):
"""
Adds a new conference
All parameters are mandatory except emails
date format: dd-mm-yyyy
time format: HH:mm
end date is always start date
adds myself by default (as H323)
early=True to start 15 minutes earlier
"""
from datetime import datetime, timedelta
s, token = start_session()
NEW_CONF_URL = "https://new.epresence.grnet.gr/conferences"
HEADERS = {'Referer': 'https://new.epresence.grnet.gr/conferences/create'}
print(confname)
# read participant emails from stdin
emails = read_emails()
if ( len(emails) > 0 ):
users_no = len(emails)
else:
users_no = 3
if(early):
start_datetime = datetime.strptime(start_time, "%H:%M")
start_datetime_earlier = start_datetime - timedelta(minutes=15)
start_time_earlier = str(start_datetime_earlier.hour)+":"+str(start_datetime_earlier.minute)
start_time = start_time_earlier
postdata = {
'_token' : token,
'users_no' : users_no,
'users_h323' : '1',
'users_vidyo_room': '0',
'title' : confname,
'institution_id' : '7',
'department_id' : '772',
'start_date' : start_date,
'start_time' : start_time,
'end_date' : start_date,
'end_time' : end_time,
'invisible' : '0',
'desc' : '',
'files' : '',
'descEn' : '',
'files' : '',
'max_users' : '15',
'max_h323' : '10',
'max_vidyo_room' : '5',
'max_duration' : '360'
}
r = s.post(NEW_CONF_URL, data=postdata, headers=HEADERS)
conf_id = r.url.split('/')[4]
# add me by default
ADD_USER_URL='https://new.epresence.grnet.gr/conferences/assign_participant'
ADD_USER_DATA = {
'user_id': 30,
'conference_id': conf_id,
}
ru = s.post(ADD_USER_URL, ADD_USER_DATA)
# change me to h323
DEVICECHANGE_USER_URL='https://new.epresence.grnet.gr/conferences/userConferenceDeviceAssign'
ADD_USER_DATA['device'] = "H323"
ru = s.post(DEVICECHANGE_USER_URL, ADD_USER_DATA)
# now add requested emails too:
if ( len(emails) > 0 ):
userdict, missinglist = check_emails(emails, s)
if ( len(userdict) > 0 ):
for user_id in userdict.values():
ADD_USER_DATA = {
'user_id': user_id,
'conference_id': conf_id,
}
ru = s.post(ADD_USER_URL, ADD_USER_DATA)
print("ConfID: "+conf_id)
print("Added: "+str(list(userdict.keys())))
if (len(missinglist) > 0):
print("Missing emails: "+ str(missinglist))
print("adding them...")
add_missing_users(missinglist, s, token, conf_id)
return r
def json2ical(sdata):
"""
Convert epresence conferences
sdata: conference list in json format (returned from fetch_conf_list)
returns a string, the iCal, to be written to a file e.g.
"""
import json
from icalendar import Calendar, Event, Alarm
from datetime import datetime
import pytz
data = json.loads(sdata)
if data:
cal = Calendar()
cal.add('prodid', '-//Gamisterous epresence ical generator//lala.la//')
cal.add('version', '3.0')
cal.add('X-WR-CALNAME', 'Τηλεδιασκέψεις')
local = pytz.timezone("Europe/Athens")
for dato in data:
confid=dato["cellID"]
desc=dato["cellDesc"]
start=datetime.strptime(dato["cellStartDate"]+"-"+dato["cellStartTime"],"%d-%m-%Y-%H:%M")
end=datetime.strptime(dato["cellStartDate"]+"-"+dato["cellEndTime"],"%d-%m-%Y-%H:%M")
now = datetime.now()
event = Event()
event.add('SUMMARY', desc)
event.add('DTSTART', local.localize(start))
event.add('DURATION', end-start)
event.add('DTEND', local.localize(end))
event.add('DTSTAMP', datetime.now())
event.add('URL', 'https://new.epresence.grnet.gr')
event['UID'] = "conf"+confid+"@conferences"
# add a reminder 30m earlier
alarm = Alarm()
alarm.add("action", "DISPLAY")
alarm.add('description', "Reminder")
alarm.add("TRIGGER;RELATED=START", "-PT30M")
event.add_component(alarm)
cal.add_component(event)
return cal.to_ical()
def confs2ical():
confs = fetch_conf_list()
ical = json2ical(confs)
f = open('confs.ics', 'wb')
f.write(ical)
f.close()
def fetch_participant_ids(s, conf_id):
"""
gets the participants ids needed to send the invitation mails
returns a list of the ids
"""
from bs4 import BeautifulSoup
CONF_URL = "https://new.epresence.grnet.gr/conferences/"+str(conf_id)+"/edit"
# Now fetch the data
rc = s.get(CONF_URL)
soup = BeautifulSoup(rc.text, 'html5lib')
data = []
table = soup.find('table', attrs={'id':'participantsTable'})
table_body = table.find('tbody')
inputs = table_body.find_all('input', attrs={'class':'check'})
for i in inputs:
data.append(i.get("value"))
return data
def send_conf_emails(conf_id):
"""
sends the invitation mails for the given conference id
"""
SEND_EMAIL_URL='https://new.epresence.grnet.gr/conferences/sendParticipantEmail'
s, token = start_session()
SEND_EMAIL_DATA = {
'_token' : token,
'conference_id' : conf_id,
}
ids = fetch_participant_ids(s, conf_id)
for userid in ids:
SEND_EMAIL_DATA["participants["+str(userid)+"]"] = userid
print(SEND_EMAIL_DATA)
r = s.post(SEND_EMAIL_URL, data=SEND_EMAIL_DATA)
print(r)
def main():
print("Begin")
#add_conference('test conference name', '25-06-2018', '14:00', '15:30')
confs2ical()
if ( __name__ == "__main__" ):
main()