From 85854cac77e3d12edb36d9ec07f4b17d8a7a1c48 Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Thu, 21 Feb 2019 15:07:22 -0600 Subject: [PATCH] Add support for custom channel URLs --- src/invidious.cr | 50 ++++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/src/invidious.cr b/src/invidious.cr index 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| end error 404 do |env| - if md = env.request.path.match(/^\/(?[a-zA-Z0-9_-]{11})/) + if md = env.request.path.match(/^\/(?[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(/^\/(?\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 + end end env.response.headers["Location"] = "/"