Invidious is an alternative front-end to YouTube
Add support for custom channel URLs
Omar Roth85854cac77e3
diff --git a/src/invidious.cr b/src/invidious.crindex e7fa4c90..971f0fbd 100644--- a/src/invidious.cr+++ b/src/invidious.cr@@ -232,28 +232,6 @@ end# Videos-get "/:id" do |env|- locale = LOCALES[env.get("locale").as(String)]?- id = env.params.url["id"]-- if md = id.match(/[a-zA-Z0-9_-]{11}/)- params = [] of String- env.params.query.each do |k, v|- params << "#{k}=#{v}"- end- params = params.join("&")-- url = "/watch?v=#{id}"- if !params.empty?- url += "&#{params}"- end-- env.redirect url- else- env.response.status_code = 404- end-end-get "/watch" do |env|locale = LOCALES[env.get("locale").as(String)]?region = env.params.query["region"]?@@ -4147,7 +4125,7 @@ get "/vi/:id/:name" do |env|enderror 404 do |env|- if md = env.request.path.match(/^\/(?<id>[a-zA-Z0-9_-]{11})/)+ if md = env.request.path.match(/^\/(?<id>[a-zA-Z0-9_-]{11})$/)id = md["id"]params = [] of String@@ -4161,8 +4139,30 @@ error 404 do |env|url += "&#{params}"end- env.response.headers["Location"] = url- halt env, status_code: 302+ client = make_client(YT_URL)+ if client.head("/#{id}").status_code == 404+ env.response.headers["Location"] = url+ halt env, status_code: 302+ end+ end++ if md = env.request.path.match(/^\/(?<name>\w+)$/)+ name = md["name"]++ client = make_client(YT_URL)+ response = client.get("/#{name}")++ if response.status_code == 301+ response = client.get(response.headers["Location"])+ end++ html = XML.parse_html(response.body)+ ucid = html.xpath_node(%q(//meta[@itemprop="channelId"]))++ if ucid+ env.response.headers["Location"] = "/channel/#{ucid["content"]}"+ halt env, status_code: 302+ endendenv.response.headers["Location"] = "/"