1
1
mirror of https://github.com/ytdl-org/youtube-dl synced 2024-12-11 17:27:50 +01:00

[RoosterTeeth] Clean up code after adding subtitle support

This commit is contained in:
wtpckl 2021-01-28 17:09:54 +01:00
parent 759e8ce15b
commit b4bc4527e4
No known key found for this signature in database
GPG Key ID: 7EEB886BD742D45A

View File

@ -7,7 +7,6 @@ from .common import InfoExtractor
from ..compat import (
compat_HTTPError,
compat_str,
compat_urlparse,
)
from ..utils import (
ExtractorError,
@ -15,6 +14,9 @@ from ..utils import (
str_or_none,
urlencode_postdata,
parse_m3u8_attributes,
try_get,
url_or_none,
urljoin,
)
@ -92,9 +94,11 @@ class RoosterTeethIE(InfoExtractor):
try:
video_json = self._download_json(
api_episode_url + '/videos', display_id)['data'][0]
m3u8_url = \
video_json['attributes'].get('url') or \
video_json['links'].get('master')
m3u8_url = url_or_none(try_get(
video_json, [
lambda j: j['attributes']['url'],
lambda j: j['links']['master']],
compat_str))
except ExtractorError as e:
if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
if self._parse_json(e.cause.read().decode(), display_id).get('access') is False:
@ -102,12 +106,14 @@ class RoosterTeethIE(InfoExtractor):
'%s is only available for FIRST members' % display_id)
raise
if m3u8_url is None:
raise ExtractorError("Unable to find formats")
formats = self._extract_m3u8_formats(
m3u8_url, display_id, 'mp4', 'm3u8_native', m3u8_id='hls')
self._sort_formats(formats)
subtitles = self._extract_m3u8_subtitles(
m3u8_url, display_id)
subtitles = self._extract_m3u8_subtitles(m3u8_url, display_id)
episode = self._download_json(
api_episode_url, display_id,
@ -158,28 +164,20 @@ class RoosterTeethIE(InfoExtractor):
m3u8_doc, urlh = res
m3u8_url = urlh.geturl()
def format_url(url, base_url):
if re.match(r'^https?://', url):
return url
else:
return compat_urlparse.urljoin(base_url, url)
subtitles = {}
for line in m3u8_doc.splitlines():
if not line.startswith("#EXT-X-MEDIA:"):
continue
media = parse_m3u8_attributes(line)
media_type, media_url_raw, media_lang = (
media.get('TYPE'),
media.get('URI'),
media.get('LANGUAGE'),
)
media.get('TYPE'), media.get('URI'), media.get('LANGUAGE'),)
if not (media_type in ('SUBTITLES',) and media_url_raw and media_lang):
continue
media_url = format_url(media_url_raw, base_url=m3u8_url)
media_url = urljoin(m3u8_url, media_url_raw)
if not media_url:
continue
res = self._download_webpage_handle(
media_url, video_id,
@ -190,11 +188,13 @@ class RoosterTeethIE(InfoExtractor):
continue
m3u8_subtitle_doc, _ = res
subtitle_url = None
for subtitle_line in m3u8_subtitle_doc.splitlines():
if subtitle_line.startswith("#"):
continue
media_url = format_url(subtitle_line, base_url=media_url)
subtitle_url = urljoin(media_url, subtitle_line)
break
subtitles[media_lang] = [{'url': media_url, }, ]
if subtitle_url:
subtitles[compat_str(media_lang)] = [{'url': subtitle_url, }, ]
return subtitles if len(subtitles) > 0 else None