feat(images): free photos of artists from Wikimedia Commons

- GET and HEAD /artists/{id}/image serve a free picture of an artist as a JPEG
  (size=small 300 px or large 1000 px, token in the query allowed for an <img>), and
  GET /artists/{id}/image/credit gives the author, the licence and the page of the file,
  to be shown next to it.
- New provider "commons" (off by default, no key, contact in the User-Agent), which also
  needs MusicBrainz on. The way to the picture uses ids only, never a name: the artist's
  MusicBrainz id, the Wikidata item MusicBrainz links to it, the file Wikidata names for
  it (P18), Commons for its licence, and a 960 px copy from a Wikimedia picture host.
- Only free licences: public domain, CC0, CC BY and CC BY-SA. Non-commercial,
  no-derivatives, GFDL alone, fair use and unknown licences are refused, since a copy is
  kept. The author is plain text, the licence address is kept only on creativecommons.org,
  and the download only from https thumb.wikimedia.org or upload.wikimedia.org.
- Built on the first request, kept in the data folder: 30 days, or a week when there is no
  free picture; an older copy is served when Commons cannot be reached; a failure is not
  remembered. Credit changes change the ETag.
- Removing the provider's data forgets the records and deletes the files; a read that started
  before it is not saved over it.
- images table gets the credit (migration 0015); the purge reports images.
- Spec, contract test, README updated; the facts of the real APIs are written down.

Closes #41
This commit is contained in:
tgiachi 2026-11-11 22:03:32 +01:00
commit e752d15536

View file

@ -26,6 +26,7 @@ import (
"codeberg.org/squidd/sonda/internal/library"
"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/lastfm"
"codeberg.org/squidd/sonda/internal/providers/musicbrainz"
"codeberg.org/squidd/sonda/internal/providers/outbound"
@ -55,6 +56,12 @@ const (
wikipediaMaxBody = 1 << 20
)
// commonsMaxBody leaves room for a 960 pixel copy of a picture, which is some hundred kilobytes.
const (
commonsMinInterval = 300 * time.Millisecond
commonsMaxBody = 8 << 20
)
func main() {
if err := run(); err != nil {
fmt.Fprintln(os.Stderr, "sondad:", err)
@ -151,9 +158,34 @@ func run() error {
providerSettings.SetChecker(providers.Wikipedia, func(ctx context.Context, _ string) error {
return wikipediaClient.Probe(ctx)
})
// Pictures of artists from Wikimedia Commons, through Wikidata, under free licences only.
commonsClient := commons.New(outbound.New(outbound.Options{
UserAgentFunc: providerSettings.UserAgentFor(providers.Commons, version),
MinInterval: commonsMinInterval,
MaxBody: commonsMaxBody,
}), commons.WikidataURL, commons.CommonsURL)
providerSettings.SetChecker(providers.Commons, func(ctx context.Context, _ string) error {
return commonsClient.Probe(ctx)
})
if err := os.MkdirAll(cfg.Storage.DataDir, 0o700); err != nil {
return fmt.Errorf("create the data folder: %w", err)
}
imageRepo := storage.NewImageRepo(db)
pictures := images.NewService(cfg.Storage.DataDir, imageRepo, imageRepo)
discoveryRepo := storage.NewDiscoveryRepo(db)
pictures.SetArtistSource(&images.CommonsSource{
Artists: discoveryRepo, Settings: providerSettings, MusicBrainz: musicbrainzClient, Commons: commonsClient,
})
providerSettings.SetPurger(func(ctx context.Context, name string) (providers.Purged, error) {
return discoveryRepo.Purge(ctx, name)
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
})
discoveries := discovery.NewService(discoveryRepo, lastfmClient, providerSettings, logger)
tags := discovery.NewTagService(discoveryRepo, map[string]discovery.TagSource{
@ -165,30 +197,27 @@ func run() error {
tasteService := taste.NewService(tasteRepo, taste.Options{MinListen: cfg.Listening.MinListen(), HalfLife: cfg.Taste.HalfLife()})
userSettings := usersettings.NewService(storage.NewUserSettingsRepo(db))
feedbacks := feedback.NewService(storage.NewFeedbackRepo(db))
if err := os.MkdirAll(cfg.Storage.DataDir, 0o700); err != nil {
return fmt.Errorf("create the data folder: %w", err)
}
imageRepo := storage.NewImageRepo(db)
weights := discover.Weights{
Affinity: cfg.Scoring.WeightAffinity, Genre: cfg.Scoring.WeightGenre,
Preference: cfg.Scoring.WeightPreference, Discovery: cfg.Scoring.WeightDiscovery,
}
router := httpapi.NewRouter(httpapi.Deps{
Logger: logger,
Auth: auth.NewService(storage.NewUserRepo(db), storage.NewRefreshRepo(db), tokens, cfg.Auth.RefreshTTL()),
Tokens: tokens,
Listening: listening.NewService(storage.NewListeningRepo(db), cfg.Listening.MinListen()),
Scan: scanControl{ctx: ctx, m: scans},
Libraries: libraries,
Catalog: catalog.NewService(storage.NewCatalogRepo(db)),
Providers: providerSettings,
Discovery: discoveries,
Tags: tags,
Taste: tasteService,
Discover: discover.NewService(tasteService, discoveries, tags, tasteRepo, userSettings, feedbacks, weights),
Feedback: feedbacks,
Images: images.NewService(cfg.Storage.DataDir, imageRepo, imageRepo),
Logger: logger,
Auth: auth.NewService(storage.NewUserRepo(db), storage.NewRefreshRepo(db), tokens, cfg.Auth.RefreshTTL()),
Tokens: tokens,
Listening: listening.NewService(storage.NewListeningRepo(db), cfg.Listening.MinListen()),
Scan: scanControl{ctx: ctx, m: scans},
Libraries: libraries,
Catalog: catalog.NewService(storage.NewCatalogRepo(db)),
Providers: providerSettings,
Discovery: discoveries,
Tags: tags,
Taste: tasteService,
Discover: discover.NewService(tasteService, discoveries, tags, tasteRepo, userSettings, feedbacks, weights),
Feedback: feedbacks,
Images: pictures,
ArtistImages: pictures,
About: about.NewService(discoveryRepo, about.WikipediaSource{MusicBrainz: musicbrainzClient, Wikipedia: wikipediaClient},
providerSettings, cfg.Wikipedia.Languages, logger),
Settings: userSettings,