From d2807223496e2778f58af9c11f1710de789e2677 Mon Sep 17 00:00:00 2001 From: girst Date: Wed, 3 Mar 2021 20:46:10 +0100 Subject: [PATCH] switch browse endpoint from browse_ajax to youtubei/v1/browse apparently, https://youtubei.googleapis.com/youtubei/v1/browse is the same endpoint. see also: https://github.com/TeamNewPipe/NewPipeExtractor/issues/566 https://github.com/iv-org/invidious/issues/1838 https://github.com/ytdl-org/youtube-dl/issues/28289 --- app/browse/__init__.py | 6 +++--- app/browse/lib.py | 14 ++++++++------ app/common/innertube.py | 6 ++---- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/app/browse/__init__.py b/app/browse/__init__.py index 4bbbc2c..3641c68 100644 --- a/app/browse/__init__.py +++ b/app/browse/__init__.py @@ -138,10 +138,10 @@ def playlist(): offset = (page-1)*100 # each call returns 100 items result = fetch_ajax(make_playlist_params(playlist_id, offset)) - if result is None or not 'continuationContents' in result[1]['response']: + if result is None or not 'continuationContents' in result: error_obj = ( - result|G(1)|G('response')|G('responseContext')|G('errors')|G('error')|G(0) - or result|G(1)|G('response')|G('alerts')|G(0)|G('alertRenderer') + result|G('responseContext')|G('errors')|G('error')|G(0) + or result|G('alerts')|G(0)|G('alertRenderer') ) error_type = error_obj|G('code', 'type') or 'Error' error = ( diff --git a/app/browse/lib.py b/app/browse/lib.py index 798c742..e3c7975 100644 --- a/app/browse/lib.py +++ b/app/browse/lib.py @@ -38,13 +38,15 @@ def fetch_ajax(params): # TODO: handle auto_generated! today = datetime.now(timezone.utc).strftime("%Y%m%d") - r = requests.get(f"https://www.youtube.com/browse_ajax", { + # TODO: this is not cached any more! -> https://github.com/reclosedev/requests-cache/issues/154 + r = requests.post(f"https://www.youtube.com/youtubei/v1/browse?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8", json={ 'continuation': params, - 'gl': 'US', - 'hl': 'en', - }, headers={ - 'x-youtube-client-name': '1', - 'x-youtube-client-version': f'2.{today}.01.01', # see fetch_searchresults() + 'context': {'client': { + 'gl': 'US', + 'hl': 'en', + 'clientName': 'WEB', + 'clientVersion': f'2.{today}.01.01', + }}, }) if not r.ok: diff --git a/app/common/innertube.py b/app/common/innertube.py index c8c3399..e98ea2e 100644 --- a/app/common/innertube.py +++ b/app/common/innertube.py @@ -77,9 +77,7 @@ def prepare_endcards(metadata): endsc = metadata.get('endscreen',{}).get('endscreenRenderer',{}).get('elements',[]) return list(filter(None, map(parse_endcard, endsc))) -def prepare_channel(result, channel_id): - response = listfind(result,'response') - +def prepare_channel(response, channel_id): if 'alerts' in response: # possibly got an error back from flask import current_app current_app.logger.error([(alert['alertRenderer']['type'],alert['alertRenderer']['text']['simpleText']) for alert in response['alerts']]) @@ -104,7 +102,7 @@ def prepare_channel(result, channel_id): return title, descr, thumb, items, has_more def prepare_playlist(result): - contents = listfind(result,'response')['continuationContents'] + contents = result['continuationContents'] unparsed = contents['playlistVideoListContinuation'].get('contents',[]) has_more = 'continuations' in contents.get('playlistVideoListContinuation') -- 2.39.3