2022-01-28 19:33:51 -05:00
|
|
|
|
import base64
|
2022-11-06 13:48:55 -05:00
|
|
|
|
import urllib.parse
|
2022-06-24 04:10:17 -04:00
|
|
|
|
|
2018-09-01 05:04:10 -04:00
|
|
|
|
from .common import InfoExtractor
|
2014-12-13 06:24:42 -05:00
|
|
|
|
from ..utils import (
|
|
|
|
|
ExtractorError,
|
2018-09-01 03:16:28 -04:00
|
|
|
|
float_or_none,
|
2022-01-28 19:33:51 -05:00
|
|
|
|
format_field,
|
|
|
|
|
join_nonempty,
|
2022-08-02 17:18:40 -04:00
|
|
|
|
parse_iso8601,
|
2021-11-04 14:04:37 -04:00
|
|
|
|
qualities,
|
2022-01-28 19:33:51 -05:00
|
|
|
|
traverse_obj,
|
2021-08-09 17:39:20 -04:00
|
|
|
|
try_get,
|
2013-11-03 21:08:17 -05:00
|
|
|
|
)
|
|
|
|
|
|
2014-02-25 08:26:11 -05:00
|
|
|
|
|
2018-09-01 05:04:10 -04:00
|
|
|
|
class CrunchyrollBaseIE(InfoExtractor):
|
2022-01-31 15:01:17 -05:00
|
|
|
|
_LOGIN_URL = 'https://www.crunchyroll.com/welcome/login'
|
|
|
|
|
_API_BASE = 'https://api.crunchyroll.com'
|
2015-10-17 20:57:57 -04:00
|
|
|
|
_NETRC_MACHINE = 'crunchyroll'
|
2022-11-06 13:48:55 -05:00
|
|
|
|
params = None
|
2017-11-13 13:15:16 -05:00
|
|
|
|
|
2022-03-18 16:53:33 -04:00
|
|
|
|
def _perform_login(self, username, password):
|
2022-01-31 15:01:17 -05:00
|
|
|
|
if self._get_cookies(self._LOGIN_URL).get('etp_rt'):
|
2016-09-15 10:53:35 -04:00
|
|
|
|
return
|
|
|
|
|
|
2022-01-31 15:01:17 -05:00
|
|
|
|
upsell_response = self._download_json(
|
|
|
|
|
f'{self._API_BASE}/get_upsell_data.0.json', None, 'Getting session id',
|
|
|
|
|
query={
|
|
|
|
|
'sess_id': 1,
|
|
|
|
|
'device_id': 'whatvalueshouldbeforweb',
|
|
|
|
|
'device_type': 'com.crunchyroll.static',
|
|
|
|
|
'access_token': 'giKq5eY27ny3cqz',
|
|
|
|
|
'referer': self._LOGIN_URL
|
|
|
|
|
})
|
|
|
|
|
if upsell_response['code'] != 'ok':
|
|
|
|
|
raise ExtractorError('Could not get session id')
|
|
|
|
|
session_id = upsell_response['data']['session_id']
|
|
|
|
|
|
|
|
|
|
login_response = self._download_json(
|
|
|
|
|
f'{self._API_BASE}/login.1.json', None, 'Logging in',
|
2022-11-06 13:48:55 -05:00
|
|
|
|
data=urllib.parse.urlencode({
|
2022-01-31 15:01:17 -05:00
|
|
|
|
'account': username,
|
|
|
|
|
'password': password,
|
|
|
|
|
'session_id': session_id
|
|
|
|
|
}).encode('ascii'))
|
|
|
|
|
if login_response['code'] != 'ok':
|
2022-03-04 06:27:35 -05:00
|
|
|
|
raise ExtractorError('Login failed. Server message: %s' % login_response['message'], expected=True)
|
2022-01-31 15:01:17 -05:00
|
|
|
|
if not self._get_cookies(self._LOGIN_URL).get('etp_rt'):
|
|
|
|
|
raise ExtractorError('Login succeeded but did not set etp_rt cookie')
|
2015-10-17 20:57:57 -04:00
|
|
|
|
|
2022-11-06 13:48:55 -05:00
|
|
|
|
def _get_embedded_json(self, webpage, display_id):
|
2022-04-05 06:51:12 -04:00
|
|
|
|
initial_state = self._parse_json(self._search_regex(
|
|
|
|
|
r'__INITIAL_STATE__\s*=\s*({.+?})\s*;', webpage, 'initial state'), display_id)
|
|
|
|
|
app_config = self._parse_json(self._search_regex(
|
|
|
|
|
r'__APP_CONFIG__\s*=\s*({.+?})\s*;', webpage, 'app config'), display_id)
|
|
|
|
|
return initial_state, app_config
|
|
|
|
|
|
|
|
|
|
def _get_params(self, lang):
|
2022-11-06 13:48:55 -05:00
|
|
|
|
if not CrunchyrollBaseIE.params:
|
|
|
|
|
if self._get_cookies(f'https://www.crunchyroll.com/{lang}').get('etp_rt'):
|
2022-08-30 12:34:13 -04:00
|
|
|
|
grant_type, key = 'etp_rt_cookie', 'accountAuthClientId'
|
|
|
|
|
else:
|
|
|
|
|
grant_type, key = 'client_id', 'anonClientId'
|
|
|
|
|
|
2022-11-06 13:48:55 -05:00
|
|
|
|
initial_state, app_config = self._get_embedded_json(self._download_webpage(
|
|
|
|
|
f'https://www.crunchyroll.com/{lang}', None, note='Retrieving main page'), None)
|
|
|
|
|
api_domain = app_config['cxApiParams']['apiDomain'].replace('beta.crunchyroll.com', 'www.crunchyroll.com')
|
2022-08-30 12:34:13 -04:00
|
|
|
|
|
2022-04-05 06:51:12 -04:00
|
|
|
|
auth_response = self._download_json(
|
2022-08-30 12:34:13 -04:00
|
|
|
|
f'{api_domain}/auth/v1/token', None, note=f'Authenticating with grant_type={grant_type}',
|
2022-04-05 06:51:12 -04:00
|
|
|
|
headers={
|
2022-08-30 12:34:13 -04:00
|
|
|
|
'Authorization': 'Basic ' + str(base64.b64encode(('%s:' % app_config['cxApiParams'][key]).encode('ascii')), 'ascii')
|
|
|
|
|
}, data=f'grant_type={grant_type}'.encode('ascii'))
|
2022-04-05 06:51:12 -04:00
|
|
|
|
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']
|
|
|
|
|
})
|
2022-11-06 13:48:55 -05:00
|
|
|
|
cms = policy_response.get('cms_web')
|
2022-05-19 20:37:04 -04:00
|
|
|
|
bucket = cms['bucket']
|
2022-04-05 06:51:12 -04:00
|
|
|
|
params = {
|
2022-05-19 20:37:04 -04:00
|
|
|
|
'Policy': cms['policy'],
|
|
|
|
|
'Signature': cms['signature'],
|
|
|
|
|
'Key-Pair-Id': cms['key_pair_id']
|
2022-04-05 06:51:12 -04:00
|
|
|
|
}
|
|
|
|
|
locale = traverse_obj(initial_state, ('localization', 'locale'))
|
|
|
|
|
if locale:
|
|
|
|
|
params['locale'] = locale
|
2022-11-06 13:48:55 -05:00
|
|
|
|
CrunchyrollBaseIE.params = (api_domain, bucket, params)
|
|
|
|
|
return CrunchyrollBaseIE.params
|
2022-04-05 06:51:12 -04:00
|
|
|
|
|
|
|
|
|
|
2022-11-06 13:48:55 -05:00
|
|
|
|
class CrunchyrollBetaIE(CrunchyrollBaseIE):
|
|
|
|
|
IE_NAME = 'crunchyroll'
|
2022-08-12 03:38:32 -04:00
|
|
|
|
_VALID_URL = r'''(?x)
|
2022-11-06 13:48:55 -05:00
|
|
|
|
https?://(?:beta|www)\.crunchyroll\.com/
|
2022-08-12 03:38:32 -04:00
|
|
|
|
(?P<lang>(?:\w{2}(?:-\w{2})?/)?)
|
|
|
|
|
watch/(?P<id>\w+)
|
|
|
|
|
(?:/(?P<display_id>[\w-]+))?/?(?:[?#]|$)'''
|
2021-10-17 07:46:05 -04:00
|
|
|
|
_TESTS = [{
|
2022-11-06 13:48:55 -05:00
|
|
|
|
'url': 'https://www.crunchyroll.com/watch/GY2P1Q98Y/to-the-future',
|
2021-10-17 07:46:05 -04:00
|
|
|
|
'info_dict': {
|
2022-08-02 17:18:40 -04:00
|
|
|
|
'id': 'GY2P1Q98Y',
|
2021-10-17 07:46:05 -04:00
|
|
|
|
'ext': 'mp4',
|
2022-08-02 17:18:40 -04:00
|
|
|
|
'duration': 1380.241,
|
|
|
|
|
'timestamp': 1459632600,
|
2021-10-17 07:46:05 -04:00
|
|
|
|
'description': 'md5:a022fbec4fbb023d43631032c91ed64b',
|
|
|
|
|
'title': 'World Trigger Episode 73 – To the Future',
|
|
|
|
|
'upload_date': '20160402',
|
2022-04-05 06:51:12 -04:00
|
|
|
|
'series': 'World Trigger',
|
2022-08-02 17:18:40 -04:00
|
|
|
|
'series_id': 'GR757DMKY',
|
2022-04-05 06:51:12 -04:00
|
|
|
|
'season': 'World Trigger',
|
2022-08-02 17:18:40 -04:00
|
|
|
|
'season_id': 'GR9P39NJ6',
|
2022-04-05 06:51:12 -04:00
|
|
|
|
'season_number': 1,
|
2022-08-02 17:18:40 -04:00
|
|
|
|
'episode': 'To the Future',
|
|
|
|
|
'episode_number': 73,
|
2022-11-06 13:48:55 -05:00
|
|
|
|
'thumbnail': r're:^https://www.crunchyroll.com/imgsrv/.*\.jpeg$',
|
2021-10-17 07:46:05 -04:00
|
|
|
|
},
|
2022-09-29 21:05:44 -04:00
|
|
|
|
'params': {'skip_download': 'm3u8', 'format': 'all[format_id~=hardsub]'},
|
|
|
|
|
}, {
|
2022-11-06 13:48:55 -05:00
|
|
|
|
'url': 'https://www.crunchyroll.com/watch/GYE5WKQGR',
|
2022-09-29 21:05:44 -04:00
|
|
|
|
'info_dict': {
|
|
|
|
|
'id': 'GYE5WKQGR',
|
|
|
|
|
'ext': 'mp4',
|
|
|
|
|
'duration': 366.459,
|
|
|
|
|
'timestamp': 1476788400,
|
|
|
|
|
'description': 'md5:74b67283ffddd75f6e224ca7dc031e76',
|
|
|
|
|
'title': 'SHELTER Episode – Porter Robinson presents Shelter the Animation',
|
|
|
|
|
'upload_date': '20161018',
|
|
|
|
|
'series': 'SHELTER',
|
|
|
|
|
'series_id': 'GYGG09WWY',
|
|
|
|
|
'season': 'SHELTER',
|
|
|
|
|
'season_id': 'GR09MGK4R',
|
|
|
|
|
'season_number': 1,
|
|
|
|
|
'episode': 'Porter Robinson presents Shelter the Animation',
|
|
|
|
|
'episode_number': 0,
|
2022-11-06 13:48:55 -05:00
|
|
|
|
'thumbnail': r're:^https://www.crunchyroll.com/imgsrv/.*\.jpeg$',
|
2022-09-29 21:05:44 -04:00
|
|
|
|
},
|
|
|
|
|
'params': {'skip_download': True},
|
|
|
|
|
'skip': 'Video is Premium only',
|
2022-04-05 06:51:12 -04:00
|
|
|
|
}, {
|
2022-11-06 13:48:55 -05:00
|
|
|
|
'url': 'https://www.crunchyroll.com/watch/GY2P1Q98Y',
|
2022-04-05 06:51:12 -04:00
|
|
|
|
'only_matching': True,
|
2022-07-27 06:41:15 -04:00
|
|
|
|
}, {
|
|
|
|
|
'url': 'https://beta.crunchyroll.com/pt-br/watch/G8WUN8VKP/the-ruler-of-conspiracy',
|
|
|
|
|
'only_matching': True,
|
2021-10-17 07:46:05 -04:00
|
|
|
|
}]
|
|
|
|
|
|
|
|
|
|
def _real_extract(self, url):
|
2022-04-05 06:51:12 -04:00
|
|
|
|
lang, internal_id, display_id = self._match_valid_url(url).group('lang', 'id', 'display_id')
|
|
|
|
|
api_domain, bucket, params = self._get_params(lang)
|
|
|
|
|
|
2022-01-28 19:33:51 -05:00
|
|
|
|
episode_response = self._download_json(
|
|
|
|
|
f'{api_domain}/cms/v2{bucket}/episodes/{internal_id}', display_id,
|
2022-07-25 23:41:52 -04:00
|
|
|
|
note='Retrieving episode metadata', query=params)
|
2022-01-28 19:33:51 -05:00
|
|
|
|
if episode_response.get('is_premium_only') and not episode_response.get('playback'):
|
|
|
|
|
raise ExtractorError('This video is for premium members only.', expected=True)
|
|
|
|
|
|
2022-08-04 14:05:58 -04:00
|
|
|
|
stream_response = self._download_json(
|
|
|
|
|
f'{api_domain}{episode_response["__links__"]["streams"]["href"]}', display_id,
|
|
|
|
|
note='Retrieving stream info', query=params)
|
2022-07-25 23:41:52 -04:00
|
|
|
|
get_streams = lambda name: (traverse_obj(stream_response, name) or {}).items()
|
2022-01-28 19:33:51 -05:00
|
|
|
|
|
|
|
|
|
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']
|
|
|
|
|
|
2022-09-29 21:05:44 -04:00
|
|
|
|
available_formats = {}
|
2022-07-25 23:41:52 -04:00
|
|
|
|
for stream_type, streams in get_streams('streams'):
|
2022-01-28 19:33:51 -05:00
|
|
|
|
if stream_type not in requested_formats:
|
|
|
|
|
continue
|
|
|
|
|
for stream in streams.values():
|
|
|
|
|
if not stream.get('url'):
|
|
|
|
|
continue
|
2022-09-29 21:05:44 -04:00
|
|
|
|
hardsub_lang = stream.get('hardsub_locale') or ''
|
|
|
|
|
format_id = join_nonempty(stream_type, format_field(stream, 'hardsub_locale', 'hardsub-%s'))
|
|
|
|
|
available_formats[hardsub_lang] = (stream_type, format_id, hardsub_lang, stream['url'])
|
|
|
|
|
|
|
|
|
|
if '' in available_formats and 'all' not in requested_hardsubs:
|
|
|
|
|
full_format_langs = set(requested_hardsubs)
|
|
|
|
|
self.to_screen(
|
|
|
|
|
'To get all formats of a hardsub language, use '
|
|
|
|
|
'"--extractor-args crunchyrollbeta:hardsub=<language_code or all>". '
|
|
|
|
|
'See https://github.com/yt-dlp/yt-dlp#crunchyrollbeta for more info',
|
|
|
|
|
only_once=True)
|
|
|
|
|
else:
|
|
|
|
|
full_format_langs = set(map(str.lower, available_formats))
|
|
|
|
|
|
|
|
|
|
formats = []
|
|
|
|
|
for stream_type, format_id, hardsub_lang, stream_url in available_formats.values():
|
|
|
|
|
if stream_type.endswith('hls'):
|
|
|
|
|
if hardsub_lang.lower() in full_format_langs:
|
2022-01-28 19:33:51 -05:00
|
|
|
|
adaptive_formats = self._extract_m3u8_formats(
|
2022-09-29 21:05:44 -04:00
|
|
|
|
stream_url, display_id, 'mp4', m3u8_id=format_id,
|
2022-07-25 23:41:52 -04:00
|
|
|
|
fatal=False, note=f'Downloading {format_id} HLS manifest')
|
2022-09-29 21:05:44 -04:00
|
|
|
|
else:
|
|
|
|
|
adaptive_formats = (self._m3u8_meta_format(stream_url, ext='mp4', m3u8_id=format_id),)
|
|
|
|
|
elif stream_type.endswith('dash'):
|
|
|
|
|
adaptive_formats = self._extract_mpd_formats(
|
|
|
|
|
stream_url, display_id, mpd_id=format_id,
|
|
|
|
|
fatal=False, note=f'Downloading {format_id} MPD manifest')
|
|
|
|
|
else:
|
|
|
|
|
self.report_warning(f'Encountered unknown stream_type: {stream_type!r}', display_id, only_once=True)
|
|
|
|
|
continue
|
|
|
|
|
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)
|
2022-01-28 19:33:51 -05:00
|
|
|
|
self._sort_formats(formats)
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
'id': internal_id,
|
2022-07-25 23:41:52 -04:00
|
|
|
|
'title': '%s Episode %s – %s' % (
|
|
|
|
|
episode_response.get('season_title'), episode_response.get('episode'), episode_response.get('title')),
|
|
|
|
|
'description': try_get(episode_response, lambda x: x['description'].replace(r'\r\n', '\n')),
|
2022-01-28 19:33:51 -05:00
|
|
|
|
'duration': float_or_none(episode_response.get('duration_ms'), 1000),
|
2022-08-02 17:18:40 -04:00
|
|
|
|
'timestamp': parse_iso8601(episode_response.get('upload_date')),
|
2022-01-28 19:33:51 -05:00
|
|
|
|
'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'),
|
2022-07-25 23:41:52 -04:00
|
|
|
|
'formats': formats,
|
|
|
|
|
'thumbnails': [{
|
|
|
|
|
'url': thumb.get('source'),
|
|
|
|
|
'width': thumb.get('width'),
|
|
|
|
|
'height': thumb.get('height'),
|
|
|
|
|
} for thumb in traverse_obj(episode_response, ('images', 'thumbnail', ..., ...)) or []],
|
|
|
|
|
'subtitles': {
|
|
|
|
|
lang: [{
|
|
|
|
|
'url': subtitle_data.get('url'),
|
|
|
|
|
'ext': subtitle_data.get('format')
|
|
|
|
|
}] for lang, subtitle_data in get_streams('subtitles')
|
|
|
|
|
},
|
2022-01-28 19:33:51 -05:00
|
|
|
|
}
|
2021-10-17 07:46:05 -04:00
|
|
|
|
|
|
|
|
|
|
2022-11-06 13:48:55 -05:00
|
|
|
|
class CrunchyrollBetaShowIE(CrunchyrollBaseIE):
|
|
|
|
|
IE_NAME = 'crunchyroll:playlist'
|
2022-08-12 03:38:32 -04:00
|
|
|
|
_VALID_URL = r'''(?x)
|
2022-11-06 13:48:55 -05:00
|
|
|
|
https?://(?:beta|www)\.crunchyroll\.com/
|
2022-08-12 03:38:32 -04:00
|
|
|
|
(?P<lang>(?:\w{2}(?:-\w{2})?/)?)
|
|
|
|
|
series/(?P<id>\w+)
|
|
|
|
|
(?:/(?P<display_id>[\w-]+))?/?(?:[?#]|$)'''
|
2021-10-17 07:46:05 -04:00
|
|
|
|
_TESTS = [{
|
2022-11-06 13:48:55 -05:00
|
|
|
|
'url': 'https://www.crunchyroll.com/series/GY19NQ2QR/Girl-Friend-BETA',
|
2021-10-17 07:46:05 -04:00
|
|
|
|
'info_dict': {
|
2022-08-02 17:18:40 -04:00
|
|
|
|
'id': 'GY19NQ2QR',
|
2021-10-17 07:46:05 -04:00
|
|
|
|
'title': 'Girl Friend BETA',
|
|
|
|
|
},
|
|
|
|
|
'playlist_mincount': 10,
|
|
|
|
|
}, {
|
2022-08-12 03:38:32 -04:00
|
|
|
|
'url': 'https://beta.crunchyroll.com/it/series/GY19NQ2QR',
|
2021-10-17 07:46:05 -04:00
|
|
|
|
'only_matching': True,
|
|
|
|
|
}]
|
|
|
|
|
|
|
|
|
|
def _real_extract(self, url):
|
2022-04-05 06:51:12 -04:00
|
|
|
|
lang, internal_id, display_id = self._match_valid_url(url).group('lang', 'id', 'display_id')
|
|
|
|
|
api_domain, bucket, params = self._get_params(lang)
|
|
|
|
|
|
|
|
|
|
series_response = self._download_json(
|
|
|
|
|
f'{api_domain}/cms/v2{bucket}/series/{internal_id}', display_id,
|
|
|
|
|
note='Retrieving series metadata', query=params)
|
|
|
|
|
|
|
|
|
|
seasons_response = self._download_json(
|
|
|
|
|
f'{api_domain}/cms/v2{bucket}/seasons?series_id={internal_id}', display_id,
|
|
|
|
|
note='Retrieving season list', query=params)
|
|
|
|
|
|
|
|
|
|
def entries():
|
|
|
|
|
for season in seasons_response['items']:
|
|
|
|
|
episodes_response = self._download_json(
|
|
|
|
|
f'{api_domain}/cms/v2{bucket}/episodes?season_id={season["id"]}', display_id,
|
|
|
|
|
note=f'Retrieving episode list for {season.get("slug_title")}', query=params)
|
|
|
|
|
for episode in episodes_response['items']:
|
|
|
|
|
episode_id = episode['id']
|
|
|
|
|
episode_display_id = episode['slug_title']
|
|
|
|
|
yield {
|
|
|
|
|
'_type': 'url',
|
2022-11-06 13:48:55 -05:00
|
|
|
|
'url': f'https://www.crunchyroll.com/{lang}watch/{episode_id}/{episode_display_id}',
|
2022-04-05 06:51:12 -04:00
|
|
|
|
'ie_key': CrunchyrollBetaIE.ie_key(),
|
|
|
|
|
'id': episode_id,
|
|
|
|
|
'title': '%s Episode %s – %s' % (episode.get('season_title'), episode.get('episode'), episode.get('title')),
|
|
|
|
|
'description': try_get(episode, lambda x: x['description'].replace(r'\r\n', '\n')),
|
|
|
|
|
'duration': float_or_none(episode.get('duration_ms'), 1000),
|
|
|
|
|
'series': episode.get('series_title'),
|
|
|
|
|
'series_id': episode.get('series_id'),
|
|
|
|
|
'season': episode.get('season_title'),
|
|
|
|
|
'season_id': episode.get('season_id'),
|
|
|
|
|
'season_number': episode.get('season_number'),
|
|
|
|
|
'episode': episode.get('title'),
|
|
|
|
|
'episode_number': episode.get('sequence_number')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return self.playlist_result(entries(), internal_id, series_response.get('title'))
|