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