feat(images): album covers from the Cover Art Archive
- The scan reads the MusicBrainz release group id of an album from the tags of its
files (MUSICBRAINZ_RELEASEGROUPID, a vorbis comment or an ID3 frame) and stores it in
albums.mbid: it is taken only when the file has an album and exactly one valid id, an
album keeps the one it has, and an id that another album owns is not given twice. A full
rescan reads it for music that was already indexed.
- New provider "coverartarchive" (off by default, no key, contact in the User-Agent). When
an album has no picture in the user's own files, which always come first, its cover is
asked from the archive by that id and never by a title: only the id leaves the server.
The same GET /albums/{id}/cover; the record keeps the source. An outage is a 502
provider_unavailable and is not remembered, and an older cover is served in its place.
- The archive answers with three redirects (coverartarchive.org, archive.org, a data node
whose name changes). The outbound client can now follow redirects, but only the ones the
caller allows, up to a limit; here that is https on archive.org and its subdomains.
- A purge counter now guards the covers too: a read that started before the provider's data
was taken back is not kept. Removing the provider's data forgets the covers that came from
it and deletes their files, and leaves the ones from the user's files.
- Spec, contract and HTTP tests, README updated; the facts of the real archive are written down.
Closes #43
This commit is contained in:
parent
699970aac9
commit
d5ed0f2e9c
1 changed files with 1349 additions and 92 deletions
|
|
@ -27,6 +27,7 @@ import (
|
|||
"codeberg.org/squidd/sonda/internal/listening"
|
||||
"codeberg.org/squidd/sonda/internal/providers"
|
||||
"codeberg.org/squidd/sonda/internal/providers/commons"
|
||||
"codeberg.org/squidd/sonda/internal/providers/coverart"
|
||||
"codeberg.org/squidd/sonda/internal/providers/lastfm"
|
||||
"codeberg.org/squidd/sonda/internal/providers/musicbrainz"
|
||||
"codeberg.org/squidd/sonda/internal/providers/outbound"
|
||||
|
|
@ -58,9 +59,10 @@ const (
|
|||
|
||||
// commonsMaxBody leaves room for a 960 pixel copy of a picture, which is some hundred kilobytes.
|
||||
const (
|
||||
commonsMinInterval = 300 * time.Millisecond
|
||||
commonsAPIMaxBody = 1 << 20
|
||||
commonsMaxBody = 8 << 20
|
||||
coverartMinInterval = 500 * time.Millisecond
|
||||
commonsMinInterval = 300 * time.Millisecond
|
||||
commonsAPIMaxBody = 1 << 20
|
||||
commonsMaxBody = 8 << 20
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
|
@ -183,19 +185,48 @@ func run() error {
|
|||
pictures.SetArtistSource(&images.CommonsSource{
|
||||
Artists: discoveryRepo, Settings: providerSettings, MusicBrainz: musicbrainzClient, Commons: commonsClient,
|
||||
})
|
||||
// Covers from the Cover Art Archive when the user's own files have none. The archive
|
||||
// answers with redirects (to archive.org and its data nodes): they are followed, to nobody else.
|
||||
coverartClient := coverart.New(outbound.New(outbound.Options{
|
||||
UserAgentFunc: providerSettings.UserAgentFor(providers.CoverArtArchive, version),
|
||||
MinInterval: coverartMinInterval,
|
||||
MaxBody: commonsMaxBody,
|
||||
FollowRedirect: coverart.RedirectAllowed,
|
||||
}), coverart.BaseURL)
|
||||
providerSettings.SetChecker(providers.CoverArtArchive, func(ctx context.Context, _ string) error {
|
||||
return coverartClient.Probe(ctx)
|
||||
})
|
||||
pictures.SetAlbumRemote(&images.CoverArtSource{Albums: imageRepo, Settings: providerSettings, Client: coverartClient})
|
||||
providerSettings.SetPurger(func(ctx context.Context, name string) (providers.Purged, error) {
|
||||
if name == providers.Commons {
|
||||
pictures.BeginArtistPurge() // a picture being read right now must not be kept
|
||||
switch name {
|
||||
case providers.Commons:
|
||||
pictures.BeginPurge() // a picture being read right now must not be kept
|
||||
out, err := discoveryRepo.Purge(ctx, name)
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
// The records are gone; the pictures themselves are files in the data folder.
|
||||
if _, err := pictures.RemoveArtistFiles(); err != nil {
|
||||
return out, fmt.Errorf("remove the pictures: %w", err)
|
||||
}
|
||||
return out, nil
|
||||
case providers.CoverArtArchive:
|
||||
pictures.BeginPurge()
|
||||
// The records say which albums have a cover from the archive: read them before they go.
|
||||
ids, err := imageRepo.OwnersBySource(ctx, "album", images.SourceCoverArt)
|
||||
if err != nil {
|
||||
return providers.Purged{}, err
|
||||
}
|
||||
out, err := discoveryRepo.Purge(ctx, name)
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
if _, err := pictures.RemoveAlbumFiles(ids); err != nil {
|
||||
return out, fmt.Errorf("remove the covers: %w", err)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
out, err := discoveryRepo.Purge(ctx, name)
|
||||
if err != nil || name != providers.Commons {
|
||||
return out, err
|
||||
}
|
||||
// The records are gone; the pictures themselves are files in the data folder.
|
||||
if _, err := pictures.RemoveArtistFiles(); err != nil {
|
||||
return out, fmt.Errorf("remove the pictures: %w", err)
|
||||
}
|
||||
return out, nil
|
||||
return discoveryRepo.Purge(ctx, name)
|
||||
})
|
||||
discoveries := discovery.NewService(discoveryRepo, lastfmClient, providerSettings, logger)
|
||||
tags := discovery.NewTagService(discoveryRepo, map[string]discovery.TagSource{
|
||||
|
|
|
|||
Loading…
Reference in a new issue