From 68f854ed5a3872f1c46559fda8c75f4661c49fbb Mon Sep 17 00:00:00 2001 From: girst Date: Mon, 17 Aug 2020 18:50:21 +0200 Subject: [PATCH] clean up v1/v2 channel protobuf handling --- app/dangerous/__init__.py | 18 ++++++++++-------- app/dangerous/lib.py | 13 ------------- 2 files changed, 10 insertions(+), 21 deletions(-) diff --git a/app/dangerous/__init__.py b/app/dangerous/__init__.py index fb051c7..6ce326a 100644 --- a/app/dangerous/__init__.py +++ b/app/dangerous/__init__.py @@ -58,18 +58,20 @@ def channel(channel_id, subpage="videos"): else: # we don't support /home, /about, ..., so redirect to /videos. return redirect(url_for('.channel', channel_id=channel_id)) - try: - result = fetch_ajax(make_channel_params(channel_id, subpage, page, sort_by, query)) - except RuntimeError: - try: - result = fetch_ajax(make_channel_params(channel_id, subpage, page, sort_by, query, v2=True)) - except RuntimeError: # XXX: this should never happen™ -- log to somewhere - flash("unable to fetch results from 'classic' ajax; displaying fallback results (15 newest)", "error") - return fallback_route(channel_id, subpage) + result = fetch_ajax(make_channel_params(channel_id, subpage, page, sort_by, query)) + + # Note: as of 2020-08-15, using the v1 format sometimes returns an error. if that's the case, try v2. + alert = listget(listget(result,1,{}).get('response',{}).get('alerts',[]),0,{}).get('alertRenderer',{}) + if alert.get('type','') == "ERROR": # alert['text']['simpleText'] == "Unknown error." + result = fetch_ajax(make_channel_params(channel_id, subpage, page, sort_by, query, v2=True)) title, descr, thumb, rows, more = prepare_channel(result, channel_id) # TODO: add is_pinned/is_hidden + if title is None: # if both v1 and v2 failed, fall back to xmlfeed: + flash("unable to fetch results from ajax; displaying fallback results (15 newest)", "error") + return fallback_route(channel_id, subpage) + return render_template('channel.html.j2', title=title, subpage=subpage, diff --git a/app/dangerous/lib.py b/app/dangerous/lib.py index 24aef25..117e640 100644 --- a/app/dangerous/lib.py +++ b/app/dangerous/lib.py @@ -34,19 +34,6 @@ def fetch_ajax(params): 'x-youtube-client-version': f'2.{today}.01.01', # see fetch_searchresults() }) - try: # TODO: cleanup - alert = r.json()[1]['response']['alerts'][0]['alertRenderer'] - if alert['type'] == "ERROR": # alert['text']['simpleText'] == "Unknown error." - ''' XXX: cache busting not necessary! - # cache bust: - from requests_cache import get_cache - cache = get_cache() - main_key = cache.create_key(r.request) - cache.delete(main_key) - ''' - raise RuntimeError() # signal to retry with v2 - except KeyError: pass - if not r.ok: return None -- 2.39.3