PK! 77mediaserver_api_client.py#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Copyright 2017, Florent Thiery, Stéphane Diemer # Source file: # https://github.com/UbiCastTeam/mediaserver-client/blob/master/mediaserver_api_client.py import hashlib import json import logging import math import os import requests import sys import time logger = logging.getLogger('mediaserver_client') # Do not edit this directly, create a config.json file instead CONFIG_DEFAULT = { 'SERVER_URL': 'https://mediaserver.example.com', 'API_KEY': 'the-api-key', 'PROXIES': {'http': '', 'https': ''}, 'UPLOAD_CHUNK_SIZE': 5 * 1024 * 1024, # 5 MiB 'VERIFY_SSL': False, 'CLIENT_ID': 'python-api-client', 'USE_SESSION': True, } class MediaServerClient: ''' Mediaserver api client There is some examples: Start/Stop a live ################# .. code-block:: python d = msc.api('/lives/prepare', method='post') if d['success']: oid = d['oid'] rtmp_uri = d['publish_uri'] print(oid, rtmp_uri) print(msc.api('/lives/start', method='post', data={'oid': oid})) print(msc.api('/lives/stop', method='post', data={'oid': oid})) Remove all users function ######################### .. code-block:: python def remove_all_users(): print('Remove all users') users = msc.api('/users')['users'] for user in users: msc.api('/users/delete', method='get', params={'id': user['id']}) Add media with a video, make it published at once ################################################# .. code-block:: python print(msc.add_media('Test multichunk upload mp4', file_path='test.mp4', validated='yes', speaker_email='user@domain.com')) Create user personal channel and upload into it ############################################### .. code-block:: python personal_channel_oid = msc.api('/channels/personal/', method='get', params={'email': 'test@test.com'}).get('oid') respone_like = { 'slug': 'testtestcom_05881', 'oid': 'c125855df7d36iudslp3', 'dbid': 113, 'title': 'test@test.com', 'success': True } if personal_channel_oid: print('Uploading to personal channel %s' % personal_channel_oid) print(msc.add_media('Test multichunk upload mp4', file_path='test.mp4', validated='yes', speaker_email='user@domain.com', channel=personal_channel_oid)) Add media with a zip #################### .. code-block:: python print(msc.add_media('Test multichunk upload zip', file_path='/tmp/test.zip')) print(msc.add_media(file_path='test.mp4')) Add user ######## .. code-block:: python print(msc.api('users/add/', method='post', data={'email': 'test@test.com'})) Add users with csv file; example file (header should be included): ################################################################## users.csv : .. code-block:: csv Firstname;Lastname;Email;Company Albert;Einstein;albert.einstein@test.com;Humanity .. code-block:: python msc.import_users_csv('users.csv') # Usage examples of annotation api ################################## POST .. code-block:: python print(msc.api('annotations/post', params={'oid': 'v125849d470d7v92kvtc', 'time': 1000,})) Get Chapters .. code-block:: python print(msc.api('annotations/chapters/list', params={'oid': 'v125849d470d7v92kvtc'})) Get types list and print chapters id .. code-block:: python response = msc.api('annotations/types/list', params={'oid': 'v125849d470d7v92kvtc'}) for a in response['types']: if a['slug'] == 'chapter': print(a['id']) ''' def __init__(self, config_path=None, config_dict=None): self.session = None self.config = CONFIG_DEFAULT.copy() self.config_checked = False self.load_config(config_path) if config_dict: self.update_config(config_dict) if not self.config['VERIFY_SSL']: from requests.packages.urllib3.exceptions import InsecureRequestWarning requests.packages.urllib3.disable_warnings(InsecureRequestWarning) def save_config(self): with open(self.config_path, 'w') as fo: json.dump(self.config, fo, sort_keys=True, indent=4, separators=(',', ': ')) def load_config(self, path=None): self.config_path = path or 'config.json' if os.path.exists(self.config_path): logger.debug('Reading configuration file "%s".', self.config_path) try: with open(self.config_path, 'r') as fo: self.config.update(json.load(fo)) except Exception as e: logger.error('Error while parsing configuration file, using defaults (%s).', e) else: error = 'Configuration file "%s" does not exist.' % self.config_path if path: logger.error(error) raise Exception(error) logger.debug(error) def update_config(self, data): if not isinstance(data, dict): raise TypeError('A dict is required to update the configuration (received a %s object).' % type(data)) self.config.update(data) self.config_checked = False def check_config(self): # check that the MediaServer url is set if self.config_checked: return if self.config['SERVER_URL'] == CONFIG_DEFAULT['SERVER_URL']: raise ValueError('The value of SERVER_URL is using the default value. Please configure it.') self.config['SERVER_URL'] = self.config['SERVER_URL'].strip('/') if self.config['API_KEY'] == CONFIG_DEFAULT['API_KEY']: raise ValueError('The value of API_KEY is using the default value. Please configure it.') self.config_checked = True def check_server(self): self.api('/', timeout=5) def request(self, url, method='get', data=None, params=None, files=None, headers=None, parse_json=True, timeout=10, ignore_404=False): if self.session is None and self.config.get('USE_SESSION', True): self.session = requests.Session() if method == 'get': req_function = self.session.get if self.session is not None else requests.get params = params or dict() params['api_key'] = self.config['API_KEY'] else: req_function = self.session.post if self.session is not None else requests.post data = data or dict() data['api_key'] = self.config['API_KEY'] req = req_function( url=url, headers=headers, params=params, data=data, files=files, timeout=timeout, proxies=self.config['PROXIES'], verify=self.config['VERIFY_SSL'], ) if req.status_code == 404 and ignore_404: logger.info('404 ignored on url %s.' % url) return None if req.status_code != 200: raise Exception('HTTP %s error on %s: %s' % (req.status_code, url, req.text)) if parse_json: response = req.json() if 'success' in response and not response['success']: raise Exception('API call failed: %s' % (response.get('error', response.get('errors', response.get('message', 'No information on error.'))))) else: response = req.text.strip() return response def api(self, suffix, *args, **kwargs): self.check_config() begin = time.time() kwargs['url'] = self.config['SERVER_URL'] + '/api/v2/' + (suffix.rstrip('/') + '/').lstrip('/') max_retry = kwargs.pop('max_retry', None) if max_retry: retry_count = 0 while True: try: result = self.request(*args, **kwargs) break except Exception as e: # do not retry when getting a 40X error if retry_count >= max_retry or 'HTTP 40' in str(e): raise else: retry_count += 1 logger.error('Request on %s failed (tried %s times): %s', suffix, retry_count, e) time.sleep(3 * retry_count * retry_count) else: result = self.request(*args, **kwargs) logger.debug('API call duration: %.2f s - %s', time.time() - begin, suffix) return result def chunked_upload(self, file_path, remote_path=None, progress_callback=None, progress_data=None, check_md5=True): chunk_size = self.config['UPLOAD_CHUNK_SIZE'] total_size = os.path.getsize(file_path) chunks_count = math.ceil(total_size / chunk_size) chunk_index = 0 start_offset = 0 end_offset = min(chunk_size, total_size) - 1 data = dict() if check_md5: md5sum = hashlib.md5() begin = time.time() with open(file_path, 'rb') as file_object: while True: chunk = file_object.read(chunk_size) if not chunk: break chunk_index += 1 logger.debug('Uploading chunk %s/%s.', chunk_index, chunks_count) if check_md5: md5sum.update(chunk) files = {'file': (os.path.basename(file_path), chunk)} headers = {'Content-Range': 'bytes %s-%s/%s' % (start_offset, end_offset, total_size)} response = self.api('medias/resource/upload/', method='post', data=data, files=files, headers=headers, timeout=3600, max_retry=5) if progress_callback: pdata = progress_data or dict() progress_callback(0.9 * end_offset / total_size, **pdata) if 'upload_id' not in data: data['upload_id'] = response['upload_id'] start_offset += chunk_size end_offset = min(end_offset + chunk_size, total_size - 1) bandwidth = total_size * 8 / ((time.time() - begin) * 1000000) logger.debug('Upload finished, average bandwidth: %.2f Mbits/s', bandwidth) if check_md5: data['md5'] = md5sum.hexdigest() else: data['no_md5'] = 'yes' if remote_path: data['path'] = remote_path response = self.api('medias/resource/upload/complete/', method='post', data=data, timeout=3600, max_retry=5) if progress_callback: pdata = progress_data or dict() progress_callback(1., **pdata) return data['upload_id'] def add_media(self, title=None, file_path=None, progress_callback=None, progress_data=None, **kwargs): if not title and not file_path: raise ValueError('You should give a title or a file to create a media.') self.check_server() metadata = kwargs metadata['origin'] = self.config['CLIENT_ID'] if title: metadata['title'] = title if file_path: metadata['code'] = self.chunked_upload(file_path, progress_callback=progress_callback, progress_data=progress_data) response = self.api('medias/add/', method='post', data=metadata, timeout=3600) return response def remove_all_content(self): logger.info('Remove all content') channels = self.api('channels/tree/')['channels'] while msc.api('channels/tree')['channels']: for c in channels: c_oid = c['oid'] msc.api('channels/delete/', method='post', data={'oid': c_oid, 'delete_content': 'yes'}) logger.info('Emptied channel %s' % c_oid) channels = msc.api('channels/tree/')['channels'] def import_users_csv(self, csv_path): groupname = 'Users imported from csv on %s' % time.ctime() groupid = self.api('groups/add/', method='post', data={'name': groupname}).get('id') logger.info('Created group %s with id %s' % (groupname, groupid)) with open(csv_path, 'r') as f: d = f.read() for index, l in enumerate(d.split('\n')): # Skip first line (contains header) if l and index > 0: fields = [field.strip() for field in l.split(';')] email = fields[2] user = { 'email': email, 'first_name': fields[0], 'last_name': fields[1], 'company': fields[3], 'username': email, 'is_active': 'true', } logger.info('Adding %s' % email) try: logger.info(self.api('users/add/', method='post', data=user)) except Exception as e: logger.error('Error : %s' % e) logger.info('Adding user %s to group %s' % (email, groupname)) try: logger.info(self.api('groups/members/add/', method='post', data={'id': groupid, 'user_email': email})) except Exception as e: logger.error('Error : %s' % e) if __name__ == '__main__': log_format = '%(asctime)s %(name)s %(levelname)s %(message)s' logging.basicConfig(level=logging.DEBUG, format=log_format) urllib3_logger = logging.getLogger('requests.packages.urllib3') urllib3_logger.setLevel(logging.WARNING) config_path = sys.argv[1] if len(sys.argv) > 1 else None msc = MediaServerClient(config_path) # ping print(msc.api('/')) PK!p.mediaserver_api_client-0.1.0.dist-info/LICENSE GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below. 0. Additional Definitions. As used herein, "this License" refers to version 3 of the GNU Lesser General Public License, and the "GNU GPL" refers to version 3 of the GNU General Public License. "The Library" refers to a covered work governed by this License, other than an Application or a Combined Work as defined below. An "Application" is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library. A "Combined Work" is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the "Linked Version". The "Minimal Corresponding Source" for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version. The "Corresponding Application Code" for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work. 1. Exception to Section 3 of the GNU GPL. You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL. 2. Conveying Modified Versions. If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version: a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy. 3. Object Code Incorporating Material from Library Header Files. The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following: a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the object code with a copy of the GNU GPL and this license document. 4. Combined Works. You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following: a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the Combined Work with a copy of the GNU GPL and this license document. c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document. d) Do one of the following: 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source. 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version. e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.) 5. Combined Libraries. You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following: a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License. b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 6. Revised Versions of the GNU Lesser General Public License. The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation. If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library. PK!HڽTU,mediaserver_api_client-0.1.0.dist-info/WHEEL A н#Z;/"d&F[xzw@Zpy3Fv]\fi4WZ^EgM_-]#0(q7PK!H/mediaserver_api_client-0.1.0.dist-info/METADATA[o@H *HKB}m0^8gL7ggd3:Ofޓ(@cEңkХT&>:oAv[E;C}{krpED ]+L-H&>Z>ڋ~GakjgTN4g{?j]ĂJ&*᱒G6|WX ݡlߋY8v_"9<%DIiˈlPٺVK  U,ۜ\(qLCΚ#ojfo˿V0kgiS4褂E,MWV_ZL]䅴J_bė3bmk 7ߞBƕ݆WlQ p 0%pCXt Xbm=7PK!HJY-mediaserver_api_client-0.1.0.dist-info/RECORDKr0нg2|̢ Eơj Oo7U,qd%KRr&Z6C|s¬rtÑsD&k a 4B%)]ro )[K"Hw8,B$ tM/LC{9m0礏P?P .|f[UL-pBX[|]` ǽ!nU_AjkD?:s'hZ =@jCC PK! 77mediaserver_api_client.pyPK!p.7mediaserver_api_client-0.1.0.dist-info/LICENSEPK!HڽTU,Umediaserver_api_client-0.1.0.dist-info/WHEELPK!H/Vmediaserver_api_client-0.1.0.dist-info/METADATAPK!HJY-Xmediaserver_api_client-0.1.0.dist-info/RECORDPKY