sapo-videos/main.rb
2023-07-28 03:01:40 +01:00

59 lines
1.2 KiB
Ruby

# SPDX-License-Identifier: AGPL-3.0-only
# SPDX-FileCopyrightText: 2023 Hugo Peixoto <hugo.peixoto@gmail.com>
require 'sinatra'
require './database.rb'
require './models.rb'
get '/' do
downloads = ActiveRecord::Base.connection.execute("
SELECT
COUNT(1) AS total,
SUM(size) AS bytes
FROM (select size from downloads group by video_id) as x;
")[0]
stats = { downloads: downloads }
ERB
.new(File.read("index.html.erb"), trim_mode: "<>-")
.result_with_hash(stats: stats)
end
get '/videos.json' do
content_type 'application/json'
amount = params['amount'].to_i
if amount > 100_000
{ error: "greedy" }.to_json
else
{ videos: Video.order("RANDOM()").limit(amount).pluck(:randname) }.to_json
end
end
post '/video' do
content_type 'application/json'
data = JSON.parse(request.body.read)
Download.create!(
video: Video.find_by!(randname: data["video_id"]),
size: data["size"],
sha256: data["sha256"],
email: data["email"],
)
{ status: 'ok' }.to_json
end
get '/config.json' do
content_type 'application/json'
{
videos_url: "https://sapo.pxto.pt/videos.json",
upload_url: "https://sapo.pxto.pt/video",
max_amount: 100_000,
}.to_json
end