Add support for custom channel URLs

このコミットが含まれているのは:
Omar Roth 2019-02-21 15:07:22 -06:00
コミット 85854cac77
1個のファイルの変更25行の追加25行の削除

ファイルの表示

@ -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"] = "/"