add ox.web.youtube.subtitles

This commit is contained in:
j 2016-07-20 19:51:41 +02:00
parent d63118827b
commit adf777ce7c

View file

@ -104,14 +104,20 @@ def info(id, timeout=cache_timeout):
info['license'] = match[0].strip() info['license'] = match[0].strip()
info['license'] = re.sub('<.+?>', '', info['license']).strip() info['license'] = re.sub('<.+?>', '', info['license']).strip()
subs = subtitles(id, timeout)
if subs:
info['subtitles'] = subs
return info
def subtitles(id, timeout=cache_timeout):
url = "http://www.youtube.com/api/timedtext?hl=en&type=list&tlangs=1&v=%s&asrs=1" % id url = "http://www.youtube.com/api/timedtext?hl=en&type=list&tlangs=1&v=%s&asrs=1" % id
data = read_url(url, timeout=timeout) data = read_url(url, timeout=timeout)
xml = parseString(data) xml = parseString(data)
languages = [t.getAttribute('lang_code') for t in xml.getElementsByTagName('track')] languages = [t.getAttribute('lang_code') for t in xml.getElementsByTagName('track')]
subtitles = {}
if languages: if languages:
info['subtitles'] = {}
for language in languages: for language in languages:
url = "http://www.youtube.com/api/timedtext?hl=en&v=%s&type=track&lang=%s&name&kind"%(id, language) url = "http://www.youtube.com/api/timedtext?hl=en&v=%s&type=track&lang=%s&name&kind" % (id, language)
data = read_url(url, timeout=timeout) data = read_url(url, timeout=timeout)
xml = parseString(data) xml = parseString(data)
subs = [] subs = []
@ -128,8 +134,8 @@ def info(id, timeout=cache_timeout):
'out': end, 'out': end,
'value': ox.decode_html(text), 'value': ox.decode_html(text),
}) })
info['subtitles'][language] = subs subtitles[language] = subs
return info return subtitles
def videos(id, format=''): def videos(id, format=''):
stream_type = { stream_type = {