sonda/internal/about/source.go
tgiachi 48cb903115 feat(about): artist descriptions from Wikipedia
- GET /artists/{id}/description shows what Wikipedia says about an artist: a short
  text with its page, a link and the licence (CC BY-SA 4.0).
- New provider "wikipedia" (off by default, no key, contact in the User-Agent, which
  Wikimedia requires). The way to the text is a chain of ids, never a name: the
  artist's MusicBrainz id, the Wikidata item MusicBrainz links to it, the Wikipedia
  page Wikidata names in the first configured language that has one. A page is used
  only if it is an article about that same item.
- [wikipedia] languages in the config (one to five codes of two or three lower case
  letters, checked before they are sent: an unknown site makes Wikidata answer with
  every language).
- Stored 30 days (a day when there was nothing to find), served stale if Wikipedia
  fails, removed with the provider's data (the purge now reports descriptions).
- MusicBrainz client: WikidataID. Wikipedia client for Wikidata and the summary,
  which also reads Wikidata's errors that come with HTTP 200.
- artist_descriptions table (migration 0014, SQLite and PostgreSQL).
- Spec, contract test, README and example config updated.

Closes #39
2026-11-11 21:36:27 +01:00

51 lines
1.7 KiB
Go

package about
import (
"context"
"errors"
"codeberg.org/squidd/sonda/internal/providers/musicbrainz"
"codeberg.org/squidd/sonda/internal/providers/wikipedia"
)
// WikipediaSource follows the chain with the real clients: MusicBrainz for the link to
// Wikidata, Wikidata for the pages, Wikipedia for the summary.
type WikipediaSource struct {
MusicBrainz *musicbrainz.Client
Wikipedia *wikipedia.Client
}
// notFound turns what means "there is nothing to find" into ErrSourceNotFound. An
// input the clients refuse (an id that is not one) is nothing to find either: asking
// again would not change that.
func notFound(err error) error {
if errors.Is(err, musicbrainz.ErrNotFound) || errors.Is(err, musicbrainz.ErrBadRequest) ||
errors.Is(err, wikipedia.ErrNotFound) || errors.Is(err, wikipedia.ErrBadRequest) {
return ErrSourceNotFound
}
return err
}
// WikidataID implements Source.
func (w WikipediaSource) WikidataID(ctx context.Context, musicbrainzID string) (string, error) {
id, err := w.MusicBrainz.WikidataID(ctx, musicbrainzID)
return id, notFound(err)
}
// Pages implements Source.
func (w WikipediaSource) Pages(ctx context.Context, item string, languages []string) (map[string]string, error) {
pages, err := w.Wikipedia.Pages(ctx, item, languages)
return pages, notFound(err)
}
// Summary implements Source.
func (w WikipediaSource) Summary(ctx context.Context, language, title string) (Summary, error) {
s, err := w.Wikipedia.Summary(ctx, language, title)
if err != nil {
return Summary{}, notFound(err)
}
return Summary{
Type: s.Type, Title: s.Title, Lang: s.Lang, Extract: s.Extract, Description: s.Description,
URL: s.URL, WikibaseItem: s.WikibaseItem,
}, nil
}