From 60e85946f27f9fb0742b3c6ace103cbe29f49f25 Mon Sep 17 00:00:00 2001 From: girst Date: Sat, 19 Sep 2020 15:47:48 +0200 Subject: [PATCH] move to v3 channel format modeled after https://github.com/user234683/youtube-local/commit/1ff97bfde1467f7f18d4d2715a9357c41d9e9b8f --- app/dangerous/__init__.py | 10 +++++----- app/dangerous/protobuf.py | 29 ++++++++++++----------------- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/app/dangerous/__init__.py b/app/dangerous/__init__.py index 552ccbc..0c20441 100644 --- a/app/dangerous/__init__.py +++ b/app/dangerous/__init__.py @@ -20,7 +20,7 @@ frontend = Blueprint('dangerous', __name__, def search(): #token = getattr(current_user, 'token', 'guest') q = request.args.get('q') or request.args.get('search_query') - page = int(request.args.get('page', 1)) + page = int(request.args.get('page') or 1) sp = make_sp(**{ k:v for k,v in request.args.items() @@ -47,7 +47,7 @@ def search(): def channel(channel_id, subpage="videos"): if subpage == "videos": page = int(request.args.get('page', 1)) - sort_by = request.args.get('sort', "newest") + sort_by = request.args.get('sort') or "newest" # XXX: using .get(x) or y instead of .get(x,y) works around "?sort=", which breaks inside protobuf.py later query = None elif subpage == "playlists": page = None # TODO: cursor @@ -66,15 +66,15 @@ def channel(channel_id, subpage="videos"): 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. + # Note: as of 2020-08-15, using the v1 format sometimes returns an error. if that's the case, try v3. 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)) + result = fetch_ajax(make_channel_params(channel_id, subpage, page, sort_by, query, v3=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: + if title is None: # if both v1 and v3 failed, fall back to xmlfeed: flash("unable to fetch results from ajax; displaying fallback results (15 newest)", "error") return fallback_route(channel_id, subpage) diff --git a/app/dangerous/protobuf.py b/app/dangerous/protobuf.py index 19f0787..69448a8 100644 --- a/app/dangerous/protobuf.py +++ b/app/dangerous/protobuf.py @@ -82,10 +82,10 @@ class Params: @dataclass class Continuation: params: Params = field(80226972) -def make_channel_params(subject, typ="videos", page=1, sort=None, query=None, v2=False): +def make_channel_params(subject, typ="videos", page=1, sort=None, query=None, v3=False): typestr = dict(videos="videos", playlists="playlists", search="search") typeint = dict(videos=0, playlists=1, search=None) # not supporting autogen'd - sortorder = dict(newest=None, popular=1, oldest=2) # v1: newest=None, v2: newest=3 + sortorder = dict(newest=None, popular=1, oldest=2) # v1: newest=None, v3: newest=3 if typ == "playlists": sortorder = dict(oldest=2, newest=3, modified=4) elif typ == "search": @@ -99,29 +99,24 @@ def make_channel_params(subject, typ="videos", page=1, sort=None, query=None, v2 type_i=typeint.get(typ), sort=sortorder.get(sort) or 3, #page=str(page) if page else None # Note: ucid/playlists doesn't support pagination - page=None if v2 or not page else str(page), + page=None if v3 or not page else str(page), field61=b64e( - ChannelData( - data=ChannelData2( - const1=dict(newest=6307666885028338688,oldest=17254859483345278706,popular=16570086088270825023).get(sort), - data2=ChannelData3( - data3=b64e( - ChannelData4( - offset=(page-1)*30 - ).dumps(), padding=False - ) - ) + ChannelData3( + data3=b64e( + ChannelData4( + offset=(page-1)*30 + ).dumps(), padding=False ) ).dumps(), padding=False - ) if v2 else None + ) if v3 else None ).dumps()), query=query, ), ).dumps()) # }}} CHANNEL v1 -# CHANNEL v2 {{{ -# NOTE: on hosts that are supposed to use v2, accessing v1 results in an "Unknown error.". on v1-hosts, accessing v2 always returns the first page. +# CHANNEL v3 {{{ +# NOTE: on hosts that are supposed to use v3, accessing v1 results in an "Unknown error.". on v1-hosts, accessing v3 always returns the first page. @message @dataclass class ChannelData4: @@ -139,7 +134,7 @@ class ChannelData2: @dataclass class ChannelData: data: ChannelData2 = field(1) -# }}} CHANNEL v2 +# }}} CHANNEL v3 # PLAYLIST {{{ @message -- 2.39.3