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