Vellum/internal/providers/provider.go
mithral 3207b89205 Add a TMDB provider for video: films and television
Five providers existed and none covered the "video" kind, so every movie
and show got a title from its filename and nothing else - no poster, no
plot, no cast, no year. That is the gap between "a folder of videos with a
nice web UI" and a media server.

TMDB keeps films and television in separate namespaces behind separate
endpoints, and a Query does not say which one it is. Rather than guessing
from the title, Search asks both at once and merges by TMDB popularity,
which is the only ranking signal available without a second request per
item.

Speed is the point, so the two requests run concurrently over one derived
context on a single shared client, and whichever fails first cancels the
other: one dead endpoint cannot hold the result hostage while the healthy
one is waited out. Verified red-green - serialising the two fetches makes
TestTMDBSearchesConcurrently fail at 2s.

Two corrections made in review, both found by reading the code rather than
trusting the report:

1. ExternalID was the bare TMDB id, so film 603 and show 603 - different
   works - would have been stored identically in provider_ids. Namespaced
   as movie/603 and tv/603. Nothing resolves an item from an external_id
   today, so this was latent rather than broken, but the table is populated
   as enrichment runs and correcting it later would have needed a data
   migration. Pinned by TestTMDBExternalIDIsNamespaced.

2. Query.Year was added but never populated, so the year filter - the main
   way to tell a remake from the original - never applied. catalog.Item
   already carried Year; the enricher now passes it.

No cover bytes are fetched here: the provider returns image URLs and the
existing enricher caches what it uses. A missing key logs the env var and
skips, so the feature is discoverable from the boot log.

Not verified: the live TMDB API. Tests run against httptest servers only.
2026-10-12 01:17:48 -04:00

81 lines
2.3 KiB
Go

// Package providers talks to external metadata catalogues.
//
// One client per service, no database, no policy: a provider turns a Query into
// candidates and knows nothing about what the caller does with them. Matching
// policy and persistence belong to the caller (internal/metadata), which is
// what keeps these clients testable against an httptest server alone.
//
// Providers are optional by design: a library with no network still browses,
// because a failed lookup is recorded and skipped, never surfaced as a scan or
// request failure.
package providers
import (
"context"
"strings"
"sync"
)
// Query is what a provider is asked to match.
type Query struct {
Title string
// Year narrows a search when the caller knows it. Providers that cannot use
// it ignore it.
Year int
Author string
ISBN string
Kind string
}
// Candidate is one possible match returned by a provider.
type Candidate struct {
Provider string `json:"provider"`
ExternalID string `json:"external_id"`
Title string `json:"title"`
Authors []string `json:"authors"`
Year int `json:"year"`
Publisher string `json:"publisher"`
Language string `json:"language"`
Description string `json:"description"`
ISBN string `json:"isbn"`
CoverURL string `json:"cover_url"`
}
// Provider is one external catalogue.
type Provider interface {
// Name is the stable identifier stored in provider_ids.provider.
Name() string
// Kinds lists the media kinds this provider can match.
Kinds() []string
// Search returns candidates, best first. An empty result is not an error.
Search(ctx context.Context, q Query) ([]Candidate, error)
}
var (
mu sync.RWMutex
providers []Provider
)
// Register adds a provider. Later registrations are consulted later, so
// registering a primary provider before a fallback gives the intended order.
func Register(p Provider) {
mu.Lock()
defer mu.Unlock()
providers = append(providers, p)
}
// For returns the providers that handle a media kind, in registration order.
func For(kind string) []Provider {
mu.RLock()
defer mu.RUnlock()
var out []Provider
for _, p := range providers {
for _, k := range p.Kinds() {
if strings.EqualFold(k, kind) {
out = append(out, p)
break
}
}
}
return out
}