From ddf66e32520a4ca5770904922b5bc171918d7e9f Mon Sep 17 00:00:00 2001 From: girst Date: Fri, 7 Aug 2020 12:49:29 +0200 Subject: [PATCH] add has_more, cleanup, test for error-alerts --- app/common/innertube.py | 33 ++++++++++++------------- app/dangerous/__init__.py | 5 ++-- app/dangerous/templates/channel.html.j2 | 6 ++--- 3 files changed, 21 insertions(+), 23 deletions(-) diff --git a/app/common/innertube.py b/app/common/innertube.py index 6facecf..b038efa 100644 --- a/app/common/innertube.py +++ b/app/common/innertube.py @@ -6,6 +6,7 @@ def findall(obj, key): """ given a list of dicts, where one dict contains a given key, return said key. """ + if obj is None: return [] return [ obj[key] for obj in obj if key in obj.keys() ] def listget(obj, index, fallback=None): return next(iter(obj[index:]), fallback) @@ -35,30 +36,28 @@ def prepare_endcards(metadata): def prepare_channel(result, channel_id): response = listfind(result,'response') + 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']]) + return None,None,[],[],False + meta1 = response.get('metadata',{}).get('channelMetadataRenderer',{}) meta2 = response.get('microformat',{}).get('microformatDataRenderer',{}) title = meta1.get('title', meta2.get('title')) descr = meta1.get('description', meta2.get('description')) # meta2.description is capped at 160chars thumb = mkthumbs(meta2.get('thumbnail',meta1.get('avatar',{})).get('thumbnails',{})) # .avatar ~ 900px - if 'continuationContents' in response.keys(): - contents = response['continuationContents'] - try: # TODO: cleanup - items, extra = parse_channel_items(contents['gridContinuation']['items'], channel_id, title) - except: - try: - items, extra = parse_channel_items(contents['sectionListContinuation']['contents'], channel_id, title) - except Exception as e: - from flask import current_app - current_app.logger.error(result) - raise e - # TODO: show extra to user - else: # if absent, we reach end of list - from flask import current_app - current_app.logger.error(result) - items = [] + contents = response.get('continuationContents') + if not contents: # overran end of list + return title, descr, thumb, [], False + + unparsed = contents.get('gridContinuation',{}).get('items') or \ + contents.get('sectionListContinuation',{}).get('contents') or [] + items, extra = parse_channel_items(unparsed, channel_id, title) + has_more = 'continuations' in (contents.get('gridContinuation') or + contents.get('sectionListContinuation') or {}) - return title, descr, thumb, items + return title, descr, thumb, items, has_more def prepare_playlist(result): contents = listfind(result,'response')['continuationContents']['playlistVideoListContinuation'] \ diff --git a/app/dangerous/__init__.py b/app/dangerous/__init__.py index a2a0626..ac5d511 100644 --- a/app/dangerous/__init__.py +++ b/app/dangerous/__init__.py @@ -55,7 +55,7 @@ def channel(channel_id, subpage="videos"): result = fetch_ajax(make_channel_params(channel_id, subpage, page, sort_by)) - title, descr, thumb, rows = prepare_channel(result, channel_id) + title, descr, thumb, rows, more = prepare_channel(result, channel_id) # TODO: add is_pinned/is_hidden return render_template('channel.html.j2', @@ -65,7 +65,8 @@ def channel(channel_id, subpage="videos"): channel_id=channel_id, channel_img=thumb, channel_desc=descr, - page=page) + page=page, + has_more=more) @frontend.route('/playlist') def playlist(): diff --git a/app/dangerous/templates/channel.html.j2 b/app/dangerous/templates/channel.html.j2 index 7ad1b7f..46d9a2a 100644 --- a/app/dangerous/templates/channel.html.j2 +++ b/app/dangerous/templates/channel.html.j2 @@ -65,10 +65,8 @@
{% if page is not none %} - {% if page > 1 %} - {{ macros.pagination("previous", {'page':(-1,1)}, -1) }} - {% endif %} - {{ macros.pagination("next", {'page':(+1,1)}, +1) }} + {{ macros.pagination("previous", {'page':(-1,1)}, -1) if page > 1 }} + {{ macros.pagination("next", {'page':(+1,1)}, +1) if has_more}} {% endif %}
{% endblock %} -- 2.39.3