Repository: yt-dlp/yt-dlp
File: yt_dlp/extractor/crunchyroll.py (Python)

Code before:
            basic_token = str(base64.b64encode(('%s:' % client_id).encode('ascii')), 'ascii')
            auth_response = self._download_json(
                f'{api_domain}/auth/v1/token', None,
                note='Authenticating with cookie',
                headers={
                    'Authorization': 'Basic ' + basic_token
                }, data='grant_type=etp_rt_cookie'.encode('ascii'))
            policy_response = self._download_json(
                f'{api_domain}/index/v2', None,
                note='Retrieving signed policy',
                headers={
                    'Authorization': auth_response['token_type'] + ' ' + auth_response['access_token']
                })
            bucket = policy_response['cms']['bucket']
            params = {
                'Policy': policy_response['cms']['policy'],
                'Signature': policy_response['cms']['signature'],
                'Key-Pair-Id': policy_response['cms']['key_pair_id']
            }
            locale = traverse_obj(initial_state, ('localization', 'locale'))
            if locale:
                params['locale'] = locale
            CrunchyrollBetaBaseIE.api_domain = api_domain
            CrunchyrollBetaBaseIE.bucket = bucket
            CrunchyrollBetaBaseIE.params = params
        return CrunchyrollBetaBaseIE.api_domain, CrunchyrollBetaBaseIE.bucket, CrunchyrollBetaBaseIE.params


class CrunchyrollBetaIE(CrunchyrollBetaBaseIE):
    IE_NAME = 'crunchyroll:beta'
    _VALID_URL = r'https?://beta\.crunchyroll\.com/(?P<lang>(?:\w{1,2}/)?)watch/(?P<id>\w+)/(?P<display_id>[\w\-]*)/?(?:\?|$)'
    _TESTS = [{
        'url': 'https://beta.crunchyroll.com/watch/GY2P1Q98Y/to-the-future',
        'info_dict': {
            'id': '696363',
            'ext': 'mp4',
            'timestamp': 1459610100,
            'description': 'md5:a022fbec4fbb023d43631032c91ed64b',
            'uploader': 'Toei Animation',
            'title': 'World Trigger Episode 73 – To the Future',
            'upload_date': '20160402',
        },
        'params': {'skip_download': 'm3u8'},
        'expected_warnings': ['Unable to download XML']
    }, {
        'url': 'https://beta.crunchyroll.com/watch/GY2P1Q98Y/',
        'only_matching': True,
    }]

    def _real_extract(self, url):
        lang, internal_id, display_id = self._match_valid_url(url).group('lang', 'id', 'display_id')

Review comment (suggestion):
```suggestion
            CrunchyrollBetaBaseIE.params = (api_domain, bucket, params)
        return CrunchyrollBetaBaseIE.params
```

Code after:
            params = {
                'Policy': policy_response['cms']['policy'],
                'Signature': policy_response['cms']['signature'],
                'Key-Pair-Id': policy_response['cms']['key_pair_id']
            }
            locale = traverse_obj(initial_state, ('localization', 'locale'))
            if locale:
                params['locale'] = locale
            CrunchyrollBetaBaseIE.params = (api_domain, bucket, params)
        return CrunchyrollBetaBaseIE.params

    def _redirect_from_beta(self, url, lang, internal_id, display_id, is_episode, iekey):
        initial_state, app_config = self._get_beta_embedded_json(self._download_webpage(url, display_id), display_id)
        content_data = initial_state['content']['byId'][internal_id]
        if is_episode:
            video_id = content_data['external_id'].split('.')[1]
            series_id = content_data['episode_metadata']['series_slug_title']
        else:
            series_id = content_data['slug_title']
        series_id = re.sub(r'-{2,}', '-', series_id)
        url = f'https://www.crunchyroll.com/{lang}{series_id}'
        if is_episode:
            url = url + f'/{display_id}-{video_id}'
        self.to_screen(f'{display_id}: Not logged in. Redirecting to non-beta site - {url}')
        return self.url_result(url, iekey, display_id)


class CrunchyrollBetaIE(CrunchyrollBetaBaseIE):
    IE_NAME = 'crunchyroll:beta'
    _VALID_URL = r'https?://beta\.crunchyroll\.com/(?P<lang>(?:\w{1,2}/)?)watch/(?P<id>\w+)/(?P<display_id>[\w\-]*)/?(?:\?|$)'
    _TESTS = [{
        'url': 'https://beta.crunchyroll.com/watch/GY2P1Q98Y/to-the-future',
        'info_dict': {
            'id': '696363',
            'ext': 'mp4',
            'timestamp': 1459610100,
            'description': 'md5:a022fbec4fbb023d43631032c91ed64b',
            'uploader': 'Toei Animation',
            'title': 'World Trigger Episode 73 – To the Future',
            'upload_date': '20160402',
            'episode_number': 73,
            'series': 'World Trigger',
            'average_rating': 4.9,
            'episode': 'To the Future',
            'season': 'World Trigger',
            'thumbnail': 'https://img1.ak.crunchyroll.com/i/spire3-tmb/c870dedca1a83137c2d3d144984155ed1459527119_main.jpg',
            'season_number': 1,
        },
        'params': {'skip_download': 'm3u8'},
        'expected_warnings': ['Unable to download XML']
    }, {