]> jfr.im git - yt-dlp.git/blobdiff - yt_dlp/extractor/funimation.py
[ie/matchtv] Fix extractor (#10190)
[yt-dlp.git] / yt_dlp / extractor / funimation.py
index 36a9c4772323651873c9e9bb2d8da69f39752287..d3e61c84f8d236613450e76ab6b55216d57a9e7b 100644 (file)
@@ -1,24 +1,22 @@
-# coding: utf-8
-from __future__ import unicode_literals
-
 import random
 import re
 import string
 
 from .common import InfoExtractor
-from ..compat import compat_HTTPError
+from ..networking.exceptions import HTTPError
 from ..utils import (
+    ExtractorError,
     determine_ext,
     int_or_none,
     join_nonempty,
     js_to_json,
+    make_archive_id,
     orderedSet,
     qualities,
     str_or_none,
     traverse_obj,
     try_get,
     urlencode_postdata,
-    ExtractorError,
 )
 
 
@@ -48,8 +46,8 @@ def _perform_login(self, username, password):
                 }))
             FunimationBaseIE._TOKEN = data['token']
         except ExtractorError as e:
-            if isinstance(e.cause, compat_HTTPError) and e.cause.code == 401:
-                error = self._parse_json(e.cause.read().decode(), None)['error']
+            if isinstance(e.cause, HTTPError) and e.cause.status == 401:
+                error = self._parse_json(e.cause.response.read().decode(), None)['error']
                 raise ExtractorError(error, expected=True)
             raise
 
@@ -98,7 +96,7 @@ def _real_extract(self, url):
             f'{show}_{episode}', query={
                 'deviceType': 'web',
                 'region': self._REGION,
-                'locale': locale or 'en'
+                'locale': locale or 'en',
             }), ('videoList', ..., 'id'), get_all=False)
 
         return self.url_result(f'https://www.funimation.com/player/{video_id}', FunimationIE.ie_key(), video_id)
@@ -159,7 +157,7 @@ def _get_experiences(episode):
                     yield lang, version.title(), f
 
     def _get_episode(self, webpage, experience_id=None, episode_id=None, fatal=True):
