Skip to content

Commit

Permalink
add crunchyroll public account
Browse files Browse the repository at this point in the history
  • Loading branch information
justin025 committed Feb 14, 2025
1 parent 0e1f178 commit 25465aa
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 21 deletions.
65 changes: 44 additions & 21 deletions src/onthespot/api/crunchyroll.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,41 @@

def crunchyroll_login_user(account):
try:
headers = {}
headers['Authorization'] = f'Basic {PUBLIC_TOKEN}'
headers['Connection'] = 'Keep-Alive'
headers['Content-Type'] = 'application/x-www-form-urlencoded'
headers['User-Agent'] = f'Crunchyroll/{APP_VERSION} Android/13 okhttp/4.12.0'

payload = {}
payload['username'] = account['login']['email']
payload['password'] = account['login']['password']
payload['grant_type'] = 'password'
payload['scope'] = 'offline_access'
payload['device_id'] = account['uuid']
payload['device_name'] = 'OnTheSpot'
payload['device_type'] = 'OnTheSpot'
if account['uuid'] == 'public_crunchyroll':
response = requests.get("https://static.crunchyroll.com/vilos-v2/web/vilos/js/bundle.js")
tokens = re.search(r'prod="([\w-]+:[\w-]+)",\w+\.staging="([\w-]+:[\w-]+)",\w+\.proto0="([\w-]+:[\w-]+)"', response.text)
if not tokens:
raise ValueError("Couldn't find tokens.")

prod, staging, proto = tokens.groups()
token = base64.b64encode(prod.encode("iso-8859-1")).decode()

headers = {}
headers['Authorization'] = f'Basic {token}'
headers['Content-Type'] = 'application/x-www-form-urlencoded'
headers['ETP-Anonymous-ID'] = str(uuid4())

payload = {}
payload['grant_type'] = 'client_id'

username = 'public_crunchyroll'
else:
headers = {}
headers['Authorization'] = f'Basic {PUBLIC_TOKEN}'
headers['Connection'] = 'Keep-Alive'
headers['Content-Type'] = 'application/x-www-form-urlencoded'
headers['User-Agent'] = f'Crunchyroll/{APP_VERSION} Android/13 okhttp/4.12.0'

payload = {}
payload['username'] = account['login']['email']
payload['password'] = account['login']['password']
payload['grant_type'] = 'password'
payload['scope'] = 'offline_access'
payload['device_id'] = account['uuid']
payload['device_name'] = 'OnTheSpot'
payload['device_type'] = 'OnTheSpot'

username = account['login']['email']

token_data = requests.post(f"{BASE_URL}/auth/v1/token", headers=headers, data=payload).json()
token = token_data.get('access_token')
Expand All @@ -45,16 +66,18 @@ def crunchyroll_login_user(account):

header_b64, payload_b64, signature_b64 = token.split('.')
jwt_data = json.loads(base64.urlsafe_b64decode(f'{payload_b64}==='))
premium = False
if 'cr_premium' in jwt_data['benefits']:
premium = True
account_type = 'free'
if jwt_data.get('status') == 'ANONYMOUS':
account_type = 'public'
elif 'cr_premium' in jwt_data.get('benefits', {}):
account_type = 'premium'

account_pool.append({
"uuid": account['uuid'],
"username": account['login']['email'],
"username": username,
"service": "crunchyroll",
"status": "active",
"account_type": 'premium' if premium else 'free',
"account_type": account_type,
"bitrate": "1080p",
"login": {
"email": account['login']['email'],
Expand Down Expand Up @@ -88,7 +111,7 @@ def crunchyroll_login_user(account):
def crunchyroll_add_account(email, password):
cfg_copy = config.get('accounts').copy()
new_user = {
"uuid": str(uuid.uuid4()),
"uuid": str(uuid4()),
"service": "crunchyroll",
"active": True,
"login": {
Expand Down Expand Up @@ -187,7 +210,7 @@ def crunchyroll_get_episode_metadata(token, item_id):
info_dict = episode_data['data'][0]
# Doesn't seem to work with android bearer.
#genre_data = make_call(f'{BASE_URL}/content/v2/discover/categories?guid={item_id.split("/")[0]}&locale=en-US', headers=headers)
# Headers not required, 403 means data does not exist
# Headers not required, 403 means data does not exist or more likely crunchyroll owns the rights to the media.
copyright_data = make_call(f'https://static.crunchyroll.com/copyright/{item_id.split("/")[0]}.json')
# intro and credit timestamps (done in downloader step)
#https://static.crunchyroll.com/skip-events/production/G4VUQ588P.json
Expand Down
5 changes: 5 additions & 0 deletions src/onthespot/otsconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ def __init__(self, cfg_path=None):
"service": "youtube_music",
"active": True,
},
{
"uuid": "public_crunchyroll",
"service": "crunchyroll",
"active": True,
},
], # Saved account information

# Web UI Settings
Expand Down

0 comments on commit 25465aa

Please sign in to comment.