Skip to content

Commit

Permalink
[crunchyroll:beta] Use anonymous access instead of redirecting
Browse files Browse the repository at this point in the history
  • Loading branch information
tejing1 committed Aug 19, 2022
1 parent b76e9ce commit 72200e6
Showing 1 changed file with 16 additions and 27 deletions.
43 changes: 16 additions & 27 deletions yt_dlp/extractor/crunchyroll.py
Expand Up @@ -720,15 +720,27 @@ class CrunchyrollBetaBaseIE(CrunchyrollBaseIE):

def _get_params(self, lang):
if not CrunchyrollBetaBaseIE.params:
if self._get_cookies(f'https://beta.crunchyroll.com/{lang}').get('etp_rt'):
auth = 'cookie'
else:
auth = 'client id'

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')

if auth == 'client id':
client_id = app_config['cxApiParams']['anonClientId']
grant_type = 'client_id'
else:
client_id = app_config['cxApiParams']['accountAuthClientId']
grant_type = 'etp_rt_cookie'

auth_response = self._download_json(
f'{api_domain}/auth/v1/token', None, note='Authenticating with cookie',
f'{api_domain}/auth/v1/token', None, note=f'Authenticating with {auth}',
headers={
'Authorization': 'Basic ' + basic_token
}, data='grant_type=etp_rt_cookie'.encode('ascii'))
'Authorization': 'Basic ' + str(base64.b64encode(('%s:' % client_id).encode('ascii')), 'ascii')
}, data=f'grant_type={grant_type}'.encode('ascii'))
policy_response = self._download_json(
f'{api_domain}/index/v2', None, note='Retrieving signed policy',
headers={
Expand All @@ -747,21 +759,6 @@ def _get_params(self, lang):
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'
Expand Down Expand Up @@ -800,10 +797,6 @@ class CrunchyrollBetaIE(CrunchyrollBetaBaseIE):

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(
Expand Down Expand Up @@ -897,10 +890,6 @@ class CrunchyrollBetaShowIE(CrunchyrollBetaBaseIE):

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, False, CrunchyrollShowPlaylistIE.ie_key())

api_domain, bucket, params = self._get_params(lang)

series_response = self._download_json(
Expand Down

0 comments on commit 72200e6

Please sign in to comment.