mirror of
https://gitea.invidious.io/iv-org/invidious-copy-2022-08-14.git
synced 2024-08-15 00:53:20 +00:00
Move DB queries related to 'videos' in a separate module
This commit is contained in:
parent
4f219362fe
commit
998edba6f0
4 changed files with 53 additions and 7 deletions
|
@ -20,12 +20,13 @@ require "kemal"
|
||||||
require "athena-negotiation"
|
require "athena-negotiation"
|
||||||
require "openssl/hmac"
|
require "openssl/hmac"
|
||||||
require "option_parser"
|
require "option_parser"
|
||||||
require "pg"
|
|
||||||
require "sqlite3"
|
require "sqlite3"
|
||||||
require "xml"
|
require "xml"
|
||||||
require "yaml"
|
require "yaml"
|
||||||
require "compress/zip"
|
require "compress/zip"
|
||||||
require "protodec/utils"
|
require "protodec/utils"
|
||||||
|
|
||||||
|
require "./invidious/database/*"
|
||||||
require "./invidious/helpers/*"
|
require "./invidious/helpers/*"
|
||||||
require "./invidious/yt_backend/*"
|
require "./invidious/yt_backend/*"
|
||||||
require "./invidious/*"
|
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
|
end
|
||||||
|
|
||||||
def get_video(id, db, refresh = true, region = nil, force_refresh = false)
|
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,
|
# If record was last updated over 10 minutes ago, or video has since premiered,
|
||||||
# refresh (expire param in response lasts for 6 hours)
|
# refresh (expire param in response lasts for 6 hours)
|
||||||
if (refresh &&
|
if (refresh &&
|
||||||
|
@ -1003,17 +1003,15 @@ def get_video(id, db, refresh = true, region = nil, force_refresh = false)
|
||||||
force_refresh
|
force_refresh
|
||||||
begin
|
begin
|
||||||
video = fetch_video(id, region)
|
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
|
rescue ex
|
||||||
db.exec("DELETE FROM videos * WHERE id = $1", id)
|
Invidious::Database::Videos.delete(id)
|
||||||
raise ex
|
raise ex
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
video = fetch_video(id, region)
|
video = fetch_video(id, region)
|
||||||
if !region
|
Invidious::Database::Videos.insert(video) 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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return video
|
return video
|
||||||
|
|
Loading…
Reference in a new issue