Last active
March 15, 2016 14:02
-
-
Save fthiery/95208fdd295c7d90984c to your computer and use it in GitHub Desktop.
Python client for the mediaserver API
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 python | |
# -*- coding: utf-8 -*- | |
# Copyright 2016, Florent Thiery | |
import requests | |
API_KEY = 'your-api-key' | |
BASE_URL = 'https://your.mediaserver.net' | |
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) | |
if __name__ == '__main__': | |
''' | |
import uuid | |
for i in range(1, 1000): | |
email = "random_%[email protected]" %(uuid.uuid4()) | |
api('users/add/', method='post', data={'email': email}) | |
''' | |
''' | |
for i in range(1, 1000): | |
import sys | |
fname = sys.argv[1] | |
print('[%s/1000]Uploading file %s' % (i, fname)) | |
print(api('medias/add/', method='post', files={'file': (fname, open(fname, 'rb'))})) | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment