<commit_before>'''Get video URLs.

I take in IDs such as ``how-better-housekeeper-12537238``
'''
import json
import re
import sys
import urllib
import urllib2


HTML_PAGE = 'http://voices.yahoo.com/video/{0}.html'
YQL = 'SELECT * FROM yahoo.media.video.streams WHERE id="{video_id}" AND format="mp4,flv,f4m" AND protocol="http" AND rt="flash" AND plrs="Gi_RxaWhgXECOj6ukNwZbO" AND acctid="{user_id}" AND plidl="{context}" AND pspid="{pagespace}" AND offnetwork="false" AND site="" AND lang="en-US" AND region="US" AND override="none" AND plist="" AND hlspre="false" AND ssl="false" AND synd="";'
YQL_URL = 'http://video.query.yahoo.com/v1/public/yql?q={0}&env=prod&format=json'


def main():
    video_id = sys.argv[1]
    response = urllib2.urlopen(HTML_PAGE.format(video_id))

    assert response.geturl() == HTML_PAGE.format(video_id)

    data = response.read()
    match = re.search(r"VideoPlayer\(({.+?})\);", data)
    snippet = match.group(1)
    snippet = snippet.replace('/* REQUIRED CONFIG ITEMS */', '')
    snippet = snippet.replace('/* OPTIONAL CONFIG ITEMS */', '')

    def rep(match):
        return '"{0}":'.format(match.group(1))

    snippet = re.sub('([a-zA-Z0-9]+) ?:', rep, snippet)

    doc = json.loads(snippet)

    video_id = doc['playlist']['mediaItems'][0]['id']
    pagespace = doc['pageSpaceId']
    context = doc['YVAP']['playContext']
    user_id = doc['YVAP']['accountId']

    yql = YQL.format(video_id=video_id, pagespace=pagespace, context=context, user_id=user_id)
    json_url = YQL_URL.format(urllib.quote(yql))

    print(json_url)

    response = urllib2.urlopen(json_url)

    doc = json.loads(response.read())

#     print(json.dumps(doc, indent=2))

    streams = doc['query']['results']['mediaObj'][0]['streams']
    streams = list(sorted(streams, key=lambda x: x['bitrate']))
    stream = streams[-1]
    stream_url = stream['host'] + stream['path']<commit_msg>video.py Support flv streams.<commit_after>'''Get video URLs.

I take in IDs such as ``how-better-housekeeper-12537238``
'''
import json
import re
import sys
import urllib
import urllib2


HTML_PAGE = 'http://voices.yahoo.com/video/{0}.html'
YQL = 'SELECT * FROM yahoo.media.video.streams WHERE id="{video_id}" AND format="mp4,flv,f4m" AND protocol="http" AND rt="flash" AND plrs="Gi_RxaWhgXECOj6ukNwZbO" AND acctid="{user_id}" AND plidl="{context}" AND pspid="{pagespace}" AND offnetwork="false" AND site="" AND lang="en-US" AND region="US" AND override="none" AND plist="" AND hlspre="false" AND ssl="false" AND synd="";'
YQL_URL = 'http://video.query.yahoo.com/v1/public/yql?q={0}&env=prod&format=json'


def main():
    video_id = sys.argv[1]
    response = urllib2.urlopen(HTML_PAGE.format(video_id))

    assert response.geturl() == HTML_PAGE.format(video_id)

    data = response.read()
    match = re.search(r"VideoPlayer\(({.+?})\);", data)
    snippet = match.group(1)
    snippet = snippet.replace('/* REQUIRED CONFIG ITEMS */', '')
    snippet = snippet.replace('/* OPTIONAL CONFIG ITEMS */', '')

    def rep(match):
        name = match.group(1)
        if name not in ('http', 'https'):
            return '"{0}":'.format(name)
        else:
            return '{0}:'.format(name)

    snippet = re.sub('([a-zA-Z0-9]+) ?:', rep, snippet)

    doc = json.loads(snippet)

    if 'streamUrl' in doc['playlist']['mediaItems'][0]:
        print(doc['playlist']['mediaItems'][0]['streamUrl'])

        return

    video_id = doc['playlist']['mediaItems'][0]['id']
    pagespace = doc['pageSpaceId']
    context = doc['YVAP']['playContext']
    user_id = doc['YVAP']['accountId']

    yql = YQL.format(video_id=video_id, pagespace=pagespace, context=context, user_id=user_id)
    json_url = YQL_URL.format(urllib.quote(yql))

    print(json_url)

    response = urllib2.urlopen(json_url)

    doc = json.loads(response.read())

#     print(json.dumps(doc, indent=2))

    streams = doc['query']['results']['mediaObj'][0]['streams']
    streams = list(sorted(streams, key=lambda x: x['bitrate']))
    stream = streams[-1]
    stream_url = stream['host'] + stream['path']
{'fraction_non_alphanumeric': 0.1858017135862913, 'fraction_numerical': 0.013953488372093023, 'mean_word_length': 4.258687258687258, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6354615', 'n_tokens_mistral': 1524, 'n_tokens_neox': 1467, 'n_words': 283}