asvk
2/24/2016 - 8:01 PM

push task to vg transcoder example

push task to vg transcoder example

import requests # pip install requests
import pprint
import sys
import json
import traceback

# host: transcoder url, http://host.com:port
# url: media public url (e.g. s3://)
def pushVGProxyTask(url, host):
    message_data = {}
    message_data["url"] = url
    message_data["commands"] = ["command1", "command2"]

    if "http://" not in host:
        host = "http://" + host
    if host.endswith("/"):
        host = host[:-1]
        print host

    try:
        task = json.dumps(message_data)
        response = requests.post(host + "/api/1/media", data = task)
    except Exception:
        print(traceback.format_exc())
        sys.exit(1)

    if response.status_code == requests.codes.ok:
        data_response = response.json()
        return data_response["id"]
    else:
        print response.text
        pprint.pprint(task)
        raise RuntimeError("could not crate transcoder task. server: " + host)
        
if __name__ == "__main__":
    if len(sys.argv) != 3:
        print ("Usage: %s <queue host> <media URL>".format(sys.argv[0]))
        sys.exit(1)
  
    media_id = pushVGProxyTask(taskUrl, queueHost)
    print ("New media id is", media_id)