Last active
March 30, 2016 12:04
-
-
Save kuu/15526b90ae318b7ae7bb809237a518d3 to your computer and use it in GitHub Desktop.
Create a remote asset
This file contains hidden or 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/python | |
import sys | |
import datetime | |
import json | |
import urllib | |
import httplib | |
import hashlib | |
import base64 | |
args = sys.argv | |
argc = len(args) | |
if (argc != 4): | |
print 'Usage: # 3 params required - 1) asset_name 2) flash_url 3) iphone_url' | |
quit() | |
asset_name = args[1] | |
flash_url = args[2] | |
iphone_url = args[3] | |
api_key = 'Your API Key' | |
secret = 'Your Secret Key' | |
method = 'POST' | |
host = 'api.ooyala.com' | |
path = '/v2/assets' | |
expires = (datetime.datetime.now() + datetime.timedelta(days=1)).strftime('%s') | |
params = { | |
'api_key': api_key, | |
'expires': expires | |
} | |
data = { | |
'name': asset_name, | |
'asset_type': 'remote_asset', | |
'is_live_stream': 'true', | |
'stream_urls': { | |
'flash': flash_url, | |
'iphone': iphone_url | |
} | |
} | |
class ooyala_api(object): | |
def generate_signature(self, secret_key, http_method, request_path, query_params, request_body=''): | |
signature = secret_key + http_method.upper() + request_path | |
for key, value in query_params.iteritems(): | |
signature += key + '=' + value | |
signature += request_body | |
signature = base64.b64encode(hashlib.sha256(signature).digest())[0:43] | |
signature = urllib.quote_plus(signature) | |
return signature | |
api = ooyala_api() | |
data_string = json.dumps(data) | |
signature = api.generate_signature(secret, method, path, params, data_string) | |
query_string = urllib.urlencode(params) + '&signature=' + signature | |
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'} | |
conn = httplib.HTTPConnection(host) | |
conn.request('POST', path + '?' + query_string, data_string, headers) | |
response = conn.getresponse() | |
print(response.status, response.reason) | |
data = response.read() | |
print(data) | |
conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment