From b4bc4527e48f8ca70078115e702a7b37f12f0924 Mon Sep 17 00:00:00 2001 From: wtpckl Date: Thu, 28 Jan 2021 17:09:54 +0100 Subject: [PATCH] [RoosterTeeth] Clean up code after adding subtitle support --- youtube_dl/extractor/roosterteeth.py | 40 ++++++++++++++-------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/youtube_dl/extractor/roosterteeth.py b/youtube_dl/extractor/roosterteeth.py index fead45a17..3fc279d0f 100644 --- a/youtube_dl/extractor/roosterteeth.py +++ b/youtube_dl/extractor/roosterteeth.py @@ -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