From 00ce2db415fc44af3c45d47288f0ca8975a4bcf3 Mon Sep 17 00:00:00 2001 From: girst Date: Sun, 6 Feb 2022 20:50:34 +0100 Subject: [PATCH] allow sorting by most popular for fallback route --- app/youtube/__init__.py | 35 +++++++++++++++++++++------ app/youtube/templates/xmlfeed.html.j2 | 8 +++++- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/app/youtube/__init__.py b/app/youtube/__init__.py index b90f949..aee084f 100644 --- a/app/youtube/__init__.py +++ b/app/youtube/__init__.py @@ -211,22 +211,41 @@ def plain_user_or_video(something): # XXX: something == 'thethoughtemporium' -> 404s raise NotFound("Note: some usernames not recognized; try searching it") -@frontend.route('/channel//') -@frontend.route('/user//') @frontend.route('/c//') -@frontend.route('/channel//') -@frontend.route('/user//') @frontend.route('/c//') +@frontend.route('/user//') +@frontend.route('/user//') +def channel_redirect(channel_id, subpage=None): + # Note: we can't check /c/, so we have to assume it is the same as /user/, + # which is sometimes wrong. + xmlfeed = fetch_xml("user", channel_id) + + if not xmlfeed: + raise NotFound("unknown channel name") + + _, _, _, channel_id, _ = parse_xml(xmlfeed) + + return redirect(url_for('.channel', channel_id=channel_id)) + +@frontend.route('/channel//') +@frontend.route('/channel//') def channel(channel_id, _=None): token = getattr(current_user, 'token', 'guest') + sort = request.args.get("sort", "newest") - if re.match(r"(UC[A-Za-z0-9_-]{22})", channel_id): - xmlfeed = fetch_xml("channel_id", channel_id) + if not re.match(r"(UC[A-Za-z0-9_-]{22})", channel_id): + # canonicalize channel id, otherwise popular won't work + return redirect(url_for('.channel_redirect', channel_id=channel_id)) + + if sort == "popular": + xmlfeed = fetch_xml("playlist_id", f"PU{channel_id[2:]}") else: - xmlfeed = fetch_xml("user", channel_id) + xmlfeed = fetch_xml("channel_id", channel_id) + #^note: could also use playlist_id=UU... if not xmlfeed: - return "not found or something", 404 # XXX + raise NotFound("unknown channel id") + title, author, videos, channel_id, _ = parse_xml(xmlfeed) with sqlite3.connect(cf['global']['database']) as conn: diff --git a/app/youtube/templates/xmlfeed.html.j2 b/app/youtube/templates/xmlfeed.html.j2 index e4a76cb..e608752 100644 --- a/app/youtube/templates/xmlfeed.html.j2 +++ b/app/youtube/templates/xmlfeed.html.j2 @@ -4,6 +4,12 @@ {% block content %}

{{ title }} {{ macros.emoji_button("subscribe", channel_id, is_subscribed) if channel_id }}

+
+ {% if channel_id %} + newest first | + most popular + {% endif %} +
{% for row in rows %} {% call macros.card(row.video_id, row.title, row.published|format_date) %} @@ -14,5 +20,5 @@
-{% if not rows|length %}no more results{% endif %} +{% if not rows|length %}no results{% endif %} {% endblock %} -- 2.39.3