Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changes to make code in line with PEP requirements #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
.DS_Store
.env
.env
venv/

#flake8 setup file
setup.cfg
195 changes: 105 additions & 90 deletions zoom-importer.py
Original file line number Diff line number Diff line change
@@ -1,109 +1,121 @@
import requests
import json
from slugify import slugify
from datetime import date, timedelta
import b2sdk.v2 as b2
import b2sdk
import os
import time
from environs import Env
import urllib
from datetime import date, timedelta

import b2sdk
import b2sdk.v2 as b2
import requests
from environs import Env
from slugify import slugify


def download_file(url, filename):
response = requests.get(
url=url,
headers={
"Authorization": f"Bearer {ZOOM_KEY}",
},
)
with open(filename, 'wb') as f:
f.write(response.content)
response = requests.get(
url=url,
headers={
"Authorization": f"Bearer {ZOOM_KEY}",
},
)
with open(filename, 'wb') as f:
f.write(response.content)


def b2_file_size(file):
try:
return b.get_file_info_by_name(file).content_length
except (b2sdk.exception.FileNotPresent):
return 0


def delete_recordings(uuid):
try:
uuid = urllib.parse.quote(urllib.parse.quote(uuid, safe = ''))
response = requests.delete(
url=f"https://api.zoom.us/v2/meetings/{uuid}/recordings",
headers={
"Authorization": f"Bearer {ZOOM_KEY}",
},
)
except requests.exceptions.RequestException:
print(f"delete failed for {uuid}")
try:
uuid = urllib.parse.quote(urllib.parse.quote(uuid, safe=''))
requests.delete(
url=f"https://api.zoom.us/v2/meetings/{uuid}/recordings",
headers={
"Authorization": f"Bearer {ZOOM_KEY}",
},
)
except requests.exceptions.RequestException:
print(f"delete failed for {uuid}")


def send_request(date_from, date_to):
try:
response = requests.get(
url="https://api.zoom.us/v2/users/me/recordings",
params={
"from": date_from,
"to": date_to
},
headers={
"Authorization": f"Bearer {ZOOM_KEY}",
},
)
meetings = response.json()["meetings"]
for meeting in meetings:
name = meeting['start_time'] + '-' + meeting['topic']
print(' ' + name)
if meeting['recording_count'] > 0:
files = meeting['recording_files']
sizes = [x['file_size'] for x in files]
global processed_storage
processed_storage = processed_storage + sum(sizes)
types = [x['recording_type'] for x in files]
wanted_types = ['audio_only', 'chat_file']
if 'shared_screen_with_gallery_view' in types:
wanted_types.append('shared_screen_with_gallery_view')
elif 'gallery_view' in types:
wanted_types.append('gallery_view')
elif 'shared_screen_with_speaker_view' in types:
wanted_types.append('shared_screen_with_speaker_view')
elif 'speaker_view' in types:
wanted_types.append('speaker_view')
elif 'shared_screen' in types:
wanted_types.append('shared_screen')
print(f" {wanted_types}")
for file in files:
if file['recording_type'] in wanted_types:
filename = slugify(name) + '.' + file['file_extension'].lower()
size_zoom = file['file_size']
size_bb = b2_file_size(filename)
try:
size_local = os.path.getsize(filename)
except FileNotFoundError:
size_local = 0
if size_zoom > size_bb:
if size_zoom > size_local:
file_url = file['download_url']
print(' start download: ' + filename)
download_file(file_url, filename)
print(' start upload: ' + filename)
b.upload_local_file(filename, filename)
try:
os.remove(filename)
except FileNotFoundError:
pass
delete_recordings(meeting['uuid'])
time_elapsed = time.time() - time_start
speed = processed_storage / (time_elapsed/60)
print(f"total processed {processed_storage:,} bytes, elapsed {time_elapsed:,.0f} sec, {speed:,.0f} bytes per minute")
except requests.exceptions.RequestException:
print('HTTP Request failed')
try:
response = requests.get(
url="https://api.zoom.us/v2/users/me/recordings",
params={
"from": date_from,
"to": date_to
},
headers={
"Authorization": f"Bearer {ZOOM_KEY}",
},
)
meetings = response.json()["meetings"]
for meeting in meetings:
name = meeting['start_time'] + '-' + meeting['topic']
print(' ' + name)
if meeting['recording_count'] > 0:
files = meeting['recording_files']
sizes = [x['file_size'] for x in files]
global processed_storage
processed_storage = processed_storage + sum(sizes)
types = [x['recording_type'] for x in files]
wanted_types = ['audio_only', 'chat_file']
if 'shared_screen_with_gallery_view' in types:
wanted_types.append('shared_screen_with_gallery_view')
elif 'gallery_view' in types:
wanted_types.append('gallery_view')
elif 'shared_screen_with_speaker_view' in types:
wanted_types.append('shared_screen_with_speaker_view')
elif 'speaker_view' in types:
wanted_types.append('speaker_view')
elif 'shared_screen' in types:
wanted_types.append('shared_screen')
print(f" {wanted_types}")
for file in files:
if file['recording_type'] in wanted_types:
filename = (
slugify(name)
+ '.'
+ file['file_extension'].lower()
)
size_zoom = file['file_size']
size_bb = b2_file_size(filename)
try:
size_local = os.path.getsize(filename)
except FileNotFoundError:
size_local = 0
if size_zoom > size_bb:
if size_zoom > size_local:
file_url = file['download_url']
print(' start download: ' + filename)
download_file(file_url, filename)
print(' start upload: ' + filename)
b.upload_local_file(filename, filename)
try:
os.remove(filename)
except FileNotFoundError:
pass
delete_recordings(meeting['uuid'])
time_elapsed = time.time() - time_start
speed = processed_storage / (time_elapsed / 60)
print(f'total processed {processed_storage:,} bytes, '
f'elapsed {time_elapsed:,.0f} sec, {speed:,.0f} '
'bytes per minute')
except requests.exceptions.RequestException:
print('HTTP Request failed')


def daterange(start_date, end_date):
if start_date < end_date:
raise RuntimeError('start date should be > than end_date!')
raise RuntimeError('start date should be > than end_date!')
for n in range(int((start_date - end_date).days)):
yield start_date - timedelta(n)


env = Env()
env.read_env()
B2_KEY_ID = env('B2_KEY_ID')
Expand All @@ -126,8 +138,11 @@ def daterange(start_date, end_date):
time_start = time.time()

if __name__ == "__main__":
start_date = date(DATE_FROM_Y, DATE_FROM_M, DATE_FROM_D)
end_date = date(DATE_TO_Y, DATE_TO_M, DATE_TO_D)
for single_date in daterange(start_date, end_date):
print(f"{single_date.strftime('%Y-%m-%d')}")
send_request(single_date.strftime("%Y-%m-%d"), single_date.strftime("%Y-%m-%d"))
start_date = date(DATE_FROM_Y, DATE_FROM_M, DATE_FROM_D)
end_date = date(DATE_TO_Y, DATE_TO_M, DATE_TO_D)
for single_date in daterange(start_date, end_date):
print(f"{single_date.strftime('%Y-%m-%d')}")
send_request(
single_date.strftime("%Y-%m-%d"),
single_date.strftime("%Y-%m-%d")
)