Newer
Older
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2017, Florent Thiery, Stéphane Diemer
import argparse
import logging
import os
import requests
import imp
logger = logging.getLogger('mediaserver_client')
MiB = 1024 * 1024
session = None
MS_CLIENT_URL = 'https://raw.githubusercontent.com/UbiCastTeam/mediaserver-client/master/mediaserver_api_client.py'
MS_CLIENT_PATH = '/tmp/mediaserver_api_client.py'
def get_client_module():
# download and load client script
if os.path.exists(MS_CLIENT_PATH):
logger.info('MediaServer client is already downloaded: "%s".', MS_CLIENT_PATH)
else:
req = requests.get(MS_CLIENT_URL, timeout=10)
if not req.ok:
raise Exception('Failed to download MS client. Status: %s, response: %s' % (req.status_code, req.text))
with open(MS_CLIENT_PATH, 'w') as fo:
fo.write(req.text)
logger.info('MediaServer client downloaded: "%s".', MS_CLIENT_PATH)
ms_client = imp.load_source('ms_client', MS_CLIENT_PATH)
return ms_client
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)
parser = argparse.ArgumentParser(description='Import media from a zip archive into a portal on the commandline')
parser.add_argument('-w', '--webtv', required=True, help='webtv url')
parser.add_argument('-u', '--url', required=True, help='media zip url')
parser.add_argument('-a', '--apikey', required=True, help='apikey')
args = parser.parse_args()
CONFIG = {
'SERVER_URL': args.webtv,
'API_KEY': args.apikey,
'PROXIES': {'http': '', 'https': ''},
'UPLOAD_CHUNK_SIZE': 5 * MiB,
'VERIFY_SSL': False,
'CLIENT_ID': 'python-api-client',
}
mscli = get_client_module()
msc = mscli.MediaServerClient()
msc.config = CONFIG
# ping
print(msc.api('/', method='get'))
with open('/tmp/file.zip', 'wb') as f:
f.write(requests.get(args.url).content)
# add media with a zip
#print(msc.add_media('Test multichunk upload zip', file_path='/tmp/file.zip'))
print(msc.add_media(file_path='/tmp/file.zip'))
# add user
# print(msc.api('users/add/', method='post', data={'email': 'test@test.com'}))
# add users with csv file; example file (header should be included):
# Firstname;Lastname;Email;Company
# Albert;Einstein;albert.einstein@test.com;Humanity
# msc.import_users_csv('users.csv')