Add basic handling for (almost) valid video URLs

This commit is contained in:
Omar Roth 2018-10-06 22:22:22 -05:00
parent f7125c1204
commit 80bc29f3cd
2 changed files with 24 additions and 2 deletions

View File

@ -237,6 +237,8 @@ get "/watch" do |env|
begin begin
video = get_video(id, PG_DB, proxies) video = get_video(id, PG_DB, proxies)
rescue ex : VideoRedirect
next env.redirect "/watch?v=#{ex.message}"
rescue ex rescue ex
error_message = ex.message error_message = ex.message
STDOUT << id << " : " << ex.message << "\n" STDOUT << id << " : " << ex.message << "\n"
@ -337,6 +339,8 @@ get "/embed/:id" do |env|
begin begin
video = get_video(id, PG_DB, proxies) video = get_video(id, PG_DB, proxies)
rescue ex : VideoRedirect
next env.redirect "/embed/#{ex.message}"
rescue ex rescue ex
error_message = ex.message error_message = ex.message
next templated "error" next templated "error"
@ -1777,6 +1781,8 @@ get "/api/v1/captions/:id" do |env|
client = make_client(YT_URL) client = make_client(YT_URL)
begin begin
video = get_video(id, PG_DB, proxies) video = get_video(id, PG_DB, proxies)
rescue ex : VideoRedirect
next env.redirect "/api/v1/captions/#{ex.message}"
rescue ex rescue ex
halt env, status_code: 403 halt env, status_code: 403
end end
@ -2228,6 +2234,8 @@ get "/api/v1/videos/:id" do |env|
begin begin
video = get_video(id, PG_DB, proxies) video = get_video(id, PG_DB, proxies)
rescue ex : VideoRedirect
next env.redirect "/api/v1/videos/#{ex.message}"
rescue ex rescue ex
error_message = {"error" => ex.message}.to_json error_message = {"error" => ex.message}.to_json
halt env, status_code: 500, response: error_message halt env, status_code: 500, response: error_message
@ -3074,6 +3082,8 @@ get "/api/manifest/dash/id/:id" do |env|
client = make_client(YT_URL) client = make_client(YT_URL)
begin begin
video = get_video(id, PG_DB, proxies) video = get_video(id, PG_DB, proxies)
rescue ex : VideoRedirect
next env.redirect "/api/manifest/dash/id/#{ex.message}"
rescue ex rescue ex
halt env, status_code: 403 halt env, status_code: 403
end end

View File

@ -477,6 +477,9 @@ class CaptionName
) )
end end
class VideoRedirect < Exception
end
def get_video(id, db, proxies = {} of String => Array({ip: String, port: Int32}), refresh = true) def get_video(id, db, proxies = {} of String => Array({ip: String, port: Int32}), refresh = true)
if db.query_one?("SELECT EXISTS (SELECT true FROM videos WHERE id = $1)", id, as: Bool) if db.query_one?("SELECT EXISTS (SELECT true FROM videos WHERE id = $1)", id, as: Bool)
video = db.query_one("SELECT * FROM videos WHERE id = $1", id, as: Video) video = db.query_one("SELECT * FROM videos WHERE id = $1", id, as: Video)
@ -511,14 +514,18 @@ def get_video(id, db, proxies = {} of String => Array({ip: String, port: Int32})
end end
def fetch_video(id, proxies) def fetch_video(id, proxies)
html_channel = Channel(XML::Node).new html_channel = Channel(XML::Node | String).new
info_channel = Channel(HTTP::Params).new info_channel = Channel(HTTP::Params).new
spawn do spawn do
client = make_client(YT_URL) client = make_client(YT_URL)
html = client.get("/watch?v=#{id}&bpctr=#{Time.new.epoch + 2000}&gl=US&hl=en&disable_polymer=1") html = client.get("/watch?v=#{id}&bpctr=#{Time.new.epoch + 2000}&gl=US&hl=en&disable_polymer=1")
html = XML.parse_html(html.body)
if md = html.headers["location"]?.try &.match(/v=(?<id>[a-zA-Z0-9_-]{11})/)
next html_channel.send(md["id"])
end
html = XML.parse_html(html.body)
html_channel.send(html) html_channel.send(html)
end end
@ -536,6 +543,11 @@ def fetch_video(id, proxies)
end end
html = html_channel.receive html = html_channel.receive
if html.as?(String)
raise VideoRedirect.new("#{html.as(String)}")
end
html = html.as(XML::Node)
info = info_channel.receive info = info_channel.receive
if info["reason"]? && info["reason"].includes? "your country" if info["reason"]? && info["reason"].includes? "your country"