Skip to content

Instantly share code, notes, and snippets.

@flavioribeiro
Created September 20, 2016 20:05
Show Gist options
  • Select an option

  • Save flavioribeiro/0fbea4ba5a298a4c4199e9effa8bb596 to your computer and use it in GitHub Desktop.

Select an option

Save flavioribeiro/0fbea4ba5a298a4c4199e9effa8bb596 to your computer and use it in GitHub Desktop.
import requests
import json
from datetime import datetime
NYT_API = "http://nytimes.com/svc/video/api/v2/video/"
MEDIA_FACTORY_API = "http://media-factory.stg.nyt.net"
PRESETS = ["1080p_hls", "720p_hls", "480p_hls", "360p_hls", "240p_hls"]
def get_creation_date(hls_rendition):
parts = hls_rendition.split("/")
date = datetime.strptime(parts[4]+"-"+parts[5]+"-"+parts[6], "%Y-%m-%d")
return datetime.strftime(date, "%Y-%m-%d %H:%M:%S.0")
def get_external_id(hls_rendition):
return hls_rendition.split("/")[7].split("_")[0]
def get_slug(hls_rendition):
try:
return hls_rendition.split("_1_")[1].split("_wg")[0]
except:
return ""
def build_payload(creation_date, external_id, slug):
return '{"metadata":{"presets":'+json.dumps(PRESETS)+',"slug":"'+slug+'","createDate":"'+creation_date+'"},"externalId":"'+external_id+'"}'
def get_source(video_id):
response = requests.get(NYT_API + video_id)
renditions = json.loads(response.text)['renditions']
for rend in renditions:
if 'synd' in rend['type']:
return rend['url']
for rend in renditions:
if '1080p_mp4' in rend['type']:
return rend['url']
def trigger_job(source, payload):
response = requests.post(MEDIA_FACTORY_API+"/jobs", payload)
job_id = json.loads(response.text)['jobId']
print "Starting", job_id, "for", source
response = requests.post(MEDIA_FACTORY_API+"/jobs/"+job_id+"/start", json.dumps({"source": source}))
print response.text
for line in open("hls_bad_renditions_test.txt"):
video_id, _, hls_rendition = line.split(" ")
creation_date = get_creation_date(hls_rendition)
external_id = get_external_id(hls_rendition)
slug = get_slug(hls_rendition)
if slug:
source = get_source(video_id)
payload = build_payload(creation_date, external_id, slug)
trigger_job(source, payload)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment