Four extractions, each chosen because the two halves have different callers,
different failure modes or different testing needs. Not a line-count exercise.
internal/home out of internal/media
- media serves item RESOURCES (detail, file, cover, progress); home computes
AGGREGATES (the five shelves). The 2328-line package was doing both.
- media keeps POST /items/{id}/view because it is item-scoped and must stay in
the package that authorizes the item; it imports home.RecordView for that one
thing. home does not import media.
- Verified live: all five shelves non-empty, and two consecutive /home calls
return different explore ids, which is the non-determinism the Explore rewrite
exists for.
internal/scanner out of internal/library
- The scanner walks the filesystem and reconciles; library CRUD is an admin HTTP
surface. Different callers (a job vs a request), different failure modes.
- The registry deliberately STAYED in internal/library: three of its four callers
are not the scanner (modules register types, handlers serve the list, metadata
reads the group mode, subsonic calls CleanTitle). It is a property of the kind,
not of the scan. The one genuinely scanner-owned helper became
library.ExtensionsFor.
- Two tests moved with the code rather than being rewritten; they now exercise
the boundary by calling library.ListMissing across it.
- Verified live: first scan updates, rescan reports unchanged=N, so change
detection still short-circuits.
internal/providers out of internal/metadata
- Providers talk to third-party APIs; the enrich job owns the matching policy and
the database writes. The split's whole point is that a provider is testable
WITHOUT a database, so a DB-free test was added and OpenLibrary gained a
BaseURL override to make it possible.
- providers imports zero internal packages — a true leaf.
- The matching policy (searchTitle, artistHint, pickBest) is untouched. It exists
because real false matches were found.
dto packages out of internal/jellyfin and internal/subsonic
- The wire types and their mapping are now separate from the handler logic, so
the contract with clients we do not control is reviewable on its own.
- Both dto packages are PURE: zero references to sql.DB or net/http, verified by
grep. That is what makes them reviewable.
- No field name, tag or error code changed.
internal/arch did its job: it failed the build twice during this phase, once for
each new package that was not yet placed in a layer. That is the rule working,
not an obstacle.
Verified: gofmt clean, build exit 0, vet exit 0, 23 packages pass, svelte-check
0/0, and end to end against a running server — home shelves, a queued scan
reporting unchanged, Jellyfin /Items, and Subsonic ping.
Process note: one agent (dto) stopped mid-task and left the tree red — the dto
packages unlayered and its test still referencing constants that had moved. The
arch test caught it immediately. I finished it by hand: exported constant
references in the test, and both dto packages placed at layer 3.
Still open from Phase 1: internal/jellyfin has no test files, catalog is
item-centric while Subsonic is file-first, and subsonic loadAlbum now scopes to
the item's own library.
139 lines
3.5 KiB
Go
139 lines
3.5 KiB
Go
package providers
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// ComicVine matches comic series. It requires a free API key, so the provider
|
|
// is only registered when one is configured.
|
|
type ComicVine struct {
|
|
APIKey string
|
|
BaseURL string
|
|
Client *http.Client
|
|
}
|
|
|
|
// NewComicVine builds the provider. An empty key yields a provider that finds
|
|
// nothing, so a misconfiguration degrades rather than erroring.
|
|
func NewComicVine(apiKey string) *ComicVine {
|
|
return &ComicVine{
|
|
APIKey: apiKey,
|
|
BaseURL: "https://comicvine.gamespot.com/api",
|
|
Client: &http.Client{Timeout: 15 * time.Second},
|
|
}
|
|
}
|
|
|
|
func (p *ComicVine) Name() string { return "comicvine" }
|
|
|
|
func (p *ComicVine) Kinds() []string { return []string{"comics"} }
|
|
|
|
// cvResponse is the subset of the ComicVine search payload we read.
|
|
type cvResponse struct {
|
|
StatusCode int `json:"status_code"`
|
|
Results []struct {
|
|
ID int `json:"id"`
|
|
Name string `json:"name"`
|
|
StartYear string `json:"start_year"`
|
|
Description string `json:"description"`
|
|
Publisher struct {
|
|
Name string `json:"name"`
|
|
} `json:"publisher"`
|
|
Image struct {
|
|
MediumURL string `json:"medium_url"`
|
|
OriginalURL string `json:"original_url"`
|
|
SmallURL string `json:"small_url"`
|
|
} `json:"image"`
|
|
} `json:"results"`
|
|
}
|
|
|
|
func (p *ComicVine) Search(ctx context.Context, q Query) ([]Candidate, error) {
|
|
if p.APIKey == "" || strings.TrimSpace(q.Title) == "" {
|
|
return nil, nil
|
|
}
|
|
|
|
params := url.Values{}
|
|
params.Set("api_key", p.APIKey)
|
|
params.Set("format", "json")
|
|
params.Set("resources", "volume")
|
|
params.Set("query", q.Title)
|
|
params.Set("limit", "5")
|
|
|
|
endpoint := strings.TrimRight(p.BaseURL, "/") + "/search/?" + params.Encode()
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// ComicVine rejects requests without a User-Agent.
|
|
req.Header.Set("User-Agent", "Vellum/0.1 (self-hosted media server)")
|
|
|
|
resp, err := p.Client.Do(req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("comicvine: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode != http.StatusOK {
|
|
return nil, fmt.Errorf("comicvine: status %d", resp.StatusCode)
|
|
}
|
|
|
|
var parsed cvResponse
|
|
if err := json.NewDecoder(resp.Body).Decode(&parsed); err != nil {
|
|
return nil, fmt.Errorf("comicvine: decode: %w", err)
|
|
}
|
|
// ComicVine returns 200 with status_code 100 for an invalid API key.
|
|
if parsed.StatusCode != 1 {
|
|
return nil, fmt.Errorf("comicvine: api status %d (check the API key)", parsed.StatusCode)
|
|
}
|
|
|
|
out := make([]Candidate, 0, len(parsed.Results))
|
|
for _, r := range parsed.Results {
|
|
if r.Name == "" {
|
|
continue
|
|
}
|
|
cover := r.Image.MediumURL
|
|
if cover == "" {
|
|
cover = r.Image.SmallURL
|
|
}
|
|
if cover == "" {
|
|
cover = r.Image.OriginalURL
|
|
}
|
|
out = append(out, Candidate{
|
|
Provider: p.Name(),
|
|
ExternalID: strconv.Itoa(r.ID),
|
|
Title: r.Name,
|
|
Year: parseYear(r.StartYear),
|
|
Publisher: r.Publisher.Name,
|
|
Description: stripHTML(r.Description),
|
|
CoverURL: cover,
|
|
})
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// stripHTML removes the tags ComicVine embeds in descriptions. It is
|
|
// deliberately minimal: descriptions are displayed as plain text, so only the
|
|
// tags need removing, not full entity decoding.
|
|
func stripHTML(s string) string {
|
|
var b strings.Builder
|
|
depth := 0
|
|
for _, r := range s {
|
|
switch r {
|
|
case '<':
|
|
depth++
|
|
case '>':
|
|
if depth > 0 {
|
|
depth--
|
|
}
|
|
default:
|
|
if depth == 0 {
|
|
b.WriteRune(r)
|
|
}
|
|
}
|
|
}
|
|
return strings.TrimSpace(b.String())
|
|
}
|