Last active
March 23, 2016 10:26
-
-
Save fthiery/ea469123aafff236bcf1 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
# Copyright 2016, Florent Thiery | |
import os | |
import sys | |
import requests | |
from zipfile import ZipFile | |
API_KEY = 'my-api-key' | |
BASE_URL = 'https://my.mediaserver.com' | |
VERIFY_SSL = False | |
PROXIES = {'http': '', 'https': ''} | |
session = None | |
if not VERIFY_SSL: | |
requests.packages.urllib3.disable_warnings(requests.packages.urllib3.exceptions.InsecureRequestWarning) | |
def request(url, method='get', data={}, params={}, files={}, headers={}, json=False, timeout=10): | |
global session | |
if session is None: | |
session = requests.Session() | |
if method == 'get': | |
req_function = session.get | |
params['api_key'] = API_KEY | |
else: | |
req_function = session.post | |
data['api_key'] = API_KEY | |
req_args = { | |
'url': url, | |
'headers': headers, | |
'params': params, | |
'data': data, | |
'timeout': timeout, | |
'proxies': PROXIES, | |
'verify': VERIFY_SSL, | |
'files': files, | |
} | |
resp = req_function(**req_args) | |
if resp.status_code != 200: | |
raise Exception('HTTP %s error on %s', resp.status_code, url) | |
return resp.json() if json else resp.text.strip() | |
def api(suffix, *args, **kwargs): | |
suffix = requests.compat.urljoin('/api/v2/', suffix) | |
kwargs['url'] = requests.compat.urljoin(BASE_URL, suffix) | |
return request(*args, **kwargs) | |
def upload(video_fname, title='untitled', creation_date='2016-02-25 10:48:06', location='Room A', language='en', layout='video'): | |
xml_fname = 'metadata.xml' | |
xml_template = ''' | |
<metacast language="{language}" layout="{layout}" version="3.0"> | |
<creation>{creation_date}</creation> | |
<location>{location}</location> | |
<title>{title}</title> | |
<videos> | |
<video> | |
<filename>{filename}</filename> | |
<transcoding service="mediaserver"/> | |
</video> | |
</videos> | |
</metacast> | |
''' | |
fields = { | |
'title': title, | |
'creation_date': creation_date, | |
'location': location, | |
'language': language, | |
'layout': layout, | |
'filename': video_fname, | |
} | |
with open(xml_fname, 'w') as xml: | |
xml.write(xml_template.format(**fields)) | |
zip_fname = os.path.splitext(video_fname)[0] + '.zip' | |
with ZipFile(zip_fname, 'w') as myzip: | |
myzip.write(xml_fname) | |
myzip.write(video_fname, 'media/%s' % os.path.basename(video_fname)) | |
print('Uploading %s to %s' % (video_fname, BASE_URL)) | |
result = api('medias/add/', method='post', files={'file': (zip_fname, open(zip_fname, 'rb'))}, json=True) | |
if result['success']: | |
os.remove(xml_fname) | |
os.remove(zip_fname) | |
print('Finished') | |
else: | |
print('Error: %s' % result) | |
sys.exit(1) | |
if __name__ == '__main__': | |
fname = sys.argv[1] | |
upload(fname) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment