+ 'entries': reversed(entries),
+ }
+
+
+class CrunchyrollBetaBaseIE(CrunchyrollBaseIE):
+ params = None
+
+ def _get_params(self, lang):
+ if not CrunchyrollBetaBaseIE.params:
+ initial_state, app_config = self._get_beta_embedded_json(self._download_webpage(
+ f'https://beta.crunchyroll.com/{lang}', None, note='Retrieving main page'), None)
+ api_domain = app_config['cxApiParams']['apiDomain']
+ basic_token = str(base64.b64encode(('%s:' % app_config['cxApiParams']['accountAuthClientId']).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']
+ })
+ cms = traverse_obj(policy_response, 'cms_beta', 'cms')
+ bucket = cms['bucket']
+ params = {
+ 'Policy': cms['policy'],
+ 'Signature': cms['signature'],
+ 'Key-Pair-Id': 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']
+ }, {
+ 'url': 'https://beta.crunchyroll.com/watch/GYK53DMPR/wicked-lord-shingan-reborn',
+ 'info_dict': {
+ 'id': '648781',
+ 'ext': 'mp4',
+ 'episode_number': 1,
+ 'timestamp': 1389173400,
+ 'series': 'Love, Chunibyo & Other Delusions - Heart Throb -',
+ 'description': 'md5:5579d1a0355cc618558ba23d27067a62',
+ 'uploader': 'TBS',
+ 'episode': 'Wicked Lord Shingan... Reborn',
+ 'average_rating': 4.9,
+ 'season': 'Love, Chunibyo & Other Delusions - Heart Throb -',
+ 'thumbnail': 'https://img1.ak.crunchyroll.com/i/spire3-tmb/2ba0384e225a5370d5f0ee9496d91ea51389046521_main.jpg',
+ 'title': 'Love, Chunibyo & Other Delusions - Heart Throb - Episode 1 – Wicked Lord Shingan... Reborn',
+ 'season_number': 2,
+ 'upload_date': '20140108',
+ },
+ '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')
+
+ if not self._get_cookies(url).get('etp_rt'):
+ return self._redirect_from_beta(url, lang, internal_id, display_id, True, CrunchyrollIE.ie_key())
+
+ api_domain, bucket, params = self._get_params(lang)
+
+ episode_response = self._download_json(
+ f'{api_domain}/cms/v2{bucket}/episodes/{internal_id}', display_id,
+ note='Retrieving episode metadata',
+ query=params)
+ if episode_response.get('is_premium_only') and not episode_response.get('playback'):
+ raise ExtractorError('This video is for premium members only.', expected=True)
+ stream_response = self._download_json(
+ episode_response['playback'], display_id,
+ note='Retrieving stream info')
+
+ thumbnails = []
+ for thumbnails_data in traverse_obj(episode_response, ('images', 'thumbnail')):
+ for thumbnail_data in thumbnails_data:
+ thumbnails.append({
+ 'url': thumbnail_data.get('source'),
+ 'width': thumbnail_data.get('width'),
+ 'height': thumbnail_data.get('height'),
+ })
+ subtitles = {}
+ for lang, subtitle_data in stream_response.get('subtitles').items():
+ subtitles[lang] = [{
+ 'url': subtitle_data.get('url'),
+ 'ext': subtitle_data.get('format')
+ }]
+
+ requested_hardsubs = [('' if val == 'none' else val) for val in (self._configuration_arg('hardsub') or ['none'])]
+ hardsub_preference = qualities(requested_hardsubs[::-1])
+ requested_formats = self._configuration_arg('format') or ['adaptive_hls']
+
+ formats = []
+ for stream_type, streams in stream_response.get('streams', {}).items():
+ if stream_type not in requested_formats:
+ continue
+ for stream in streams.values():
+ hardsub_lang = stream.get('hardsub_locale') or ''
+ if hardsub_lang.lower() not in requested_hardsubs:
+ continue
+ format_id = join_nonempty(
+ stream_type,
+ format_field(stream, 'hardsub_locale', 'hardsub-%s'))
+ if not stream.get('url'):
+ continue
+ if stream_type.split('_')[-1] == 'hls':
+ adaptive_formats = self._extract_m3u8_formats(
+ stream['url'], display_id, 'mp4', m3u8_id=format_id,
+ note='Downloading %s information' % format_id,
+ fatal=False)
+ elif stream_type.split('_')[-1] == 'dash':
+ adaptive_formats = self._extract_mpd_formats(
+ stream['url'], display_id, mpd_id=format_id,
+ note='Downloading %s information' % format_id,
+ fatal=False)
+ for f in adaptive_formats:
+ if f.get('acodec') != 'none':
+ f['language'] = stream_response.get('audio_locale')
+ f['quality'] = hardsub_preference(hardsub_lang.lower())
+ formats.extend(adaptive_formats)
+ self._sort_formats(formats)
+
+ return {
+ 'id': internal_id,
+ 'title': '%s Episode %s – %s' % (episode_response.get('season_title'), episode_response.get('episode'), episode_response.get('title')),
+ 'description': episode_response.get('description').replace(r'\r\n', '\n'),
+ 'duration': float_or_none(episode_response.get('duration_ms'), 1000),
+ 'thumbnails': thumbnails,
+ 'series': episode_response.get('series_title'),
+ 'series_id': episode_response.get('series_id'),
+ 'season': episode_response.get('season_title'),
+ 'season_id': episode_response.get('season_id'),
+ 'season_number': episode_response.get('season_number'),
+ 'episode': episode_response.get('title'),
+ 'episode_number': episode_response.get('sequence_number'),
+ 'subtitles': subtitles,
+ 'formats': formats