- A panic while counting no longer leaves the job marked as running; a panic in the work of one item is a failure of that item, and a panic in the list ends the job with a reason. - A stop while the items are counted is recorded as a stop instead of a 500, and a count that fails leaves the status as the previous job left it. - An artist that lost its MusicBrainz id since it was listed is skipped by the pictures job, as the spec says, instead of counting as a failure. - A picture that cannot be used now (refused download, bytes that are not an image) is a failure when a good picture is stored, so a refresh never throws it away. - The outcomes the artist jobs report share their constants in the jobs package. - The spec no longer calls each job "the one request" that goes through the library. - The test that waited with a sleep for the count now waits for a signal.
138 lines
4.5 KiB
Go
138 lines
4.5 KiB
Go
package images
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"codeberg.org/squidd/sonda/internal/discovery"
|
|
"codeberg.org/squidd/sonda/internal/providers"
|
|
"codeberg.org/squidd/sonda/internal/providers/commons"
|
|
"codeberg.org/squidd/sonda/internal/providers/musicbrainz"
|
|
)
|
|
|
|
// ArtistLookup finds an artist by id.
|
|
type ArtistLookup interface {
|
|
Artist(ctx context.Context, id int64) (discovery.Artist, error)
|
|
}
|
|
|
|
// Switches says whether a provider may be used right now.
|
|
type Switches interface {
|
|
Active(ctx context.Context, name string) (providers.Settings, bool, error)
|
|
}
|
|
|
|
// WikidataLink gives the Wikidata item that MusicBrainz links to an artist.
|
|
type WikidataLink interface {
|
|
WikidataID(ctx context.Context, musicbrainzID string) (string, error)
|
|
}
|
|
|
|
// CommonsAPI is what the chain needs from Wikidata and Commons: *commons.Client.
|
|
type CommonsAPI interface {
|
|
FileName(ctx context.Context, item string) (string, error)
|
|
Info(ctx context.Context, name string) (commons.File, error)
|
|
Download(ctx context.Context, pictureURL string) ([]byte, error)
|
|
}
|
|
|
|
// CommonsSource finds the picture of an artist by following identifiers, never a name:
|
|
// the artist's MusicBrainz id, the Wikidata item MusicBrainz links to it, the Commons file
|
|
// Wikidata names for it, and, if its licence is free, a copy of it. It needs the commons
|
|
// provider and the musicbrainz one to be on, since the artist's id goes to both.
|
|
type CommonsSource struct {
|
|
Artists ArtistLookup
|
|
Settings Switches
|
|
MusicBrainz WikidataLink
|
|
Commons CommonsAPI
|
|
}
|
|
|
|
// active says whether both providers are on.
|
|
func (c *CommonsSource) active(ctx context.Context) (bool, error) {
|
|
for _, name := range []string{providers.Commons, providers.MusicBrainz} {
|
|
_, on, err := c.Settings.Active(ctx, name)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if !on {
|
|
return false, nil
|
|
}
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
// failed turns what went wrong into ErrSourceUnavailable, keeping the cause. When the
|
|
// caller's own context has ended (it left, or ran out of time) that is returned instead:
|
|
// it is not the provider's fault. A deadline buried in the error is not a sign of that, since
|
|
// the outbound client's own timeout produces one when the provider is merely slow.
|
|
func failed(ctx context.Context, err error) error {
|
|
if ctxErr := ctx.Err(); ctxErr != nil {
|
|
return ctxErr
|
|
}
|
|
return fmt.Errorf("%w: %w", ErrSourceUnavailable, err)
|
|
}
|
|
|
|
// ArtistImage implements ArtistSource.
|
|
func (c *CommonsSource) ArtistImage(ctx context.Context, artistID int64) (Found, error) {
|
|
artist, err := c.Artists.Artist(ctx, artistID)
|
|
if errors.Is(err, discovery.ErrNotFound) {
|
|
return Found{}, ErrArtistNotFound
|
|
}
|
|
if err != nil {
|
|
return Found{}, err
|
|
}
|
|
if on, err := c.active(ctx); err != nil {
|
|
return Found{}, err
|
|
} else if !on {
|
|
return Found{}, ErrProviderDisabled
|
|
}
|
|
if artist.MBID == "" { // Sonda never looks an id up by name
|
|
return Found{}, ErrNoID
|
|
}
|
|
|
|
item, err := c.MusicBrainz.WikidataID(ctx, artist.MBID)
|
|
switch {
|
|
case errors.Is(err, musicbrainz.ErrNotFound), errors.Is(err, musicbrainz.ErrBadRequest):
|
|
return Found{}, ErrNoImage
|
|
case err != nil:
|
|
return Found{}, failed(ctx, err)
|
|
case item == "":
|
|
return Found{}, ErrNoImage
|
|
}
|
|
name, err := c.Commons.FileName(ctx, item)
|
|
switch {
|
|
case errors.Is(err, commons.ErrNotFound), errors.Is(err, commons.ErrBadRequest):
|
|
return Found{}, ErrNoImage
|
|
case err != nil:
|
|
return Found{}, failed(ctx, err)
|
|
case name == "":
|
|
return Found{}, ErrNoImage
|
|
}
|
|
file, err := c.Commons.Info(ctx, name)
|
|
switch {
|
|
case errors.Is(err, commons.ErrNotFound), errors.Is(err, commons.ErrNotFree),
|
|
errors.Is(err, commons.ErrUnusable), errors.Is(err, commons.ErrBadRequest):
|
|
return Found{}, ErrNoImage
|
|
case err != nil:
|
|
return Found{}, failed(ctx, err)
|
|
}
|
|
// The admin may have switched the provider off while the chain was being followed:
|
|
// do not download, and do not hand over, what they no longer allow.
|
|
if on, err := c.active(ctx); err != nil {
|
|
return Found{}, err
|
|
} else if !on {
|
|
return Found{}, ErrProviderDisabled
|
|
}
|
|
data, err := c.Commons.Download(ctx, file.ThumbURL)
|
|
switch {
|
|
case errors.Is(err, commons.ErrBadRequest), errors.Is(err, commons.ErrNotFound), errors.Is(err, commons.ErrUnusable):
|
|
return Found{}, fmt.Errorf("%w: %w", ErrNoImage, ErrBadPicture)
|
|
case err != nil:
|
|
return Found{}, failed(ctx, err)
|
|
}
|
|
if on, err := c.active(ctx); err != nil {
|
|
return Found{}, err
|
|
} else if !on {
|
|
return Found{}, ErrProviderDisabled
|
|
}
|
|
return Found{Data: data, Credit: Credit{
|
|
Author: file.Author, License: file.License, LicenseURL: file.LicenseURL, PageURL: file.PageURL,
|
|
}}, nil
|
|
}
|