]> jfr.im git - yt-dlp.git/blobdiff - yt_dlp/extractor/espn.py
[cleanup] Add more ruff rules (#10149)
[yt-dlp.git] / yt_dlp / extractor / espn.py
index 451148636daacc2489ba2752543506130ebd563b..4e9b63524eea29eb0e32b1d131c7c125eae0406a 100644 (file)
@@ -10,6 +10,7 @@
     determine_ext,
     dict_get,
     int_or_none,
+    traverse_obj,
     unified_strdate,
     unified_timestamp,
 )
@@ -99,13 +100,13 @@ class ESPNIE(OnceIE):
     }, {
         'url': 'http://www.espn.com/watch/player?bucketId=257&id=19505875',
         'only_matching': True,
-    }]
+    }]
 
     def _real_extract(self, url):
         video_id = self._match_id(url)
 
         clip = self._download_json(
-            'http://api-app.espn.com/v1/video/clips/%s' % video_id,
+            f'http://api-app.espn.com/v1/video/clips/{video_id}',
             video_id)['videos'][0]
 
         title = clip['headline']
@@ -114,16 +115,16 @@ def _real_extract(self, url):
         formats = []
 
         def traverse_source(source, base_source_id=None):
-            for source_id, source in source.items():
-                if source_id == 'alert':
+            for src_id, src_item in source.items():
+                if src_id == 'alert':
                     continue
-                elif isinstance(source, str):
-                    extract_source(source, base_source_id)
-                elif isinstance(source, dict):
+                elif isinstance(src_item, str):
+                    extract_source(src_item, base_source_id)
+                elif isinstance(src_item, dict):
                     traverse_source(
-                        source,
-                        '%s-%s' % (base_source_id, source_id)
-                        if base_source_id else source_id)
+                        src_item,
+                        f'{base_source_id}-{src_id}'
+                        if base_source_id else src_id)
 
         def extract_source(source_url, source_id=None):
             if source_url in format_urls:
@@ -161,7 +162,6 @@ def extract_source(source_url, source_id=None):
         links = clip.get('links', {})
         traverse_source(links.get('source', {}))
         traverse_source(links.get('mobile', {}))
-        self._sort_formats(formats)
 
         description = clip.get('caption') or clip.get('description')
         thumbnail = clip.get('thumbnail')
@@ -209,7 +209,7 @@ def _real_extract(self, url):
             webpage, 'video id', group='id')
 
         return self.url_result(
-            'http://espn.go.com/video/clip?id=%s' % video_id, ESPNIE.ie_key())
+            f'http://espn.go.com/video/clip?id={video_id}', ESPNIE.ie_key())
 
 
 class FiveThirtyEightIE(InfoExtractor):
@@ -240,7 +240,7 @@ def _real_extract(self, url):
 
 
 class ESPNCricInfoIE(InfoExtractor):
-    _VALID_URL = r'https?://(?:www\.)?espncricinfo\.com/video/[^#$&?/]+-(?P<id>\d+)'
+    _VALID_URL = r'https?://(?:www\.)?espncricinfo\.com/(?:cricket-)?videos?/[^#$&?/]+-(?P<id>\d+)'
     _TESTS = [{
         'url': 'https://www.espncricinfo.com/video/finch-chasing-comes-with-risks-despite-world-cup-trend-1289135',
         'info_dict': {
@@ -251,16 +251,28 @@ class ESPNCricInfoIE(InfoExtractor):
             'upload_date': '20211113',
             'duration': 96,
         },
-        'params': {'skip_download': True}
+        'params': {'skip_download': True},
+    }, {
+        'url': 'https://www.espncricinfo.com/cricket-videos/daryl-mitchell-mitchell-santner-is-one-of-the-best-white-ball-spinners-india-vs-new-zealand-1356225',
+        'info_dict': {
+            'id': '1356225',
+            'ext': 'mp4',
+            'description': '"Santner has done it for a long time for New Zealand - we\'re lucky to have him"',
+            'upload_date': '20230128',
+            'title': 'Mitchell: \'Santner is one of the best white-ball spinners at the moment\'',
+            'duration': 87,
+        },
+        'params': {'skip_download': 'm3u8'},
     }]
 
     def _real_extract(self, url):
