Add support for custom channel URLs

This commit is contained in:
Omar Roth 2019-02-21 15:07:22 -06:00
parent 5bf3c28436
commit 85854cac77

View File

@ -232,28 +232,6 @@ end
# Videos # 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| get "/watch" do |env|
locale = LOCALES[env.get("locale").as(String)]? locale = LOCALES[env.get("locale").as(String)]?
region = env.params.query["region"]? region = env.params.query["region"]?
@ -4147,7 +4125,7 @@ get "/vi/:id/:name" do |env|
end end
error 404 do |env| 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"] id = md["id"]
params = [] of String params = [] of String
@ -4161,8 +4139,30 @@ error 404 do |env|
url += "&#{params}" url += "&#{params}"
end end
env.response.headers["Location"] = url client = make_client(YT_URL)
halt env, status_code: 302 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 end
env.response.headers["Location"] = "/" env.response.headers["Location"] = "/"