From f8e2945266ed84aa7611535b3c4f348d3d10cfed Mon Sep 17 00:00:00 2001 From: Hugo Peixoto Date: Sun, 30 Jul 2023 01:13:02 +0100 Subject: [PATCH] Prioritize videos that haven't been downloaded yet --- main.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/main.rb b/main.rb index 1549f26..96c515b 100644 --- a/main.rb +++ b/main.rb @@ -54,7 +54,14 @@ get '/videos.json' do if amount > 100_000 { error: "greedy" }.to_json else - { videos: Video.order("RANDOM()").limit(amount).pluck(:randname) }.to_json + videos = Video + .left_outer_joins(:downloads) + .group("randname") + .order("COUNT(1) ASC, RANDOM()") + .limit(amount) + .pluck(:randname) + + { videos: videos }.to_json end end