-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcozoomus.py
192 lines (170 loc) · 8.61 KB
/
cozoomus.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
import os
import logging
import json
from zoomus import ZoomClient
from datetime import datetime, timezone
class NotSyncedError(Exception):
pass
def user_update_type(client, user, user_type):
user_data = {
"id": user['id'],
"type": user_type
}
response = client.user.update(**user_data)
if response.status_code == 204:
return True
else:
logging.error("ERROR: cannot assign/unassign the license. More info: {}".format(response.text))
return False
def is_meeting_soon(meeting):
try:
meeting_id = meeting['id']
except:
meeting_id = meeting['occurrence_id']
try:
start_time = datetime.strptime(meeting['start_time'], '%Y-%m-%dT%H:%M:%SZ')
except:
raise ValueError('Invalid meeting start_time: %s' % meeting['start_time'])
logging.debug('is_meeting_soon({}) :: start_time = {}'.format(meeting_id, start_time))
#print('test: {}'.format(utc_to_local(start_time).strftime("%H:%M:%S")))
wrong_years = [1980, 2099]
if start_time.year in wrong_years:
raise NotSyncedError('Meeting not properly synced from Google Calendar')
now = datetime.now()
delta = abs(start_time - now).total_seconds() // 3600
logging.debug('is_meeting_soon({}) :: delta = {} hours'.format(meeting_id, delta))
# Match meetings before and after TIME_DELTA
# TODO: allow using diffent deltas (TIME_DELTA_BEFORE & TIME_DELTA_AFTER)
if delta < TIME_DELTA:
return True
else:
return False
def utc_to_local(utc_dt):
return utc_dt.replace(tzinfo=timezone.utc).astimezone(tz=None)
if __name__ == "__main__":
# Set log level
loglevel = os.getenv('LOGLEVEL', 'warning')
numeric_loglevel = getattr(logging, loglevel.upper(), None)
if not isinstance(numeric_loglevel, int):
raise ValueError('Invalid log level: %s' % loglevel)
logging.basicConfig(level=numeric_loglevel)
logging.info("SETTINGS :: LOGLEVEL = {}".format(numeric_loglevel))
# Required environment variables
if "ZOOM_ACCOUNT_ID" not in os.environ or "ZOOM_CLIENT_ID" not in os.environ or "ZOOM_CLIENT_SECRET" not in os.environ:
logging.critical("Cannot find required environment variables")
print("[ERROR] Cannot find required environment variables:")
print(" - ZOOM_ACCOUNT_ID")
print(" - ZOOM_CLIENT_ID")
print(" - ZOOM_CLIENT_SECRET")
exit()
ACCOUNT_ID = os.getenv('ZOOM_ACCOUNT_ID')
CLIENT_ID = os.getenv('ZOOM_CLIENT_ID')
CLIENT_SECRET = os.getenv('ZOOM_CLIENT_SECRET')
# Time delta (in hours)
# TODO: allow using diffent deltas (TIME_DELTA_BEFORE & TIME_DELTA_AFTER)
TIME_DELTA = int(os.getenv('ZOOM_TIME_DELTA', default=24))
logging.info("SETTINGS :: TIME_DELTA = {} (hours)".format(TIME_DELTA))
# Available licenses
LICENSES = int(os.getenv('ZOOM_LICENSES', default=20))
logging.info("SETTINGS :: LICENSES = {}".format(LICENSES))
# The following users will always keep their license
WHITELISTED_USERS = list(os.getenv('ZOOM_WHITELISTED_USERS', default="").strip('"').split(" "))
logging.info("SETTINGS :: WHITELISTED_USERS = {}".format(WHITELISTED_USERS))
# Ignore recurrent meetings without time: do not assign licenses if the meeting is a recurrent meeting without time
IGNORE_RECURRENT_WITHOUT_TIME = int(os.getenv('IGNORE_RECURRENT_WITHOUT_TIME', default=True))
# Ignore recurrent meetings with time: do not assign licenses if the meeting is a recurrent meeting with time
IGNORE_RECURRENT_WITH_TIME = int(os.getenv('IGNORE_RECURRENT_WITH_TIME', default=False))
# User type values (ZOOM API)
USER_NON_LICENSED = 1
USER_LICENSED = 2
# Meeting type values (ZOOM API)
MEETING_TYPE_INSTANT = 1
MEETING_TYPE_SCHEDULED = 2
MEETING_TYPE_RECURRING_WITHOUT_TIME = 3
MEETING_TYPE_RECURRING_WITH_TIME = 8
client = ZoomClient(CLIENT_ID, CLIENT_SECRET, ACCOUNT_ID)
users = json.loads(client.user.list(page_size=100).content)['users']
required_licenses = 0
recurring_meetings = 0
recurring_meetings_notime = 0
scheduled_meetings = 0
for user in users:
if user['email'] in WHITELISTED_USERS:
if user['type'] == USER_NON_LICENSED:
user_update_type(client, user, USER_LICENSED)
print("[%s] User whitelisted. License assigned" % user['email'])
else:
logging.debug("[%s] User whitelisted. Nothing to do, already licensed" % user['email'])
required_licenses += 1
continue
try:
meetings = json.loads(client.meeting.list(user_id=user['id'], page_size=100).content)['meetings']
except:
continue
for meeting in meetings:
if meeting['type'] == MEETING_TYPE_RECURRING_WITHOUT_TIME:
if not IGNORE_RECURRENT_WITHOUT_TIME:
# Recurring meeting, with no fixed time
if user['type'] == USER_NON_LICENSED:
user_update_type(client, user, USER_LICENSED)
print("[%s] Recurring meeting with no fixed time. License assigned" % user['email'])
else:
print("[%s] Recurring meeting with no fixed time. Nothing to do, already licensed" % user['email'])
required_licenses += 1
recurring_meetings_notime += 1
break
print("[%s] Recurring meeting with no fixed time. Ignored" % user['email'])
break
if meeting['type'] == MEETING_TYPE_RECURRING_WITH_TIME:
# Recurring meeting with fixed time
## We need to query for meeting occurrences
## and in order to do that we load "full" meeting info
meeting = json.loads(client.meeting.get(id=meeting['id']).content)
if not IGNORE_RECURRENT_WITH_TIME:
if 'occurrences' in meeting:
for occurrence in meeting['occurrences']:
if is_meeting_soon(occurrence):
if user['type'] == USER_NON_LICENSED:
user_update_type(client, user, USER_LICENSED)
print("[%s] Recurring meetings scheduled. License assigned" % user['email'])
else:
print("[%s] Recurring meetings scheduled. Nothings to do, already licensed" % user['email'])
required_licenses += 1
recurring_meetings += 1
break
else:
continue # all occurrences are far
break # there is a close occurrence
print("[%s] Recurring meeting with fixed time. Ignored" % user['email'])
break
else:
# Scheduled meeting or instant meeting
try:
if is_meeting_soon(meeting):
if user['type'] == USER_NON_LICENSED:
user_update_type(client, user, USER_LICENSED)
print("[%s] Meetings scheduled. License assigned" % user['email'])
else:
print("[%s] Meetings scheduled. Nothing to do, already licensed" % user['email'])
required_licenses += 1
scheduled_meetings += 1
break
except NotSyncedError as err:
print("[%s] WARNING: Meeting not synced with Google Calendar" % user['email'])
#TODO: assign license if envar INCLUDE_NOT_SYNCED_MEETINGS
else:
if user['type'] == USER_LICENSED:
user_update_type(client, user, USER_NON_LICENSED)
print("[%s] No meetings scheduled. License removed" % user['email'])
else:
logging.debug("[%s] No meetings scheduled. Nothing to do" % user['email'])
print("\n")
print("~ SUMMARY ~")
print("Users: %i" % len(users))
print("Whitelisted users: %i" % len(WHITELISTED_USERS))
print("Users with meetings: %i" % (required_licenses - len(WHITELISTED_USERS)))
print("- Recurring meetings (no fixed time): %i" % (recurring_meetings_notime))
print("- Recurring meetings (fixed time): %i" % (recurring_meetings))
print("- Scheduled or live meetings: %i" % (scheduled_meetings))
print("Assigned licenses: %i/%i" % (required_licenses, LICENSES))
print("Available licenses: %i/%i" % ((LICENSES - required_licenses), LICENSES))