-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.py
279 lines (245 loc) · 13.4 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
# !/usr/bin/env python3.9
# Program Name: ZoomToVimeo.py
# Description: ZoomToVimeo is a Python script that uses Zoom and Vimeo's API to download
# specific or all cloud recordings from a Zoom Businnes account,
# save them locally and then upload them to a Vimeo Businnes account.
# All via simple command line inputs.
# This script uses the Server to Server OAUTH app
# to access the Zoom API and an OAUTH app to access
# the Vimeo API.
# Created: 16/1/2023
# Author: Alessandro Cvetković
# Website: https://github.com/alek-tech/ZoomToVimeo
# Forked from: https://github.com/ricardorodrigues-ca/zoom-recording-downloader
# IN THIS SCRIPTS ZOOM MEETINGS ARE CALLED RECORDINGS AND RECORDINGS ARE CALLED FILES.
import requests
import pandas as pd
from datetime import date, timedelta, datetime
from time import sleep
from dotenv import load_dotenv
from Functions import *
# import imaplib
# import ssl
# ctx = ssl.create_default_context()
# ctx.set_ciphers('DEFAULT')
# imapSrc = imaplib.IMAP4_SSL('mail.safemail.it', ssl_context = ctx)
class ZoomToVimeo():
# CLEAR SCREEN BUFFER
os.system('cls' if os.name == 'nt' else 'clear')
# CONNECT TO VIMEO API
vimeo_connect()
########### ADD PATH TO DOWNLOAD FOLDER HERE ###########
DOWNLOAD_DIRECTORY = r'PATH/TO/FOLDER'
# COMPLETED DOWNLOADS LOG
COMPLETED_DOWNLOADS_LOG = os.sep.join([os.path.dirname(os.path.realpath(__file__)), "completed-downloads.log"])
COMPLETED_DOWNLOADS_IDS = set()
# COMPLETED UPLOADS LOG
COMPLETED_UPLOADS_LOG = os.sep.join([os.path.dirname(os.path.realpath(__file__)), "completed-uploads.log"])
COMPLETED_UPLOADS_IDS = set()
# DEFAULT RECORDING DATES WE START TO DOWNLOAD FROM (class variables)
RECORDING_START_YEAR = 2020
RECORDING_START_MONTH = 1
RECORDING_START_DAY = 1
RECORDING_END_DATE = date.today()
SINGLES_PATH = os.sep.join([os.path.dirname(os.path.realpath(__file__)), "Single_users.xlsx"])
load_dotenv()
def __init__(self):
self.vimeo_token = os.environ.get("vimeo_token")
self.zoom_token = get_token()
self.token_expiry = datetime.now() + timedelta(minutes=50)
self.auth_header = {'Authorization': f'Bearer {self.zoom_token}'}
self.dates = choose_dates(self)
self.specific_folder = specific_folder()
self.load_completed_downloads = load_completed_downloads_ids(self)
self.load_completed_uploads = load_completed_uploads_ids(self)
self.correct_foldername = ""
def all(self):
#GET USERS EMAILS
ems = get_all_users(self)
#LOOP THROUGH EMAILS
for em in ems:
print("\nGetting recordings of user: " + em + "\n")
sleep(1.1)
if user_check(em):
vimeo_folder_id = int(user_check(em))
else:
vimeo_folder_id = None
if self.specific_folder != False:
vimeo_folder_id = int(self.specific_folder)
API_ENDPOINT_RECORDING_LIST = 'https://api.zoom.us/v2/users/' + em + '/recordings'
recordings = []
# GET INFO OF ALL RECORDINGS(MEETINGS) OF THE CURRENT EMAIL
# CREATE REQUEST PARAMS FOR EACH REQUEST
for start, end in dates_gen(date(self.RECORDING_START_YEAR, self.RECORDING_START_MONTH, self.RECORDING_START_DAY), self.RECORDING_END_DATE, timedelta(days=30)):
post_data = params(start, end, em)
recs = []
# REFRESH ZOOM TOKEN IF EXPIRED
if datetime.now() >= self.token_expiry:
while not get_token:
sleep(1)
else:
print("New Zoom Token recieved. Resetting Token expiry timestamp.")
self.token_expiry = datetime.now() + timedelta(minutes=50)
pass
# REQUEST RECORDINGS INFO
try:
response = requests.get(url=API_ENDPOINT_RECORDING_LIST,headers=self.auth_header, params=post_data)
except:
print("Couldn't get user recordings")
exit(1)
sleep(0.3)
recordings_data = response.json()
# APPEND RECORDINGS TO RECS(useful for debugging)
for rec in recordings_data['meetings']:
recs.append(rec)
if len(recs) > 0:
print("Found",len(recs), "recordings between", start, "and", end)
else:
pass
# PUT INFO OF EACH RECORDING TO LIST
recordings.extend(recordings_data['meetings'])
#LOOP THROUGH RECORDING INFO
for recording in recordings:
print("Recordings")
success = False
recording_id = recording['uuid']
if recording_id in self.COMPLETED_DOWNLOADS_IDS:
print("==> Skipping already downloaded recording: ", recording_id)
continue
# EXTRACT INFO ABOUT EVERY FILE FOUND IN CURRENT RECORDING INCLUDING DOWNLOAD URL
rec_files = get_recordings_files(self,recording)
# LOOP THROUGH FILES
for file_type, file_extension, download_url, recording_type, recording_id, recording_name, recording_date in rec_files:
filename = recording_name + f".{file_extension.swapcase()}"
print("rec_files")
if recording_type != 'incomplete':
# REFRESH ZOOM TOKEN IF EXPIRED
if datetime.now() >= self.token_expiry:
while not get_token:
sleep(1)
else:
print("New Zoom Token recieved. Resetting Token expiry timestamp.")
self.token_expiry = datetime.now() + timedelta(minutes=50)
# MODIFY DOWNLOAD URL IF NEW TOKEN RECIEVED
for download in recording['recording_files']:
download_url = download['download_url'] + "?access_token=" + self.zoom_token
pass
# DOWNLOAD FILE
success |= download_file(self,download_url, em, filename)
else:
print("### Incomplete Recording " + filename + "from account " + em)
success = False
if success:
# if successful, write the ID of this recording to the completed downloads ids
with open(self.COMPLETED_DOWNLOADS_LOG, 'a') as log:
self.COMPLETED_DOWNLOADS_IDS.add(recording_id)
log.write(recording_id)
log.write('\n')
log.flush()
# UPLOAD FILE TO VIMEO
if recording_id in self.COMPLETED_UPLOAD_IDS:
print("==> Skipping already uploaded recording: ", recording_id)
continue
all_upload(self, em, filename, vimeo_folder_id, recording_id)
else:
pass
print("\n*** All done! ***")
def single(self):
#LOOP THROUGH EXCEL FILE WITH EMAIL - FOLDER_ID PAIRS
df = pd.read_excel(fr"{self.SINGLES_PATH}")#, sheet_name = "Sheet1")
for i, row in df.iloc[0:].iterrows():
sleep(1.1)
em = row['Email']
API_ENDPOINT_RECORDING_LIST = 'https://api.zoom.us/v2/users/' + em + '/recordings'
try:
vimeo_folder_id = int(row['Folder'])
except:
print("\nMissing Vimeo folder id for ",em, "in", "Single_users.xlsx")
pass
if self.specific_folder != False:
vimeo_folder_id = int(self.specific_folder)
else:
vimeo_folder_id = float(row['Folder'])
print("\nChecking user:", em, "\n")
recordings = []
# GET INFO OF ALL RECORDINGS(MEETINGS) OF THE CURRENT EMAIL
# CREATE REQUEST PARAMS FOR EACH REQUEST
for start, end in dates_gen(date(self.RECORDING_START_YEAR, self.RECORDING_START_MONTH, self.RECORDING_START_DAY), self.RECORDING_END_DATE, timedelta(days=30)):
post_data = params(start, end, em)
recs = []
# REFRESH ZOOM TOKEN IF EXPIRED
if datetime.now() >= self.token_expiry:
while not get_token:
sleep(1)
else:
print("New Zoom Token recieved. Resetting Token expiry timestamp.\n")
self.token_expiry = datetime.now() + timedelta(minutes=50)
pass
# REQUEST RECORDINGS INFO
try:
response = requests.get(url=API_ENDPOINT_RECORDING_LIST,headers=self.auth_header, params=post_data)
except:
print("Couldn't get user recordings")
exit(1)
sleep(0.3)
recordings_data = response.json()
# APPEND RECORDINGS TO RECS(useful for debugging)
for rec in recordings_data['meetings']:
recs.append(rec)
if len(recs) > 0:
print("Found",len(recs), "recordings between", start, "and", end)
else:
pass
# PUT INFO OF EACH RECORDING TO LIST
recordings.extend(recordings_data['meetings'])
#LOOP THROUGH RECORDING INFO
for recording in recordings:
success = False
recording_id = recording['uuid']
if recording_id in self.COMPLETED_DOWNLOADS_IDS:
print("==> Skipping already downloaded recording: ", recording_id)
continue
# EXTRACT INFO ABOUT EVERY FILE FOUND IN CURRENT RECORDING INCLUDING DOWNLOAD URL
rec_files = get_recordings_files(self, recording)
# LOOP THROUGH FILES
for file_type, file_extension, download_url, recording_type, recording_id, recording_name, recording_date in rec_files:
filename = recording_name + f".{file_extension.swapcase()}"
if recording_type != 'incomplete':
# REFRESH ZOOM TOKEN IF EXPIRED
if datetime.now() >= self.token_expiry:
while not get_token:
sleep(1)
else:
print("New Zoom Token recieved. Resetting Token expiry timestamp.")
self.token_expiry = datetime.now() + timedelta(minutes=50)
# MODIFY DOWNLOAD URL IF NEW TOKEN RECIEVED
for download in recording['recording_files']:
download_url = download['download_url'] + "?access_token=" + self.zoom_token
pass
# DOWNLOAD FILE
success |= download_file(self,download_url, em, filename)
else:
print("### Incomplete Recording " + filename + "from account " + em)
success = False
if success:
# if successful, write the ID of this recording to the completed downloads ids
with open(self.COMPLETED_DOWNLOADS_LOG, 'a') as log:
self.COMPLETED_DOWNLOADS_IDS.add(recording_id)
log.write(recording_id)
log.write('\n')
log.flush()
# UPLOAD FILE TO VIMEO
if recording_id in self.COMPLETED_UPLOAD_IDS:
print("==> Skipping already uploaded recording: ", recording_id)
continue
single_upload(self, em, filename, vimeo_folder_id, recording_id)
else:
pass
print("\n*** All done! ***")
if __name__ == "__main__":
try:
start = ZoomToVimeo()
start_program(start)
except KeyboardInterrupt:
print("\nSIGINT or CTRL-C detected. Exiting gracefully.")
raise(SystemExit)