-        id = self._match_id(url)
-        data_json = self._download_json(f'https://hs-consumer-api.espncricinfo.com/v1/pages/video/video-details?videoId={id}', id)['video']
+        video_id = self._match_id(url)
+        data_json = self._download_json(
+            f'https://hs-consumer-api.espncricinfo.com/v1/pages/video/video-details?videoId={video_id}', video_id)['video']
         formats, subtitles = [], {}
         for item in data_json.get('playbacks') or []:
             if item.get('type') == 'HLS' and item.get('url'):
-                m3u8_frmts, m3u8_subs = self._extract_m3u8_formats_and_subtitles(item['url'], id)
+                m3u8_frmts, m3u8_subs = self._extract_m3u8_formats_and_subtitles(item['url'], video_id)
                 formats.extend(m3u8_frmts)
                 subtitles = self._merge_subtitles(subtitles, m3u8_subs)
             elif item.get('type') == 'AUDIO' and item.get('url'):
@@ -268,9 +280,8 @@ def _real_extract(self, url):
                     'url': item['url'],
                     'vcodec': 'none',
                 })
-        self._sort_formats(formats)
         return {
-            'id': id,
+            'id': video_id,
             'title': data_json.get('title'),
             'description': data_json.get('summary'),
             'upload_date': unified_strdate(dict_get(data_json, ('publishedAt', 'recordedAt'))),
@@ -281,29 +292,43 @@ def _real_extract(self, url):
 
 
 class WatchESPNIE(AdobePassIE):
-    _VALID_URL = r'https://www.espn.com/watch/player/_/id/(?P<id>[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})'
+    _VALID_URL = r'https?://(?:www\.)?espn\.com/(?:watch|espnplus)/player/_/id/(?P<id>[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})'
     _TESTS = [{
-        'url': 'https://www.espn.com/watch/player/_/id/ba7d17da-453b-4697-bf92-76a99f61642b',
+        'url': 'https://www.espn.com/watch/player/_/id/dbbc6b1d-c084-4b47-9878-5f13c56ce309',
         'info_dict': {
-            'id': 'ba7d17da-453b-4697-bf92-76a99f61642b',
+            'id': 'dbbc6b1d-c084-4b47-9878-5f13c56ce309',
             'ext': 'mp4',
-            'title': 'Serbia vs. Turkey',
-            'thumbnail': 'https://artwork.api.espn.com/artwork/collections/media/ba7d17da-453b-4697-bf92-76a99f61642b/default?width=640&apikey=1ngjw23osgcis1i1vbj96lmfqs',
+            'title': 'Huddersfield vs. Burnley',
+            'duration': 7500,
+            'thumbnail': 'https://artwork.api.espn.com/artwork/collections/media/dbbc6b1d-c084-4b47-9878-5f13c56ce309/default?width=640&apikey=1ngjw23osgcis1i1vbj96lmfqs',
         },
         'params': {
             'skip_download': True,
         },
     }, {
-        'url': 'https://www.espn.com/watch/player/_/id/4e9b5bd1-4ceb-4482-9d28-1dd5f30d2f34',
+        'url': 'https://www.espn.com/watch/player/_/id/a049a56e-a7ce-477e-aef3-c7e48ef8221c',
         'info_dict': {
-            'id': '4e9b5bd1-4ceb-4482-9d28-1dd5f30d2f34',
+            'id': 'a049a56e-a7ce-477e-aef3-c7e48ef8221c',
             'ext': 'mp4',
-            'title': 'Real Madrid vs. Real Betis (LaLiga)',
+            'title': 'Dynamo Dresden vs. VfB Stuttgart (Round #1) (German Cup)',
+            'duration': 8335,
             'thumbnail': 'https://s.secure.espncdn.com/stitcher/artwork/collections/media/bd1f3d12-0654-47d9-852e-71b85ea695c7/16x9.jpg?timestamp=202201112217&showBadge=true&cb=12&package=ESPN_PLUS',
         },
         'params': {
             'skip_download': True,
         },
+    }, {
+        'url': 'https://www.espn.com/espnplus/player/_/id/317f5fd1-c78a-4ebe-824a-129e0d348421',
+        'info_dict': {
+            'id': '317f5fd1-c78a-4ebe-824a-129e0d348421',
+            'ext': 'mp4',
+            'title': 'The Wheel - Episode 10',
+            'duration': 3352,
+            'thumbnail': 'https://s.secure.espncdn.com/stitcher/artwork/collections/media/317f5fd1-c78a-4ebe-824a-129e0d348421/16x9.jpg?timestamp=202205031523&showBadge=true&cb=12&package=ESPN_PLUS',
+        },
+        'params': {
+            'skip_download': True,
+        },
     }]
 
     _API_KEY = 'ZXNwbiZicm93c2VyJjEuMC4w.ptUt7QxsteaRruuPmGZFaJByOoqKvDP2a5YkInHrc7c'
@@ -317,9 +342,10 @@ def _call_bamgrid_api(self, path, video_id, payload=None, headers={}):
 
     def _real_extract(self, url):
         video_id = self._match_id(url)
-        video_data = self._download_json(
+        cdn_data = self._download_json(
             f'https://watch-cdn.product.api.espn.com/api/product/v3/watchespn/web/playback/event?id={video_id}',
-            video_id)['playbackState']
+            video_id)
+        video_data = cdn_data['playbackState']
 
         # ESPN+ subscription required, through cookies
         if 'DTC' in video_data.get('sourceId'):
@@ -341,28 +367,28 @@ def _real_extract(self, url):
                     'subject_token': assertion,
                     'subject_token_type': 'urn:bamtech:params:oauth:token-type:device',
                     'platform': 'android',
-                    'grant_type': 'urn:ietf:params:oauth:grant-type:token-exchange'
+                    'grant_type': 'urn:ietf:params:oauth:grant-type:token-exchange',
                 })['access_token']
 
             assertion = self._call_bamgrid_api(
                 'accounts/grant', video_id, payload={'id_token': cookie.value.split('|')[1]},
                 headers={
                     'Authorization': token,
-                    'Content-Type': 'application/json; charset=UTF-8'
+                    'Content-Type': 'application/json; charset=UTF-8',
                 })['assertion']
             token = self._call_bamgrid_api(
                 'token', video_id, payload={
                     'subject_token': assertion,
                     'subject_token_type': 'urn:bamtech:params:oauth:token-type:account',
                     'platform': 'android',
-                    'grant_type': 'urn:ietf:params:oauth:grant-type:token-exchange'
+                    'grant_type': 'urn:ietf:params:oauth:grant-type:token-exchange',
                 })['access_token']
 
             playback = self._download_json(
                 video_data['videoHref'].format(scenario='browser~ssai'), video_id,
                 headers={
                     'Accept': 'application/vnd.media-service+json; version=5',
-                    'Authorization': token
+                    'Authorization': token,
                 })
             m3u8_url, headers = playback['stream']['complete'][0]['url'], {'authorization': token}
 
@@ -384,10 +410,10 @@ def _real_extract(self, url):
             m3u8_url, headers = asset['stream'], {}
 
         formats, subtitles = self._extract_m3u8_formats_and_subtitles(m3u8_url, video_id, 'mp4', m3u8_id='hls')
-        self._sort_formats(formats)
 
         return {
             'id': video_id,
+            'duration': traverse_obj(cdn_data, ('tracking', 'duration')),
             'title': video_data.get('name'),
             'formats': formats,
             'subtitles': subtitles,