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