From f0b33b297555ee811bb8c6a69e653523b8384c6c Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Sun, 4 Mar 2018 08:54:19 -0600 Subject: [PATCH] Clean up Video class --- src/helpers.cr | 34 +++++++++++++++++----------------- src/invidious.cr | 5 +---- 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/helpers.cr b/src/helpers.cr index 1492e7cc..e26c464f 100644 --- a/src/helpers.cr +++ b/src/helpers.cr @@ -5,12 +5,6 @@ class Video end end - module XMLConverter - def self.from_rs(rs) - XML.parse_html(rs.read(String)) - end - end - def initialize(id, info, updated, title, views, likes, dislikes, wilson_score, published, description) @id = id @info = info @@ -105,10 +99,8 @@ def get_client(pool) end def fetch_video(id, client) - begin - info = client.get("/get_video_info?video_id=#{id}&el=detailpage&ps=default&eurl=&gl=US&hl=en").body - html = client.get("/watch?v=#{id}").body - end + info = client.get("/get_video_info?video_id=#{id}&el=detailpage&ps=default&eurl=&gl=US&hl=en").body + html = client.get("/watch?v=#{id}").body html = XML.parse_html(html) info = HTTP::Params.parse(info) @@ -175,22 +167,21 @@ def get_video(id, client, db, refresh = true) # 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.hours video = fetch_video(id, client) - db.exec("UPDATE videos SET info = $2, updated = $3,\ - title = $4, views = $5, likes = $6, dislikes = $7, wilson_score = $8,\ - published = $9, description = $10 WHERE id = $1", video.to_a) + db.exec("DELETE FROM videos * WHERE id = $1", id) + args = arg_array(video.to_a) + db.exec("INSERT INTO videos VALUES (#{args})", video.to_a) end else video = fetch_video(id, client) - db.exec("INSERT INTO videos VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)", video.to_a) + args = arg_array(video.to_a) + db.exec("INSERT INTO videos VALUES (#{args})", video.to_a) end return video end def search(query, client) - begin - html = client.get("https://www.youtube.com/results?q=#{query}&sp=EgIQAVAU").body - end + html = client.get("https://www.youtube.com/results?q=#{query}&sp=EgIQAVAU").body html = XML.parse_html(html) @@ -330,3 +321,12 @@ end def number_with_separator(number) number.to_s.reverse.gsub(/(\d{3})(?=\d)/, "\\1,").reverse end + +def arg_array(array) + args = [] of String + (1..array.size).each { |i| args << "($#{i})," } + args = args.join("") + args = args.chomp(",") + + return args +end diff --git a/src/invidious.cr b/src/invidious.cr index 46dc02ee..22311abf 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -150,11 +150,8 @@ spawn do top = rank_videos(PG_DB, 40) client = get_client(youtube_pool) - args = [] of String if top.size > 0 - (1..top.size).each { |i| args << "($#{i})," } - args = args.join("") - args = args.chomp(",") + args = arg_array(top) else next end