From 0cf8f859ec41b14a1af9413ab0b5cf6a38c5c9ed Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Mon, 30 Jul 2018 14:14:11 -0500 Subject: [PATCH] Provide user with error message on '/watch' page --- src/invidious.cr | 26 +++++++++----------------- src/invidious/helpers.cr | 27 +++++++++++++++------------ 2 files changed, 24 insertions(+), 29 deletions(-) diff --git a/src/invidious.cr b/src/invidious.cr index c9392c13..6addc1be 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -95,7 +95,7 @@ crawl_threads.times do begin id = ids[0] - video = get_video(id, client, PG_DB) + video = get_video(id, PG_DB) rescue ex STDOUT << id << " : " << ex.message << "\n" next @@ -159,14 +159,11 @@ video_threads.times do |i| OFFSET (SELECT count(*)*$1/$2 FROM videos)" PG_DB.query(query, i, video_threads) do |rs| rs.each do - client = make_client(YT_URL) - begin id = rs.read(String) - video = get_video(id, client, PG_DB) + video = get_video(id, PG_DB) rescue ex STDOUT << id << " : " << ex.message << "\n" - client = make_client(YT_URL) next end end @@ -204,9 +201,8 @@ spawn do videos = [] of Video top.each do |id| - client = make_client(YT_URL) begin - videos << get_video(id, client, PG_DB) + videos << get_video(id, PG_DB) rescue ex next end @@ -323,12 +319,11 @@ get "/watch" do |env| end listen ||= false - client = make_client(YT_URL) begin - video = get_video(id, client, PG_DB) + video = get_video(id, PG_DB) rescue ex error_message = ex.message - env.response.status_code = 500 + STDOUT << id << " : " << ex.message << "\n" next templated "error" end @@ -444,7 +439,7 @@ get "/api/v1/captions/:id" do |env| client = make_client(YT_URL) begin - video = get_video(id, client, PG_DB) + video = get_video(id, PG_DB) rescue ex halt env, status_code: 403 end @@ -706,9 +701,8 @@ end get "/api/v1/videos/:id" do |env| id = env.params.url["id"] - client = make_client(YT_URL) begin - video = get_video(id, client, PG_DB) + video = get_video(id, PG_DB) rescue ex halt env, status_code: 403 end @@ -1292,9 +1286,8 @@ get "/embed/:id" do |env| video_loop = env.params.query["loop"]?.try &.to_i video_loop ||= 0 - client = make_client(YT_URL) begin - video = get_video(id, client, PG_DB) + video = get_video(id, PG_DB) rescue ex error_message = ex.message next templated "error" @@ -1370,7 +1363,6 @@ get "/search" do |env| page ||= 1 client = make_client(YT_URL) - html = client.get("/results?q=#{URI.escape(query)}&page=#{page}&sp=EgIQAVAU").body html = XML.parse_html(html) @@ -2356,7 +2348,7 @@ get "/api/manifest/dash/id/:id" do |env| client = make_client(YT_URL) begin - video = get_video(id, client, PG_DB) + video = get_video(id, PG_DB) rescue ex halt env, status_code: 403 end diff --git a/src/invidious/helpers.cr b/src/invidious/helpers.cr index b76befb2..497edc8a 100644 --- a/src/invidious/helpers.cr +++ b/src/invidious/helpers.cr @@ -247,24 +247,26 @@ def elapsed_text(elapsed) "#{(millis * 1000).round(2)}µs" end -def fetch_video(id, client) +def fetch_video(id) info_channel = Channel(HTTP::Params).new html_channel = Channel(XML::Node).new spawn do - html = client.get("/watch?v=#{id}&bpctr=#{Time.new.epoch + 2000}&disable_polymer=1").body - html = XML.parse_html(html) + client = make_client(YT_URL) + html = client.get("/watch?v=#{id}&bpctr=#{Time.new.epoch + 2000}&disable_polymer=1") + html = XML.parse_html(html.body) html_channel.send(html) end spawn do - info = client.get("/get_video_info?video_id=#{id}&el=detailpage&ps=default&eurl=&gl=US&hl=en&disable_polymer=1").body - info = HTTP::Params.parse(info) + client = make_client(YT_URL) + 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"]? - info = client.get("/get_video_info?video_id=#{id}&ps=default&eurl=&gl=US&hl=en&disable_polymer=1").body - info = HTTP::Params.parse(info) + info = client.get("/get_video_info?video_id=#{id}&ps=default&eurl=&gl=US&hl=en&disable_polymer=1") + info = HTTP::Params.parse(info.body) end info_channel.send(info) @@ -273,7 +275,7 @@ def fetch_video(id, client) html = html_channel.receive info = info_channel.receive - if info["reson"]? + if info["reason"]? raise info["reason"] end @@ -308,14 +310,14 @@ def fetch_video(id, client) return video 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) 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 refresh && Time.now - video.updated > 1.hour begin - video = fetch_video(id, client) + video = fetch_video(id) video_array = video.to_a 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) rescue ex db.exec("DELETE FROM videos * WHERE id = $1", id) + raise ex end end else - video = fetch_video(id, client) + video = fetch_video(id) video_array = video.to_a args = arg_array(video_array) @@ -448,7 +451,7 @@ def rank_videos(db, n, filter, url) else client = make_client(url) begin - video = get_video(id, client, db) + video = get_video(id, db) rescue ex next end