]> jfr.im git - yt-dlp.git/blobdiff - yt_dlp/extractor/redbulltv.py
[ie/matchtv] Fix extractor (#10190)
[yt-dlp.git] / yt_dlp / extractor / redbulltv.py
index 3aae79f5da2a0717c44991130738cfce00760c6b..ceeef5204560cc017e04c1715564f2bbd5153275 100644 (file)
@@ -1,13 +1,8 @@
-# coding: utf-8
-from __future__ import unicode_literals
-
-import re
-
 from .common import InfoExtractor
-from ..compat import compat_HTTPError
+from ..networking.exceptions import HTTPError
 from ..utils import (
-    float_or_none,
     ExtractorError,
+    float_or_none,
 )
 
 
@@ -62,7 +57,7 @@ def extract_info(self, video_id):
                 'os_family': 'http',
             })
         if session.get('code') == 'error':
-            raise ExtractorError('%s said: %s' % (
+            raise ExtractorError('{} said: {}'.format(
                 self.IE_NAME, session['message']))
         token = session['token']
 
@@ -70,36 +65,33 @@ def extract_info(self, video_id):
             video = self._download_json(
                 'https://api.redbull.tv/v3/products/' + video_id,
                 video_id, note='Downloading video information',
-                headers={'Authorization': token}
+                headers={'Authorization': token},
             )
         except ExtractorError as e:
-            if isinstance(e.cause, compat_HTTPError) and e.cause.code == 404:
+            if isinstance(e.cause, HTTPError) and e.cause.status == 404:
                 error_message = self._parse_json(
-                    e.cause.read().decode(), video_id)['error']
-                raise ExtractorError('%s said: %s' % (
-                    self.IE_NAME, error_message), expected=True)
+                    e.cause.response.read().decode(), video_id)['error']
+                raise ExtractorError(f'{self.IE_NAME} said: {error_message}', expected=True)
             raise
 
         title = video['title'].strip()
 
-        formats = self._extract_m3u8_formats(
-            'https://dms.redbull.tv/v3/%s/%s/playlist.m3u8' % (video_id, token),
+        formats, subtitles = self._extract_m3u8_formats_and_subtitles(
+            f'https://dms.redbull.tv/v3/{video_id}/{token}/playlist.m3u8',
             video_id, 'mp4', entry_protocol='m3u8_native', m3u8_id='hls')
-        self._sort_formats(formats)
 
-        subtitles = {}
         for resource in video.get('resources', []):
             if resource.startswith('closed_caption_'):
                 splitted_resource = resource.split('_')
                 if splitted_resource[2]:
                     subtitles.setdefault('en', []).append({
-                        'url': 'https://resources.redbull.tv/%s/%s' % (video_id, resource),
+                        'url': f'https://resources.redbull.tv/{video_id}/{resource}',
                         'ext': splitted_resource[2],
                     })
 
         subheading = video.get('subheading')
         if subheading:
-            title += ' - %s' % subheading
+            title += f' - {subheading}'
 
         return {
             'id': video_id,
@@ -116,7 +108,7 @@ def _real_extract(self, url):
         return self.extract_info(video_id)
 
 
-class RedBullEmbedIE(RedBullTVIE):
+class RedBullEmbedIE(RedBullTVIE):  # XXX: Do not subclass from concrete IE
     _VALID_URL = r'https?://(?:www\.)?redbull\.com/embed/(?P<id>rrn:content:[^:]+:[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}:[a-z]{2}-[A-Z]{2,3})'
     _TESTS = [{
         # HLS manifest accessible only using assetId
@@ -133,14 +125,16 @@ def _real_extract(self, url):
         rrn_id = self._match_id(url)
         asset_id = self._download_json(
             'https://edge-graphql.crepo-production.redbullaws.com/v1/graphql',
-            rrn_id, headers={'API-KEY': 'e90a1ff11335423998b100c929ecc866'},
-            query={
+            rrn_id, headers={
+                'Accept': 'application/json',
+                'API-KEY': 'e90a1ff11335423998b100c929ecc866',
+            }, query={
                 'query': '''{
   resource(id: "%s", enforceGeoBlocking: false) {
     %s
     %s
   }
-}''' % (rrn_id, self._VIDEO_ESSENSE_TMPL % 'LiveVideo', self._VIDEO_ESSENSE_TMPL % 'VideoResource'),
+}''' % (rrn_id, self._VIDEO_ESSENSE_TMPL % 'LiveVideo', self._VIDEO_ESSENSE_TMPL % 'VideoResource'),  # noqa: UP031
             })['data']['resource']['videoEssence']['attributes']['assetId']
         return self.extract_info(asset_id)
 
@@ -159,8 +153,8 @@ class RedBullTVRrnContentIE(InfoExtractor):
     }]
 
     def _real_extract(self, url):
-        region, lang, rrn_id = re.search(self._VALID_URL, url).groups()
-        rrn_id += ':%s-%s' % (lang, region.upper())
+        region, lang, rrn_id = self._match_valid_url(url).groups()
+        rrn_id += f':{lang}-{region.upper()}'
         return self.url_result(
             'https://www.redbull.com/embed/' + rrn_id,
             RedBullEmbedIE.ie_key(), rrn_id)
@@ -202,7 +196,7 @@ class RedBullIE(InfoExtractor):
     _LAT_FALLBACK_MAP = ['ar', 'bo', 'car', 'cl', 'co', 'mx', 'pe']
 
     def _real_extract(self, url):
-        region, lang, filter_type, display_id = re.search(self._VALID_URL, url).groups()
+        region, lang, filter_type, display_id = self._match_valid_url(url).groups()
         if filter_type == 'episodes':
             filter_type = 'episode-videos'
         elif filter_type == 'live':
@@ -214,7 +208,7 @@ def _real_extract(self, url):
                 regions.append('LAT')
             if lang in self._INT_FALLBACK_LIST:
                 regions.append('INT')
-        locale = '>'.join(['%s-%s' % (lang, reg) for reg in regions])
+        locale = '>'.join([f'{lang}-{reg}' for reg in regions])
 
         rrn_id = self._download_json(
             'https://www.redbull.com/v3/api/graphql/v1/v3/query/' + locale,