fix ox.web.youtube
This commit is contained in:
parent
14ea6a0f7d
commit
ff0d776b09
1 changed files with 34 additions and 18 deletions
|
@ -38,11 +38,27 @@ def video_url(youtubeId, format='mp4', timeout=cache_timeout):
|
||||||
fmt=35
|
fmt=35
|
||||||
elif format == 'webm':
|
elif format == 'webm':
|
||||||
streams = videos(youtubeId, 'webm')
|
streams = videos(youtubeId, 'webm')
|
||||||
return streams[max(streams.keys())]['stream_url']
|
return streams[max(streams.keys())]['url']
|
||||||
|
|
||||||
streams = videos(youtubeId)
|
streams = videos(youtubeId)
|
||||||
if str(fmt) in streams:
|
if str(fmt) in streams:
|
||||||
return streams[str(fmt)]['stream_url']
|
return streams[str(fmt)]['url']
|
||||||
|
|
||||||
|
def get_video_info(id):
|
||||||
|
eurl = get_url(id)
|
||||||
|
data = read_url(eurl)
|
||||||
|
t = re.compile('\W[\'"]?t[\'"]?: ?[\'"](.+?)[\'"]').findall(data)
|
||||||
|
if t:
|
||||||
|
t = t[0]
|
||||||
|
else:
|
||||||
|
raise IOError
|
||||||
|
url = "http://www.youtube.com/get_video_info?&video_id=%s&el=$el&ps=default&eurl=%s&hl=en_US&t=%s" % (id, quote(eurl), quote(t))
|
||||||
|
data = read_url(url)
|
||||||
|
info = {}
|
||||||
|
for part in data.split('&'):
|
||||||
|
key, value = part.split('=')
|
||||||
|
info[key] = unquote_plus(value).replace('+', ' ')
|
||||||
|
return info
|
||||||
|
|
||||||
def find(query, max_results=10, offset=1, orderBy='relevance'):
|
def find(query, max_results=10, offset=1, orderBy='relevance'):
|
||||||
query = quote(query)
|
query = quote(query)
|
||||||
|
@ -121,19 +137,18 @@ def videos(id, format=''):
|
||||||
'webm': 'video/webm',
|
'webm': 'video/webm',
|
||||||
'mp4': 'video/mp4'
|
'mp4': 'video/mp4'
|
||||||
}.get(format)
|
}.get(format)
|
||||||
url = "http://www.youtube.com/watch?v=%s" % id
|
info = get_video_info(id)
|
||||||
data = read_url(url)
|
stream_map = info['url_encoded_fmt_stream_map']
|
||||||
match = re.compile('"url_encoded_fmt_stream_map": "(.*?)"').findall(data)
|
|
||||||
streams = {}
|
streams = {}
|
||||||
if match:
|
for x in stream_map.split(','):
|
||||||
for x in match[0].split(','):
|
|
||||||
stream = {}
|
stream = {}
|
||||||
for s in x.split('\\u0026'):
|
#for s in x.split('\\u0026'):
|
||||||
|
for s in x.split('&'):
|
||||||
key, value = s.split('=')
|
key, value = s.split('=')
|
||||||
value = unquote_plus(value)
|
value = unquote_plus(value)
|
||||||
stream[key] = value
|
stream[key] = value
|
||||||
if 'url' in stream and 'sig' in stream:
|
if 'url' in stream and 'sig' in stream:
|
||||||
stream['stream_url'] = '%s&signature=%s' % (stream['url'], stream['sig'])
|
stream['url'] = '%s&signature=%s' % (stream['url'], stream['sig'])
|
||||||
if not stream_type or stream['type'].startswith(stream_type):
|
if not stream_type or stream['type'].startswith(stream_type):
|
||||||
streams[stream['itag']] = stream
|
streams[stream['itag']] = stream
|
||||||
return streams
|
return streams
|
||||||
|
@ -173,7 +188,9 @@ def download_webm(id, filename):
|
||||||
streams[stream['itag']] = stream
|
streams[stream['itag']] = stream
|
||||||
if streams:
|
if streams:
|
||||||
s = max(streams.keys())
|
s = max(streams.keys())
|
||||||
url = '%s&signature=%s' % (streams[s]['url'], streams[s]['sig'])
|
url = streams[s]['url']
|
||||||
|
if 'sig' in streams[s]:
|
||||||
|
url += 'signature=' + streams[s]['sig']
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@ -198,4 +215,3 @@ def get_config(id):
|
||||||
if match:
|
if match:
|
||||||
config = json.load(match[0])
|
config = json.load(match[0])
|
||||||
return config
|
return config
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue