Skip to content
Snippets Groups Projects
publish_zip_by_url.py 2.59 KiB
Newer Older
#!/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')