diff options
| author | Omar Roth <omarroth@hotmail.com> | 2019-02-21 15:07:22 -0600 |
|---|---|---|
| committer | Omar Roth <omarroth@hotmail.com> | 2019-02-21 15:07:22 -0600 |
| commit | 85854cac77e3d12edb36d9ec07f4b17d8a7a1c48 (patch) | |
| tree | 9b775ec3e4b24e1a0a7a60605b74432250eb6956 | |
| parent | 5bf3c284362905a02406a71fc0cbc396c782c426 (diff) | |
| download | invidious-85854cac77e3d12edb36d9ec07f4b17d8a7a1c48.tar.gz invidious-85854cac77e3d12edb36d9ec07f4b17d8a7a1c48.tar.bz2 invidious-85854cac77e3d12edb36d9ec07f4b17d8a7a1c48.zip | |
Add support for custom channel URLs
| -rw-r--r-- | src/invidious.cr | 50 |
1 files 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(/^\/(?<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 + end end env.response.headers["Location"] = "/" |
