Created
January 6, 2021 21:04
-
-
Save FabienLavocat/45cdb159bfb7020331636ac2955bed3e to your computer and use it in GitHub Desktop.
Dolby.IO - Monitoring API - Query conferences
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
########################### | |
# | |
# Dolby.IO - Monitoring API | |
# By Fabien Lavocat | |
# | |
########################### | |
import argparse | |
import base64 | |
import requests | |
import time | |
from urllib.parse import urlencode | |
parser = argparse.ArgumentParser(description='Dolby.io Monitor APIs') | |
parser.add_argument('--key', | |
help='Dolby Application Consumer Key', | |
type=str) | |
parser.add_argument('--secret', | |
help='Dolby Application Consumer Secret', | |
type=str) | |
parser.add_argument('--alias', | |
help='Conference Alias Filter (regex) - e.g. customer.* - see this URL for examples https://dolby.io/developers/interactivity-apis/rest-apis/monitor#operation/getConferences', | |
default='', | |
type=str) | |
parser.add_argument('--days', | |
help='Number of days to go back to', | |
default=30, | |
type=int) | |
args = parser.parse_args() | |
consumer_key = args.key | |
consumer_secret = args.secret | |
if len(consumer_key) <= 0 or len(consumer_secret) < 0: | |
print('You must provide a consumer key and secret with the arguments --key and --secret') | |
exit() | |
alias_filter = args.alias | |
days = args.days | |
if days < 0: | |
print('You must provide a valid number of days') | |
exit() | |
# | |
# Request an access token | |
# | |
token_url = 'https://api.voxeet.com/v1/auth/token' | |
creds = (consumer_key + ':' + consumer_secret).encode('ascii') | |
headers = { | |
'Authorization': 'Basic ' + base64.b64encode(creds).decode('ascii'), | |
'Cache-Control': 'no-cache', | |
'Content-Type': 'application/x-www-form-urlencoded', | |
'Accepted': 'application/json' | |
} | |
body = 'grant_type=client_credentials' | |
print('Request an access token...') | |
http_response = requests.post(token_url, data=body, headers=headers) | |
if http_response.status_code != 200: | |
print('Authentication error!!!') | |
exit() | |
jwt = http_response.json() | |
access_token = jwt['access_token'] | |
token_type = jwt['token_type'] | |
# | |
# Query the conferences | |
# | |
dt_from = int(round((time.time() - (days * 24 * 60 * 60)) * 1000)) | |
query_params = { | |
'max': 100, | |
'from': dt_from | |
} | |
if len(alias_filter) > 0: | |
query_params['alias'] = alias_filter | |
monitor_url = 'https://api.voxeet.com/v1/monitor/conferences?' + urlencode(query_params) | |
headers = { | |
'Authorization': token_type + ' ' + access_token, | |
'Cache-Control': 'no-cache', | |
'Accepted': 'application/json' | |
} | |
conferences = [] | |
next_token = '' | |
while True: | |
url = monitor_url | |
if len(next_token) > 0: | |
url += '&start=' + next_token | |
print('Load conferences from', url) | |
http_response = requests.get(url, headers=headers) | |
if http_response.status_code != 200: | |
print('There was a problem in the request!') | |
print('Status code: ' + str(http_response.status_code)) | |
exit() | |
response_json = http_response.json() | |
if 'conferences' in response_json: | |
sub_result = response_json['conferences'] | |
if len(sub_result) <= 0: | |
break | |
conferences += sub_result | |
if 'next' in response_json: | |
next_token = response_json['next'] | |
else: | |
break | |
else: | |
break | |
# | |
# Print the report | |
# | |
presenceDuration = 0 | |
csv_filename = str(int(round(time.time()))) + '_conferences.csv' | |
with open(csv_filename, 'w') as f: | |
f.write('Conference Id,Alias,Start,End,Presence Duration in Seconds\n') | |
for conference in conferences: | |
str_start = time.strftime("%Y-%m-%d %H:%M:%S GMT", time.gmtime(conference['start'] / 1000)) | |
str_end = time.strftime("%Y-%m-%d %H:%M:%S GMT", time.gmtime(conference['end'] / 1000)) | |
presence_duration_seconds = round(conference['presenceDuration'] / 1000) | |
print('->', conference['confId'], '-', str_start, '-', presence_duration_seconds, 'seconds') | |
presenceDuration += conference['presenceDuration'] | |
# Write the row in the csv | |
row = conference['confId'] + ',' + \ | |
conference['alias'] + ',' + \ | |
str_start + ',' + \ | |
str_end + ',' + \ | |
str(presence_duration_seconds) + '\n' | |
f.write(row) | |
print() | |
str_from = time.strftime("%Y-%m-%d %H:%M:%S GMT", time.gmtime(dt_from / 1000)) | |
print('- From', str_from) | |
if len(alias_filter) > 0: | |
print('- Alias filter =', alias_filter) | |
print('- Found', len(conferences), 'conferences.') | |
print('- Presence Duration =', round(presenceDuration / (1000 * 60)), 'minutes.') | |
print('- Report saved at', csv_filename) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment