- Every provider can look an entry up again by its ID (TMDB, TheTVDB,
IGDB, Deezer, MusicBrainz, Wikipedia); search and lookup share parsing
- POST /api/items/{id}/refresh re-applies title, subtitle, year,
description, link, details and artwork. Kept: the "why" note, rank,
theater production/venue/cast and a theater entry's year, the Deezer
preview match. Empty source fields don't erase existing values
- New items.image_custom column (migration 4; existing uploads marked)
records artwork the owner chose; refresh keeps it unless asked
- Refused for custom lists and hand-added entries; entries the source
dropped are reported, and a refresh whose title shares no words with
the current one is rejected as a likely bad ID
- Editor: refresh button per row (asks about the owner's own artwork),
and "Refresh all" with a confirm step, replace-my-art checkbox,
progress with Stop, and a summary of problems
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
129 lines
3.6 KiB
Go
129 lines
3.6 KiB
Go
package providers
|
|
|
|
import (
|
|
"context"
|
|
"net/url"
|
|
"regexp"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// wikipedia finds stage works (musicals, plays, operas) on English
|
|
// Wikipedia. There's no free, open theater database with an API, and
|
|
// Wikipedia has the best coverage plus poster/logo art for most shows. The
|
|
// specific production (Broadway, a tour, …) is chosen by the owner.
|
|
type wikipedia struct {
|
|
r *Registry
|
|
// stage limits results to musicals, plays and operas. Custom lists
|
|
// search all of Wikipedia.
|
|
stage bool
|
|
}
|
|
|
|
func (w *wikipedia) Name() string { return "wikipedia" }
|
|
func (w *wikipedia) Label() string { return "Wikipedia" }
|
|
|
|
const theaterTemplates = `hastemplate:"Infobox musical|Infobox play|Infobox opera"`
|
|
|
|
func (w *wikipedia) Search(ctx context.Context, query string) ([]Result, error) {
|
|
if !w.stage {
|
|
return w.search(ctx, query)
|
|
}
|
|
res, err := w.search(ctx, query+" "+theaterTemplates)
|
|
if err == nil && len(res) == 0 {
|
|
res, err = w.search(ctx, query)
|
|
}
|
|
return res, err
|
|
}
|
|
|
|
var leadingYear = regexp.MustCompile(`^(1[5-9]\d\d|20\d\d)\s+`)
|
|
|
|
func (w *wikipedia) search(ctx context.Context, query string) ([]Result, error) {
|
|
return w.pages(ctx, url.Values{"generator": {"search"}, "gsrsearch": {query}, "gsrlimit": {"15"}})
|
|
}
|
|
|
|
// Lookup refreshes one page by its page ID.
|
|
func (w *wikipedia) Lookup(ctx context.Context, providerID string) (Result, error) {
|
|
res, err := w.pages(ctx, url.Values{"pageids": {providerID}})
|
|
if err != nil {
|
|
return Result{}, err
|
|
}
|
|
if len(res) == 0 {
|
|
return Result{}, ErrGone
|
|
}
|
|
return res[0], nil
|
|
}
|
|
|
|
// pages runs a query for pages (a search, or specific page IDs) and shapes
|
|
// them into results.
|
|
func (w *wikipedia) pages(ctx context.Context, which url.Values) ([]Result, error) {
|
|
q := url.Values{
|
|
"action": {"query"},
|
|
"format": {"json"},
|
|
"formatversion": {"2"},
|
|
"prop": {"pageimages|description|info|extracts"},
|
|
"piprop": {"thumbnail"},
|
|
"pithumbsize": {"800"},
|
|
"pilicense": {"any"},
|
|
"inprop": {"url"},
|
|
"exintro": {"1"},
|
|
"explaintext": {"1"},
|
|
"exsentences": {"3"},
|
|
"exlimit": {"max"},
|
|
}
|
|
for k, v := range which {
|
|
q[k] = v
|
|
}
|
|
var resp struct {
|
|
Query struct {
|
|
Pages []struct {
|
|
PageID int64 `json:"pageid"`
|
|
Missing bool `json:"missing"`
|
|
Title string `json:"title"`
|
|
Index int `json:"index"`
|
|
Description string `json:"description"`
|
|
Extract string `json:"extract"`
|
|
FullURL string `json:"fullurl"`
|
|
Thumbnail struct {
|
|
Source string `json:"source"`
|
|
} `json:"thumbnail"`
|
|
} `json:"pages"`
|
|
} `json:"query"`
|
|
}
|
|
if err := w.r.getJSON(ctx, "https://en.wikipedia.org/w/api.php?"+q.Encode(), nil, &resp); err != nil {
|
|
return nil, err
|
|
}
|
|
pages := resp.Query.Pages
|
|
sort.Slice(pages, func(i, j int) bool { return pages[i].Index < pages[j].Index })
|
|
out := make([]Result, 0, len(pages))
|
|
for _, p := range pages {
|
|
if p.Missing {
|
|
continue
|
|
}
|
|
title := p.Title
|
|
// "Hamilton (musical)" → "Hamilton"
|
|
if i := strings.LastIndex(title, " ("); i > 0 && strings.HasSuffix(title, ")") {
|
|
title = title[:i]
|
|
}
|
|
desc := leadingYear.ReplaceAllString(p.Description, "")
|
|
if desc != "" {
|
|
desc = strings.ToUpper(desc[:1]) + desc[1:]
|
|
}
|
|
img := p.Thumbnail.Source
|
|
if i := strings.Index(img, "?"); i > 0 {
|
|
img = img[:i]
|
|
}
|
|
out = append(out, Result{
|
|
Provider: "wikipedia",
|
|
ProviderID: strconv.FormatInt(p.PageID, 10),
|
|
Title: title,
|
|
Subtitle: desc,
|
|
Year: yearOf(p.Description),
|
|
ImageURL: img,
|
|
ExternalURL: p.FullURL,
|
|
Overview: truncate(p.Extract, maxDescription),
|
|
})
|
|
}
|
|
return out, nil
|
|
}
|