Provide user with error message on '/watch' page

This commit is contained in:
Omar Roth 2018-07-30 14:14:11 -05:00
parent 24c79a3554
commit 0cf8f859ec
2 changed files with 24 additions and 29 deletions

View file

@ -95,7 +95,7 @@ crawl_threads.times do
begin begin
id = ids[0] id = ids[0]
video = get_video(id, client, PG_DB) video = get_video(id, PG_DB)
rescue ex rescue ex
STDOUT << id << " : " << ex.message << "\n" STDOUT << id << " : " << ex.message << "\n"
next next
@ -159,14 +159,11 @@ video_threads.times do |i|
OFFSET (SELECT count(*)*$1/$2 FROM videos)" OFFSET (SELECT count(*)*$1/$2 FROM videos)"
PG_DB.query(query, i, video_threads) do |rs| PG_DB.query(query, i, video_threads) do |rs|
rs.each do rs.each do
client = make_client(YT_URL)
begin begin
id = rs.read(String) id = rs.read(String)
video = get_video(id, client, PG_DB) video = get_video(id, PG_DB)
rescue ex rescue ex
STDOUT << id << " : " << ex.message << "\n" STDOUT << id << " : " << ex.message << "\n"
client = make_client(YT_URL)
next next
end end
end end
@ -204,9 +201,8 @@ spawn do
videos = [] of Video videos = [] of Video
top.each do |id| top.each do |id|
client = make_client(YT_URL)
begin begin
videos << get_video(id, client, PG_DB) videos << get_video(id, PG_DB)
rescue ex rescue ex
next next
end end
@ -323,12 +319,11 @@ get "/watch" do |env|
end end
listen ||= false listen ||= false
client = make_client(YT_URL)
begin begin
video = get_video(id, client, PG_DB) video = get_video(id, PG_DB)
rescue ex rescue ex
error_message = ex.message error_message = ex.message
env.response.status_code = 500 STDOUT << id << " : " << ex.message << "\n"
next templated "error" next templated "error"
end end
@ -444,7 +439,7 @@ get "/api/v1/captions/:id" do |env|
client = make_client(YT_URL) client = make_client(YT_URL)
begin begin
video = get_video(id, client, PG_DB) video = get_video(id, PG_DB)
rescue ex rescue ex
halt env, status_code: 403 halt env, status_code: 403
end end
@ -706,9 +701,8 @@ end
get "/api/v1/videos/:id" do |env| get "/api/v1/videos/:id" do |env|
id = env.params.url["id"] id = env.params.url["id"]
client = make_client(YT_URL)
begin begin
video = get_video(id, client, PG_DB) video = get_video(id, PG_DB)
rescue ex rescue ex
halt env, status_code: 403 halt env, status_code: 403
end end
@ -1292,9 +1286,8 @@ get "/embed/:id" do |env|
video_loop = env.params.query["loop"]?.try &.to_i video_loop = env.params.query["loop"]?.try &.to_i
video_loop ||= 0 video_loop ||= 0
client = make_client(YT_URL)
begin begin
video = get_video(id, client, PG_DB) video = get_video(id, PG_DB)
rescue ex rescue ex
error_message = ex.message error_message = ex.message
next templated "error" next templated "error"
@ -1370,7 +1363,6 @@ get "/search" do |env|
page ||= 1 page ||= 1
client = make_client(YT_URL) client = make_client(YT_URL)
html = client.get("/results?q=#{URI.escape(query)}&page=#{page}&sp=EgIQAVAU").body html = client.get("/results?q=#{URI.escape(query)}&page=#{page}&sp=EgIQAVAU").body
html = XML.parse_html(html) html = XML.parse_html(html)
@ -2356,7 +2348,7 @@ get "/api/manifest/dash/id/:id" do |env|
client = make_client(YT_URL) client = make_client(YT_URL)
begin begin
video = get_video(id, client, PG_DB) video = get_video(id, PG_DB)
rescue ex rescue ex
halt env, status_code: 403 halt env, status_code: 403
end end

View file

@ -247,24 +247,26 @@ def elapsed_text(elapsed)
"#{(millis * 1000).round(2)}µs" "#{(millis * 1000).round(2)}µs"
end end
def fetch_video(id, client) def fetch_video(id)
info_channel = Channel(HTTP::Params).new info_channel = Channel(HTTP::Params).new
html_channel = Channel(XML::Node).new html_channel = Channel(XML::Node).new
spawn do spawn do
html = client.get("/watch?v=#{id}&bpctr=#{Time.new.epoch + 2000}&disable_polymer=1").body client = make_client(YT_URL)
html = XML.parse_html(html) html = client.get("/watch?v=#{id}&bpctr=#{Time.new.epoch + 2000}&disable_polymer=1")
html = XML.parse_html(html.body)
html_channel.send(html) html_channel.send(html)
end end
spawn do spawn do
info = client.get("/get_video_info?video_id=#{id}&el=detailpage&ps=default&eurl=&gl=US&hl=en&disable_polymer=1").body client = make_client(YT_URL)
info = HTTP::Params.parse(info) info = client.get("/get_video_info?video_id=#{id}&el=detailpage&ps=default&eurl=&gl=US&hl=en&disable_polymer=1")
info = HTTP::Params.parse(info.body)
if info["reason"]? if info["reason"]?
info = client.get("/get_video_info?video_id=#{id}&ps=default&eurl=&gl=US&hl=en&disable_polymer=1").body info = client.get("/get_video_info?video_id=#{id}&ps=default&eurl=&gl=US&hl=en&disable_polymer=1")
info = HTTP::Params.parse(info) info = HTTP::Params.parse(info.body)
end end
info_channel.send(info) info_channel.send(info)
@ -273,7 +275,7 @@ def fetch_video(id, client)
html = html_channel.receive html = html_channel.receive
info = info_channel.receive info = info_channel.receive
if info["reson"]? if info["reason"]?
raise info["reason"] raise info["reason"]
end end
@ -308,14 +310,14 @@ def fetch_video(id, client)
return video return video
end end
def get_video(id, client, db, refresh = true) def get_video(id, db, 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)
# If record was last updated over an hour ago, refresh (expire param in response lasts for 6 hours) # If record was last updated over an hour ago, refresh (expire param in response lasts for 6 hours)
if refresh && Time.now - video.updated > 1.hour if refresh && Time.now - video.updated > 1.hour
begin begin
video = fetch_video(id, client) video = fetch_video(id)
video_array = video.to_a video_array = video.to_a
args = arg_array(video_array[1..-1], 2) args = arg_array(video_array[1..-1], 2)
@ -324,10 +326,11 @@ def get_video(id, client, db, refresh = true)
= (#{args}) WHERE id = $1", video_array) = (#{args}) WHERE id = $1", video_array)
rescue ex rescue ex
db.exec("DELETE FROM videos * WHERE id = $1", id) db.exec("DELETE FROM videos * WHERE id = $1", id)
raise ex
end end
end end
else else
video = fetch_video(id, client) video = fetch_video(id)
video_array = video.to_a video_array = video.to_a
args = arg_array(video_array) args = arg_array(video_array)
@ -448,7 +451,7 @@ def rank_videos(db, n, filter, url)
else else
client = make_client(url) client = make_client(url)
begin begin
video = get_video(id, client, db) video = get_video(id, db)
rescue ex rescue ex
next next
end end