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
|
||||
elif format == 'webm':
|
||||
streams = videos(youtubeId, 'webm')
|
||||
return streams[max(streams.keys())]['stream_url']
|
||||
return streams[max(streams.keys())]['url']
|
||||
|
||||
streams = videos(youtubeId)
|
||||
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'):
|
||||
query = quote(query)
|
||||
|
@ -121,19 +137,18 @@ def videos(id, format=''):
|
|||
'webm': 'video/webm',
|
||||
'mp4': 'video/mp4'
|
||||
}.get(format)
|
||||
url = "http://www.youtube.com/watch?v=%s" % id
|
||||
data = read_url(url)
|
||||
match = re.compile('"url_encoded_fmt_stream_map": "(.*?)"').findall(data)
|
||||
info = get_video_info(id)
|
||||
stream_map = info['url_encoded_fmt_stream_map']
|
||||
streams = {}
|
||||
if match:
|
||||
for x in match[0].split(','):
|
||||
for x in stream_map.split(','):
|
||||
stream = {}
|
||||
for s in x.split('\\u0026'):
|
||||
#for s in x.split('\\u0026'):
|
||||
for s in x.split('&'):
|
||||
key, value = s.split('=')
|
||||
value = unquote_plus(value)
|
||||
stream[key] = value
|
||||
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):
|
||||
streams[stream['itag']] = stream
|
||||
return streams
|
||||
|
@ -173,7 +188,9 @@ def download_webm(id, filename):
|
|||
streams[stream['itag']] = stream
|
||||
if streams:
|
||||
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:
|
||||
return None
|
||||
|
||||
|
@ -198,4 +215,3 @@ def get_config(id):
|
|||
if match:
|
||||
config = json.load(match[0])
|
||||
return config
|
||||
|
||||
|
|
Loading…
Reference in a new issue