]> jfr.im git - yt-dlp.git/blobdiff - yt_dlp/extractor/funimation.py
[ie/crunchyroll] Fix stream extraction (#10005)
[yt-dlp.git] / yt_dlp / extractor / funimation.py
index 96dad2ca3451fcfcb2f4428ff549fa25b566d61c..c32f005bab7b7e4c8f630727ca23d5749d64c231 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,
 )
 
 
@@ -36,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(
@@ -47,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
 
@@ -90,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')
@@ -154,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():
@@ -219,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:
@@ -252,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'],
@@ -271,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):
@@ -306,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,
@@ -317,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,
@@ -340,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(