From fa1da8178c419baec7f2e13769849e200ac896dc Mon Sep 17 00:00:00 2001 From: girst Date: Sun, 19 Mar 2023 20:18:09 +0000 Subject: [PATCH] hackily implement @usernames --- app/browse/__init__.py | 4 +++- app/browse/lib.py | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/app/browse/__init__.py b/app/browse/__init__.py index bfebfbf..df39a04 100644 --- a/app/browse/__init__.py +++ b/app/browse/__init__.py @@ -170,7 +170,9 @@ def plain_user_or_video(something): # prevent a lot of false-positives (and reduce youtube api calls) raise NotFound - channel_id = canonicalize_channel(something) + # possible channel names: need to distinguish /name from /@name + typ = "c" if something[0] != "@" else "" + channel_id = canonicalize_channel(something, typ) if channel_id: return redirect(url_for('.channel', channel_id=channel_id)) elif re.match(r"^[-_0-9A-Za-z]{11}$", something): # looks like a video id diff --git a/app/browse/lib.py b/app/browse/lib.py index 3d1f9d3..5d46b05 100644 --- a/app/browse/lib.py +++ b/app/browse/lib.py @@ -34,7 +34,8 @@ def canonicalize_channel(name, typ="c"): # get UCID of /c/ (vanity URLs): today = datetime.now(timezone.utc).strftime("%Y%m%d") - r = requests.get(f'https://www.youtube.com/{typ}/{name}/about?pbj=1&hl=en_US', headers={ + typ += "/" if typ != "@" else "" + r = requests.get(f'https://www.youtube.com/{typ}{name}/about?pbj=1&hl=en_US', headers={ 'x-youtube-client-name': '1', 'x-youtube-client-version': f'2.{today}.01.01', # see fetch_searchresults() }) -- 2.39.3