sonda/internal/about/source.go
tgiachi a880b87a9e chore: move the module path to git.kmalloc.org/squid/sonda
Migrating hosting from Codeberg to git.kmalloc.org. Renames the
module path in go.mod, every internal import, and the OCI image
source label in the Dockerfile. Build and full test suite verified
green after the rename.
2026-11-15 17:30:29 +01:00

51 lines
1.7 KiB
Go

package about
import (
"context"
"errors"
"git.kmalloc.org/squid/sonda/internal/providers/musicbrainz"
"git.kmalloc.org/squid/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) (string, map[string]string, error) {
resolved, pages, err := w.Wikipedia.Pages(ctx, item, languages)
return resolved, 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
}