mirror of
https://github.com/iv-org/invidious
synced 2024-11-27 21:43:28 +01:00
Move DB queries related to 'videos' in a separate module
This commit is contained in:
parent
4f219362fe
commit
998edba6f0
@ -20,12 +20,13 @@ require "kemal"
|
||||
require "athena-negotiation"
|
||||
require "openssl/hmac"
|
||||
require "option_parser"
|
||||
require "pg"
|
||||
require "sqlite3"
|
||||
require "xml"
|
||||
require "yaml"
|
||||
require "compress/zip"
|
||||
require "protodec/utils"
|
||||
|
||||
require "./invidious/database/*"
|
||||
require "./invidious/helpers/*"
|
||||
require "./invidious/yt_backend/*"
|
||||
require "./invidious/*"
|
||||
|
4
src/invidious/database/base.cr
Normal file
4
src/invidious/database/base.cr
Normal file
@ -0,0 +1,4 @@
|
||||
require "pg"
|
||||
|
||||
module Invidious::Database
|
||||
end
|
43
src/invidious/database/videos.cr
Normal file
43
src/invidious/database/videos.cr
Normal file
@ -0,0 +1,43 @@
|
||||
require "./base.cr"
|
||||
|
||||
module Invidious::Database::Videos
|
||||
extend self
|
||||
|
||||
def insert(video : Video)
|
||||
request = <<-SQL
|
||||
INSERT INTO videos
|
||||
VALUES ($1, $2, $3)
|
||||
ON CONFLICT (id) DO NOTHING
|
||||
SQL
|
||||
|
||||
PG_DB.exec(request, video.id, video.info.to_json, video.updated)
|
||||
end
|
||||
|
||||
def delete(id)
|
||||
request = <<-SQL
|
||||
DELETE FROM videos *
|
||||
WHERE id = $1
|
||||
SQL
|
||||
|
||||
PG_DB.exec(request, id)
|
||||
end
|
||||
|
||||
def update(video : Video)
|
||||
request = <<-SQL
|
||||
UPDATE videos
|
||||
SET (id, info, updated) = ($1, $2, $3)
|
||||
WHERE id = $1
|
||||
SQL
|
||||
|
||||
PG_DB.exec(request, video.id, video.info.to_json, video.updated)
|
||||
end
|
||||
|
||||
def select(id : String) : Video?
|
||||
request = <<-SQL
|
||||
SELECT * FROM videos
|
||||
WHERE id = $1
|
||||
SQL
|
||||
|
||||
return PG_DB.query_one?(request, id, as: Video)
|
||||
end
|
||||
end
|
@ -994,7 +994,7 @@ def extract_video_info(video_id : String, proxy_region : String? = nil, context_
|
||||
end
|
||||
|
||||
def get_video(id, db, refresh = true, region = nil, force_refresh = false)
|
||||
if (video = db.query_one?("SELECT * FROM videos WHERE id = $1", id, as: Video)) && !region
|
||||
if (video = Invidious::Database::Videos.select(id)) && !region
|
||||
# If record was last updated over 10 minutes ago, or video has since premiered,
|
||||
# refresh (expire param in response lasts for 6 hours)
|
||||
if (refresh &&
|
||||
@ -1003,17 +1003,15 @@ def get_video(id, db, refresh = true, region = nil, force_refresh = false)
|
||||
force_refresh
|
||||
begin
|
||||
video = fetch_video(id, region)
|
||||
db.exec("UPDATE videos SET (id, info, updated) = ($1, $2, $3) WHERE id = $1", video.id, video.info.to_json, video.updated)
|
||||
Invidious::Database::Videos.update(video)
|
||||
rescue ex
|
||||
db.exec("DELETE FROM videos * WHERE id = $1", id)
|
||||
Invidious::Database::Videos.delete(id)
|
||||
raise ex
|
||||
end
|
||||
end
|
||||
else
|
||||
video = fetch_video(id, region)
|
||||
if !region
|
||||
db.exec("INSERT INTO videos VALUES ($1, $2, $3) ON CONFLICT (id) DO NOTHING", video.id, video.info.to_json, video.updated)
|
||||
end
|
||||
Invidious::Database::Videos.insert(video) if !region
|
||||
end
|
||||
|
||||
return video
|
||||
|
Loading…
Reference in New Issue
Block a user