From 1ea72908929ff1666fd3e022a7c182ad669903c3 Mon Sep 17 00:00:00 2001 From: Omar Roth Date: Sat, 28 Apr 2018 10:50:02 -0500 Subject: [PATCH] Add video refresh --- config/config.yml | 3 ++- src/helpers.cr | 1 + src/invidious.cr | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/config/config.yml b/config/config.yml index 3891c228..ad8ccd98 100644 --- a/config/config.yml +++ b/config/config.yml @@ -1,5 +1,6 @@ -channel_threads: 5 threads: 5 +channel_threads: 5 +video_threads: 5 db: user: kemal password: kemal diff --git a/src/helpers.cr b/src/helpers.cr index 6cfe7baa..6d403446 100644 --- a/src/helpers.cr +++ b/src/helpers.cr @@ -17,6 +17,7 @@ class Config YAML.mapping({ threads: Int32, channel_threads: Int32, + video_threads: Int32, db: NamedTuple( user: String, password: String, diff --git a/src/invidious.cr b/src/invidious.cr index 1948c8e2..aede9904 100644 --- a/src/invidious.cr +++ b/src/invidious.cr @@ -27,6 +27,7 @@ CONFIG = Config.from_yaml(File.read("config/config.yml")) threads = CONFIG.threads channel_threads = CONFIG.channel_threads +video_threads = CONFIG.video_threads Kemal.config.extra_options do |parser| parser.banner = "Usage: invidious [arguments]" @@ -46,6 +47,14 @@ Kemal.config.extra_options do |parser| exit end end + parser.on("-v THREADS", "--video-threads=THREADS", "Number of threads for refreshing videos (default: #{video_threads})") do |number| + begin + video_threads = number.to_i + rescue ex + puts "THREADS must be integer" + exit +end + end end Kemal::CLI.new @@ -139,6 +148,31 @@ channel_threads.times do |i| end end +video_threads.times do |i| + spawn do + loop do + query = "SELECT id FROM videos ORDER BY updated \ + LIMIT (SELECT count(*)/$2 FROM videos) \ + 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) + rescue ex + STDOUT << id << " : " << ex.message << "\n" + client = make_client(YT_URL) + next + end + end + end + Fiber.yield + end + end +end + top_videos = [] of Video spawn do