]> jfr.im git - yt-dlp.git/blobdiff - yt_dlp/extractor/funimation.py
[ie/orf:on] Improve extraction (#9677)
[yt-dlp.git] / yt_dlp / extractor / funimation.py
index 382cbe159cb5efc2e432ec92caa7cc659b12176f..c32f005bab7b7e4c8f630727ca23d5749d64c231 100644 (file)
@@ -1,23 +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,
 )
 
 
@@ -35,9 +34,8 @@ def _get_region(self):
                 note='Checking geo-location', errnote='Unable to fetch geo-location information'),
             'region') or 'US'
 
-    def _login(self):
-        username, password = self._get_login_info()
-        if username is None:
+    def _perform_login(self, username, password):
+        if self._TOKEN:
             return
         try:
             data = self._download_json(
@@ -46,10 +44,10 @@ def _login(self):
                     'username': username,
                     'password': password,
                 }))
-            return data['token']
+            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
 
@@ -89,8 +87,6 @@ class FunimationPageIE(FunimationBaseIE):
     def _real_initialize(self):
         if not self._REGION:
             FunimationBaseIE._REGION = self._get_region()
-        if not self._TOKEN:
-            FunimationBaseIE._TOKEN = self._login()
 
     def _real_extract(self, url):
         locale, show, episode = self._match_valid_url(url).group('lang', 'show', 'episode')
@@ -153,10 +149,6 @@ class FunimationIE(FunimationBaseIE):
         },
     }]
 
-    def _real_initialize(self):
-        if not self._TOKEN:
-            FunimationBaseIE._TOKEN = self._login()
-
     @staticmethod
     def _get_experiences(episode):
         for lang, lang_data in episode.get('languages', {}).items():
@@ -218,7 +210,7 @@ def _real_extract(self, url):
             page = self._download_json(
                 'https://www.funimation.com/api/showexperience/%s/' % 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:
@@ -251,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'],
@@ -270,12 +265,13 @@ 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):
         if isinstance(episode, str):
             webpage = self._download_webpage(
-                f'https://www.funimation.com/player/{experience_id}', display_id,
+                f'https://www.funimation.com/player/{experience_id}/', display_id,
                 fatal=False, note=f'Downloading player webpage for {format_name}')
             episode, _, _ = self._get_episode(webpage, episode_id=episode, fatal=False)
 
@@ -288,10 +284,11 @@ 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(filter(None, (version, text_track.get('label'), sub_type)))
+                        'name': join_nonempty(version, text_track.get('label'), sub_type, delim=' ')
                     }
-                    lang = '_'.join(filter(None, (
-                        text_track.get('language', 'und'), version if version != 'Simulcast' else None, sub_type)))
+                    lang = join_nonempty(text_track.get('language', 'und'),
+                                         version if version != 'Simulcast' else None,
+                                         sub_type, delim='_')
                     if current_sub not in subtitles.get(lang, []):
                         subtitles.setdefault(lang, []).append(current_sub)
         return subtitles
@@ -304,7 +301,7 @@ class FunimationShowIE(FunimationBaseIE):
     _TESTS = [{
         'url': 'https://www.funimation.com/en/shows/sk8-the-infinity',
         'info_dict': {
-            'id': 1315000,
+            'id': '1315000',
             'title': 'SK8 the Infinity'
         },
         'playlist_count': 13,
@@ -315,7 +312,7 @@ class FunimationShowIE(FunimationBaseIE):
         # without lang code
         'url': 'https://www.funimation.com/shows/ouran-high-school-host-club/',
         'info_dict': {
-            'id': 39643,
+            'id': '39643',
             'title': 'Ouran High School Host Club'
         },
         'playlist_count': 26,
@@ -338,11 +335,11 @@ def _real_extract(self, url):
             'https://prod-api-funimationnow.dadcdigital.com/api/funimation/episodes/?limit=99999&title_id=%s'
             % 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(