Add alternative names and cover art for soundtracks
Alternative names (#32): - PUT /api/v1/soundtracks/{uuid}/alternative-names replaces a soundtrack's alternative names (trimmed, deduplicated, without the soundtrack's own name). - alternative_names is in the soundtrack and statistics responses. - /find matches them too and shows which name matched. Covers (#12), stored in COVERS_PATH and off when it is not set: - GET, PUT (upload) and DELETE /api/v1/soundtracks/{uuid}/cover, and PUT .../cover/source to pick a SteamGridDB game or AniList anime by ID. Uploaded and picked covers are never replaced automatically. - With STEAMGRIDDB_API_KEY, a background job searches SteamGridDB, then AniList, by name, alternative names and the name without bracketed parts, at startup, after a sync and after names change. Search results are ranked by matching words, so "Civilization IV" gets "Sid Meier's Civilization IV". - cover_modified (Unix seconds) is in the soundtrack and statistics responses, so clients can cache covers. The new columns are added to migration 000006, which is not released yet. Closes #12, closes #32 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f79f51ea4c
commit
bb8de5548d
39 files changed
+2418
-99
No files matched your search
@@ -9,6 +9,7 @@ import (
|
||||
"context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const addSoundtrackPlayed = `-- name: AddSoundtrackPlayed :exec
|
||||
@@ -29,6 +30,19 @@ func (q *Queries) AddSoundtrackPlayedByUUID(ctx context.Context, argUuid uuid.UU
|
||||
return err
|
||||
}
|
||||
|
||||
const clearCover = `-- name: ClearCover :exec
|
||||
UPDATE soundtrack
|
||||
SET cover_file = NULL, cover_source = NULL, cover_external_id = NULL, cover_manual = false,
|
||||
cover_modified = NULL, cover_checked = NULL
|
||||
WHERE uuid = $1
|
||||
`
|
||||
|
||||
// Removes the cover, so the job searches for one again.
|
||||
func (q *Queries) ClearCover(ctx context.Context, argUuid uuid.UUID) error {
|
||||
_, err := q.db.Exec(ctx, clearCover, argUuid)
|
||||
return err
|
||||
}
|
||||
|
||||
const clearSoundtracks = `-- name: ClearSoundtracks :exec
|
||||
DELETE FROM soundtrack
|
||||
`
|
||||
@@ -39,7 +53,7 @@ func (q *Queries) ClearSoundtracks(ctx context.Context) error {
|
||||
}
|
||||
|
||||
const findAllSoundtracks = `-- name: FindAllSoundtracks :many
|
||||
SELECT id, soundtrack_name, added, deleted, last_changed, path, times_played, last_played, number_of_songs, hash, uuid, fingerprint
|
||||
SELECT id, soundtrack_name, added, deleted, last_changed, path, times_played, last_played, number_of_songs, hash, uuid, fingerprint, alternative_names, cover_file, cover_source, cover_external_id, cover_manual, cover_modified, cover_checked
|
||||
FROM soundtrack
|
||||
WHERE deleted IS NULL
|
||||
ORDER BY soundtrack_name
|
||||
@@ -67,6 +81,13 @@ func (q *Queries) FindAllSoundtracks(ctx context.Context) ([]Soundtrack, error)
|
||||
&i.Hash,
|
||||
&i.Uuid,
|
||||
&i.Fingerprint,
|
||||
&i.AlternativeNames,
|
||||
&i.CoverFile,
|
||||
&i.CoverSource,
|
||||
&i.CoverExternalID,
|
||||
&i.CoverManual,
|
||||
&i.CoverModified,
|
||||
&i.CoverChecked,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -79,7 +100,7 @@ func (q *Queries) FindAllSoundtracks(ctx context.Context) ([]Soundtrack, error)
|
||||
}
|
||||
|
||||
const getAllSoundtracksIncludingDeleted = `-- name: GetAllSoundtracksIncludingDeleted :many
|
||||
SELECT id, soundtrack_name, added, deleted, last_changed, path, times_played, last_played, number_of_songs, hash, uuid, fingerprint
|
||||
SELECT id, soundtrack_name, added, deleted, last_changed, path, times_played, last_played, number_of_songs, hash, uuid, fingerprint, alternative_names, cover_file, cover_source, cover_external_id, cover_manual, cover_modified, cover_checked
|
||||
FROM soundtrack
|
||||
ORDER BY soundtrack_name
|
||||
`
|
||||
@@ -106,6 +127,13 @@ func (q *Queries) GetAllSoundtracksIncludingDeleted(ctx context.Context) ([]Soun
|
||||
&i.Hash,
|
||||
&i.Uuid,
|
||||
&i.Fingerprint,
|
||||
&i.AlternativeNames,
|
||||
&i.CoverFile,
|
||||
&i.CoverSource,
|
||||
&i.CoverExternalID,
|
||||
&i.CoverManual,
|
||||
&i.CoverModified,
|
||||
&i.CoverChecked,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -129,7 +157,7 @@ func (q *Queries) GetIdBySoundtrackName(ctx context.Context, soundtrackName stri
|
||||
}
|
||||
|
||||
const getSoundtrackById = `-- name: GetSoundtrackById :one
|
||||
SELECT id, soundtrack_name, added, deleted, last_changed, path, times_played, last_played, number_of_songs, hash, uuid, fingerprint
|
||||
SELECT id, soundtrack_name, added, deleted, last_changed, path, times_played, last_played, number_of_songs, hash, uuid, fingerprint, alternative_names, cover_file, cover_source, cover_external_id, cover_manual, cover_modified, cover_checked
|
||||
FROM soundtrack
|
||||
WHERE id = $1
|
||||
AND deleted IS NULL
|
||||
@@ -151,12 +179,19 @@ func (q *Queries) GetSoundtrackById(ctx context.Context, id int32) (Soundtrack,
|
||||
&i.Hash,
|
||||
&i.Uuid,
|
||||
&i.Fingerprint,
|
||||
&i.AlternativeNames,
|
||||
&i.CoverFile,
|
||||
&i.CoverSource,
|
||||
&i.CoverExternalID,
|
||||
&i.CoverManual,
|
||||
&i.CoverModified,
|
||||
&i.CoverChecked,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getSoundtrackByUUID = `-- name: GetSoundtrackByUUID :one
|
||||
SELECT id, soundtrack_name, added, deleted, last_changed, path, times_played, last_played, number_of_songs, hash, uuid, fingerprint
|
||||
SELECT id, soundtrack_name, added, deleted, last_changed, path, times_played, last_played, number_of_songs, hash, uuid, fingerprint, alternative_names, cover_file, cover_source, cover_external_id, cover_manual, cover_modified, cover_checked
|
||||
FROM soundtrack
|
||||
WHERE uuid = $1
|
||||
AND deleted IS NULL
|
||||
@@ -178,6 +213,13 @@ func (q *Queries) GetSoundtrackByUUID(ctx context.Context, argUuid uuid.UUID) (S
|
||||
&i.Hash,
|
||||
&i.Uuid,
|
||||
&i.Fingerprint,
|
||||
&i.AlternativeNames,
|
||||
&i.CoverFile,
|
||||
&i.CoverSource,
|
||||
&i.CoverExternalID,
|
||||
&i.CoverManual,
|
||||
&i.CoverModified,
|
||||
&i.CoverChecked,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
@@ -253,6 +295,76 @@ func (q *Queries) InsertSoundtrackWithExistingId(ctx context.Context, arg Insert
|
||||
return err
|
||||
}
|
||||
|
||||
const listCoverFiles = `-- name: ListCoverFiles :many
|
||||
SELECT cover_file::text FROM soundtrack WHERE cover_file IS NOT NULL
|
||||
`
|
||||
|
||||
func (q *Queries) ListCoverFiles(ctx context.Context) ([]string, error) {
|
||||
rows, err := q.db.Query(ctx, listCoverFiles)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []string
|
||||
for rows.Next() {
|
||||
var cover_file string
|
||||
if err := rows.Scan(&cover_file); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, cover_file)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listSoundtracksNeedingCover = `-- name: ListSoundtracksNeedingCover :many
|
||||
SELECT uuid, soundtrack_name, alternative_names
|
||||
FROM soundtrack
|
||||
WHERE deleted IS NULL
|
||||
AND cover_file IS NULL
|
||||
AND NOT cover_manual
|
||||
AND (cover_checked IS NULL OR cover_checked < $1)
|
||||
ORDER BY soundtrack_name
|
||||
`
|
||||
|
||||
type ListSoundtracksNeedingCoverRow struct {
|
||||
Uuid uuid.UUID `json:"uuid"`
|
||||
SoundtrackName string `json:"soundtrack_name"`
|
||||
AlternativeNames []string `json:"alternative_names"`
|
||||
}
|
||||
|
||||
// Active soundtracks without a cover that the job has not searched since checked_before.
|
||||
func (q *Queries) ListSoundtracksNeedingCover(ctx context.Context, checkedBefore pgtype.Timestamptz) ([]ListSoundtracksNeedingCoverRow, error) {
|
||||
rows, err := q.db.Query(ctx, listSoundtracksNeedingCover, checkedBefore)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []ListSoundtracksNeedingCoverRow
|
||||
for rows.Next() {
|
||||
var i ListSoundtracksNeedingCoverRow
|
||||
if err := rows.Scan(&i.Uuid, &i.SoundtrackName, &i.AlternativeNames); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const markCoverChecked = `-- name: MarkCoverChecked :exec
|
||||
UPDATE soundtrack SET cover_checked = now() WHERE uuid = $1
|
||||
`
|
||||
|
||||
func (q *Queries) MarkCoverChecked(ctx context.Context, argUuid uuid.UUID) error {
|
||||
_, err := q.db.Exec(ctx, markCoverChecked, argUuid)
|
||||
return err
|
||||
}
|
||||
|
||||
const markSoundtracksDeletedExcept = `-- name: MarkSoundtracksDeletedExcept :exec
|
||||
UPDATE soundtrack SET deleted=now() WHERE deleted IS NULL AND NOT (id = ANY($1::int[]))
|
||||
`
|
||||
@@ -282,6 +394,77 @@ func (q *Queries) ResetSoundtrackIdSeq(ctx context.Context) (int64, error) {
|
||||
return setval, err
|
||||
}
|
||||
|
||||
const setAlternativeNames = `-- name: SetAlternativeNames :one
|
||||
UPDATE soundtrack
|
||||
SET alternative_names = $1::text[],
|
||||
cover_checked = CASE WHEN cover_file IS NULL THEN NULL ELSE cover_checked END
|
||||
WHERE uuid = $2 AND deleted IS NULL
|
||||
RETURNING id, soundtrack_name, added, deleted, last_changed, path, times_played, last_played, number_of_songs, hash, uuid, fingerprint, alternative_names, cover_file, cover_source, cover_external_id, cover_manual, cover_modified, cover_checked
|
||||
`
|
||||
|
||||
type SetAlternativeNamesParams struct {
|
||||
Names []string `json:"names"`
|
||||
Uuid uuid.UUID `json:"uuid"`
|
||||
}
|
||||
|
||||
// Replaces the alternative names. A soundtrack without a cover is searched again with them.
|
||||
func (q *Queries) SetAlternativeNames(ctx context.Context, arg SetAlternativeNamesParams) (Soundtrack, error) {
|
||||
row := q.db.QueryRow(ctx, setAlternativeNames, arg.Names, arg.Uuid)
|
||||
var i Soundtrack
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.SoundtrackName,
|
||||
&i.Added,
|
||||
&i.Deleted,
|
||||
&i.LastChanged,
|
||||
&i.Path,
|
||||
&i.TimesPlayed,
|
||||
&i.LastPlayed,
|
||||
&i.NumberOfSongs,
|
||||
&i.Hash,
|
||||
&i.Uuid,
|
||||
&i.Fingerprint,
|
||||
&i.AlternativeNames,
|
||||
&i.CoverFile,
|
||||
&i.CoverSource,
|
||||
&i.CoverExternalID,
|
||||
&i.CoverManual,
|
||||
&i.CoverModified,
|
||||
&i.CoverChecked,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const setCover = `-- name: SetCover :exec
|
||||
UPDATE soundtrack
|
||||
SET cover_file = $1,
|
||||
cover_source = $2,
|
||||
cover_external_id = $3,
|
||||
cover_manual = $4,
|
||||
cover_modified = now(),
|
||||
cover_checked = now()
|
||||
WHERE uuid = $5
|
||||
`
|
||||
|
||||
type SetCoverParams struct {
|
||||
File *string `json:"file"`
|
||||
Source *string `json:"source"`
|
||||
ExternalID *string `json:"external_id"`
|
||||
Manual bool `json:"manual"`
|
||||
Uuid uuid.UUID `json:"uuid"`
|
||||
}
|
||||
|
||||
func (q *Queries) SetCover(ctx context.Context, arg SetCoverParams) error {
|
||||
_, err := q.db.Exec(ctx, setCover,
|
||||
arg.File,
|
||||
arg.Source,
|
||||
arg.ExternalID,
|
||||
arg.Manual,
|
||||
arg.Uuid,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const setSoundtrackDeletionDate = `-- name: SetSoundtrackDeletionDate :exec
|
||||
UPDATE soundtrack SET deleted=now() WHERE deleted IS NULL
|
||||
`
|
||||
|
||||
Reference in new issue
Block a user