From eca8650f4132707a05a93aa118817d25af345473 Mon Sep 17 00:00:00 2001 From: James Taylor Date: Mon, 1 May 2023 21:11:53 -0700 Subject: [PATCH] Revert to android URLs and fix 403s by including params Including 'params': '8AEB' fixes the issue with the URLs returning 403 after a couple minutes into the video. Credit to @ImportTaste for pointing this out Closes #168 --- youtube/watch.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/youtube/watch.py b/youtube/watch.py index 85a3f32f..a56b9fdd 100644 --- a/youtube/watch.py +++ b/youtube/watch.py @@ -25,15 +25,19 @@ 'INNERTUBE_API_KEY': 'AIzaSyA8eiZmM1FaDVjRy-df2KTyQ_vz_yYM39w', 'INNERTUBE_CONTEXT': { 'client': { + 'hl': 'en', + 'gl': 'US', 'clientName': 'ANDROID', 'clientVersion': '17.31.35', + 'osName': 'Android', + 'osVersion': '12', 'androidSdkVersion': 31, - 'userAgent': 'com.google.android.youtube/17.31.35 (Linux; U; Android 11) gzip' + 'userAgent': 'com.google.android.youtube/17.31.35 (Linux; U; Android 12) gzip' }, # https://github.com/yt-dlp/yt-dlp/pull/575#issuecomment-887739287 - 'thirdParty': { - 'embedUrl': 'https://google.com', # Can be any valid URL - } + #'thirdParty': { + # 'embedUrl': 'https://google.com', # Can be any valid URL + #} }, 'INNERTUBE_CONTEXT_CLIENT_NAME': 3, 'REQUIRE_JS_PLAYER': False, @@ -45,6 +49,8 @@ 'INNERTUBE_API_KEY': 'AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8', 'INNERTUBE_CONTEXT': { 'client': { + 'hl': 'en', + 'gl': 'US', 'clientName': 'TVHTML5_SIMPLY_EMBEDDED_PLAYER', 'clientVersion': '2.0', }, @@ -377,13 +383,14 @@ def fetch_player_response(client, video_id): client_params = INNERTUBE_CLIENTS[client] context = client_params['INNERTUBE_CONTEXT'] key = client_params['INNERTUBE_API_KEY'] - host = client_params.get('INNERTUBE_HOST') or 'youtubei.googleapis.com' + host = client_params.get('INNERTUBE_HOST') or 'www.youtube.com' user_agent = context['client'].get('userAgent') or util.mobile_user_agent url = 'https://' + host + '/youtubei/v1/player?key=' + key data = { 'videoId': video_id, 'context': context, + 'params': '8AEB', } data = json.dumps(data) headers = (('Content-Type', 'application/json'),('User-Agent', user_agent)) @@ -435,9 +442,7 @@ def extract_info(video_id, use_invidious, playlist_id=None, index=None): util.check_gevent_exceptions(*tasks) info, player_response = tasks[0].value, tasks[1].value - if yt_data_extract.requires_decryption(info): - print('Encrypted. Replacing with URLs from Android client') - yt_data_extract.update_with_new_urls(info, player_response) + yt_data_extract.update_with_new_urls(info, player_response) # Age restricted video, retry if info['age_restricted'] or info['player_urls_missing']: