From fda57ed5937f846adeb242fcc419f21ce2055ac4 Mon Sep 17 00:00:00 2001 From: girst Date: Sat, 4 Sep 2021 11:50:00 +0200 Subject: [PATCH] take full youtube.com urls as path, and redirect accordingly this version only handles redirections to /watch for now. --- app/youtube/__init__.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/app/youtube/__init__.py b/app/youtube/__init__.py index b499e34..1786696 100644 --- a/app/youtube/__init__.py +++ b/app/youtube/__init__.py @@ -355,6 +355,32 @@ def manage_subscriptions(): return redirect(request.url, code=303) +@frontend.record +def redirect_youtube_dot_com(state): + """ + This is executed when the blueprint is loaded dynamically builds a number + of routes so that URLs like + https://subscriptions.gir.st/https://www.youtube.com/watch?v=dQw4w9WgXcQ + redirect to the /watch page. Works with /watch, /embed/ and youtu.be short + links, with or without protocl and/or 'www'. + """ + def real_redirect_youtube_dot_com(video_id=None): + if not re.match(r"^[-_0-9A-Za-z]{11}$", video_id or ''): video_id = None + if not video_id: video_id = request.args.get('v') + return redirect(url_for('.watch', v=video_id)) + + for protocol in ("", "http://", "https://"): + for prefix in ("", "www.", "m."): + for domain in ("youtube.com", "youtu.be", "youtube-nocookie.com"): + for urlpath in ("/watch", "/embed/", "/"): + if domain != "youtu.be" and urlpath == "/": + continue # that's a channel, not a video + frontend.add_url_rule( + f"/{protocol}{prefix}{domain}{urlpath}", + view_func=real_redirect_youtube_dot_com, + strict_slashes=False + ) + def get_cipher(): # reload cipher from database every 1 hour if 'cipher' not in g or time.time() - g.get('cipher_updated', 0) > 1 * 60 * 60: -- 2.39.3