-        ''' Extract the episode, season and show objects given either episode/experience id '''
+        """ Extract the episode, season and show objects given either episode/experience id """
         show = self._parse_json(
             self._search_regex(
                 r'show\s*=\s*({.+?})\s*;', webpage, 'show data', fatal=fatal),
@@ -201,24 +199,24 @@ def _real_extract(self, url):
                 continue
             thumbnails.append({'url': fmt.get('poster')})
             duration = max(duration, fmt.get('duration', 0))
-            format_name = '%s %s (%s)' % (version, lang, experience_id)
+            format_name = f'{version} {lang} ({experience_id})'
             self.extract_subtitles(
                 subtitles, experience_id, display_id=display_id, format_name=format_name,
                 episode=episode if experience_id == initial_experience_id else episode_id)
 
             headers = {}
             if self._TOKEN:
-                headers['Authorization'] = 'Token %s' % self._TOKEN
+                headers['Authorization'] = f'Token {self._TOKEN}'
             page = self._download_json(
-                'https://www.funimation.com/api/showexperience/%s/' % experience_id,
+                f'https://www.funimation.com/api/showexperience/{experience_id}/',
                 display_id, headers=headers, expected_status=403, query={
-                    'pinst_id': ''.join([random.choice(string.digits + string.ascii_letters) for _ in range(8)]),
+                    'pinst_id': ''.join(random.choices(string.digits + string.ascii_letters, k=8)),
                 }, note=f'Downloading {format_name} JSON')
             sources = page.get('items') or []
             if not sources:
                 error = try_get(page, lambda x: x['errors'][0], dict)
                 if error:
-                    self.report_warning('%s said: Error %s - %s' % (
+                    self.report_warning('{} said: Error {} - {}'.format(
                         self.IE_NAME, error.get('code'), error.get('detail') or error.get('title')))
                 else:
                     self.report_warning('No sources found for format')
@@ -229,11 +227,11 @@ def _real_extract(self, url):
                 source_type = source.get('videoType') or determine_ext(source_url)
                 if source_type == 'm3u8':
                     current_formats.extend(self._extract_m3u8_formats(
-                        source_url, display_id, 'mp4', m3u8_id='%s-%s' % (experience_id, 'hls'), fatal=False,
+                        source_url, display_id, 'mp4', m3u8_id='{}-{}'.format(experience_id, 'hls'), fatal=False,
                         note=f'Downloading {format_name} m3u8 information'))
                 else:
                     current_formats.append({
-                        'format_id': '%s-%s' % (experience_id, source_type),
+                        'format_id': f'{experience_id}-{source_type}',
                         'url': source_url,
                     })
                 for f in current_formats:
@@ -245,11 +243,14 @@ def _real_extract(self, url):
                         'language_preference': language_preference(lang.lower()),
                     })
                 formats.extend(current_formats)
+        if not formats and (requested_languages or requested_versions):
+            self.raise_no_formats(
+                'There are no video formats matching the requested languages/versions', expected=True, video_id=display_id)
         self._remove_duplicate_formats(formats)
-        self._sort_formats(formats, ('lang', 'source'))
 
         return {
-            'id': initial_experience_id if only_initial_experience else episode_id,
+            'id': episode_id,
+            '_old_archive_ids': [make_archive_id(self, initial_experience_id)],
             'display_id': display_id,
             'duration': duration,
             'title': episode['episodeTitle'],
@@ -264,6 +265,7 @@ def _real_extract(self, url):
             'formats': formats,
             'thumbnails': thumbnails,
             'subtitles': subtitles,
+            '_format_sort_fields': ('lang', 'source'),
         }
 
     def _get_subtitles(self, subtitles, experience_id, episode, display_id, format_name):
@@ -282,7 +284,7 @@ def _get_subtitles(self, subtitles, experience_id, episode, display_id, format_n
                     sub_type = sub_type if sub_type != 'FULL' else None
                     current_sub = {
                         'url': text_track['src'],
-                        'name': join_nonempty(version, text_track.get('label'), sub_type, delim=' ')
+                        'name': join_nonempty(version, text_track.get('label'), sub_type, delim=' '),
                     }
                     lang = join_nonempty(text_track.get('language', 'und'),
                                          version if version != 'Simulcast' else None,
@@ -299,8 +301,8 @@ class FunimationShowIE(FunimationBaseIE):
     _TESTS = [{
         'url': 'https://www.funimation.com/en/shows/sk8-the-infinity',
         'info_dict': {
-            'id': 1315000,
-            'title': 'SK8 the Infinity'
+            'id': '1315000',
+            'title': 'SK8 the Infinity',
         },
         'playlist_count': 13,
         'params': {
@@ -310,8 +312,8 @@ class FunimationShowIE(FunimationBaseIE):
         # without lang code
         'url': 'https://www.funimation.com/shows/ouran-high-school-host-club/',
         'info_dict': {
-            'id': 39643,
-            'title': 'Ouran High School Host Club'
+            'id': '39643',
+            'title': 'Ouran High School Host Club',
         },
         'playlist_count': 26,
         'params': {
@@ -327,21 +329,21 @@ def _real_extract(self, url):
         base_url, locale, display_id = self._match_valid_url(url).groups()
 
         show_info = self._download_json(
-            'https://title-api.prd.funimationsvc.com/v2/shows/%s?region=%s&deviceType=web&locale=%s'
-            % (display_id, self._REGION, locale or 'en'), display_id)
+            'https://title-api.prd.funimationsvc.com/v2/shows/{}?region={}&deviceType=web&locale={}'.format(
+                display_id, self._REGION, locale or 'en'), display_id)
         items_info = self._download_json(
-            'https://prod-api-funimationnow.dadcdigital.com/api/funimation/episodes/?limit=99999&title_id=%s'
-            % show_info.get('id'), display_id)
+            'https://prod-api-funimationnow.dadcdigital.com/api/funimation/episodes/?limit=99999&title_id={}'.format(
+                show_info.get('id')), display_id)
 
-        vod_items = traverse_obj(items_info, ('items', ..., re.compile('(?i)mostRecent[AS]vod').match, 'item'))
+        vod_items = traverse_obj(items_info, ('items', ..., lambda k, _: re.match(r'(?i)mostRecent[AS]vod', k), 'item'))
 
         return {
             '_type': 'playlist',
-            'id': show_info['id'],
+            'id': str_or_none(show_info['id']),
             'title': show_info['name'],
             'entries': orderedSet(
                 self.url_result(
-                    '%s/%s' % (base_url, vod_item.get('episodeSlug')), FunimationPageIE.ie_key(),
+                    '{}/{}'.format(base_url, vod_item.get('episodeSlug')), FunimationPageIE.ie_key(),
                     vod_item.get('episodeId'), vod_item.get('episodeName'))
                 for vod_item in sorted(vod_items, key=lambda x: x.get('episodeOrder', -1))),
         }