ratings

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 1, 2026 License: GPL-3.0 Imports: 18 Imported by: 0

Documentation

Overview

Package ratings caches licensed, owner-keyed rating data. Values preserve their source scale; Plex's main rating fields alone use the 0–10 scale.

Index

Constants

View Source
const Redacted = "[configured]"

Variables

This section is empty.

Functions

This section is empty.

Types

type Badge

type Badge struct {
	Source string  `json:"source"`
	Value  float64 `json:"value"`
	Scale  float64 `json:"scale"`
	Votes  int64   `json:"votes,omitempty"`
}

func (Badge) PlexImage

func (b Badge) PlexImage() string

PlexImage returns only icons understood by official Plex clients. Other providers remain explicitly named poster badges; they never impersonate IMDb.

func (Badge) PlexValue

func (b Badge) PlexValue() float64

type Options

type Options struct {
	Client         *http.Client
	OMDbBaseURL    string
	MDBListBaseURL string
	Now            func() time.Time
	// Sealer protects the stored API key, bound to its provider; nil
	// stores it as given.
	Sealer config.FieldSealer
	// Certification receives the US certification (IMDb's, e.g. "TV-G")
	// a provider reports with a fresh fetch, so the catalogue can rate
	// titles TMDB leaves unrated. Nil ignores it.
	Certification func(ctx context.Context, key core.RatingKey, label, provider string) error
	// Unrated lists catalogue titles without a rating, by key after the
	// given one; Run looks a few of them up each cycle (Backfill). Nil
	// disables the backfill.
	Unrated func(ctx context.Context, after core.RatingKey, limit int) ([]core.Title, error)
}

type Presented added in v0.3.0

type Presented struct {
	Community *float64
	Critic    *float64
	Sources   []PresentedSource
}

Presented is the wire-neutral form of one title's cached provider ratings. Community is 0–10 and Critic is 0–100, matching the Jellyfin and Emby fields Android TV divides by 10 and 100. A nil pointer means that configured source had no cached value. Sources lists every cached badge, including Letterboxd, under its own name.

func Present added in v0.3.0

func Present(cfg Settings, badges []Badge) Presented

Present maps cached badges through the owner's critic and audience choice. Disabled settings and a missing badge produce no number. A zero score that the provider actually returned is kept.

type PresentedSource added in v0.3.0

type PresentedSource struct {
	Source, Name, Display string
	Score                 float64
	Votes                 int64
}

PresentedSource is one named score. Score is 0–100. Display is the source scale the viewer reads.

type Service

type Service struct {
	// contains filtered or unexported fields
}

func New

func New(db *sql.DB, o Options) *Service

func (*Service) Backfill added in v0.3.0

func (s *Service) Backfill(ctx context.Context)

Backfill queues the next few catalogue titles without a rating that the provider has not answered within the cache interval, so their certification (Options.Certification) reaches titles nobody opens, such as ones a library rule hides. It walks the catalogue by key and wraps.

func (*Service) Cached

func (s *Service) Cached(ctx context.Context, t *core.Title) ([]Badge, error)

Cached never performs network I/O; safe for metadata and hub rendering.

func (*Service) CachedMany added in v0.3.0

func (s *Service) CachedMany(ctx context.Context, titles []core.Title) (map[core.RatingKey][]Badge, error)

CachedMany is Cached for a page. Missing and expired titles are omitted. The provider being disabled returns an empty map and a nil error.

func (*Service) MigrateSecrets added in v0.2.4

func (s *Service) MigrateSecrets(ctx context.Context) (int, error)

MigrateSecrets seals an API key stored in plaintext; it returns 1 when it rewrote the settings. The row is replaced only if unchanged since read, and a key that does not open is left untouched.

func (*Service) Prune

func (s *Service) Prune(ctx context.Context) (int64, error)

Prune deletes rating rows older than the maximum cache interval. It is meant to be called periodically.

func (*Service) Queue

func (s *Service) Queue(t core.Title)

Queue is bounded, deduplicated and nonblocking; Run owns the network worker.

func (*Service) Refresh

func (s *Service) Refresh(ctx context.Context, t *core.Title) ([]Badge, error)

Refresh returns fresh-or-cached badges for t.

Semântica de concorrência: nenhum lock é segurado durante o HTTP — flightMu só protege o map flights (leitura/escrita curta), e cache/DB são acessados sem lock global. Chamadas concorrentes para a MESMA key compartilham um único fetch em voo (singleflight): quem espera recebe o resultado (badges/err) do líder, sem disparar outro request. Se o contexto do líder encerrou o fetch (abandoned) e o contexto de quem espera ainda está vivo, quem espera NÃO herda esse erro de cancelamento: ele refaz — vira o próximo líder ou entra no fetch seguinte. Se o contexto de quem espera é cancelado enquanto aguarda, ele retorna ctx.Err() sem cancelar o fetch do líder. Keys diferentes fazem fetch em paralelo.

func (*Service) Router

func (s *Service) Router(cat core.Catalog) http.Handler

Router is owner-only; a catalog is supplied for explicit cache warming.

func (*Service) Run

func (s *Service) Run(ctx context.Context)

func (*Service) SaveSettings

func (s *Service) SaveSettings(ctx context.Context, v Settings) error

func (*Service) Settings

func (s *Service) Settings(ctx context.Context) (Settings, error)

func (*Service) WithSettingsSnapshot added in v0.2.6

func (s *Service) WithSettingsSnapshot(ctx context.Context) context.Context

WithSettingsSnapshot reads settings once for a bounded render. It avoids a database round trip per tile without keeping settings stale across requests.

type Settings

type Settings struct {
	Enabled    bool   `json:"enabled"`
	Provider   string `json:"provider"`
	APIKey     string `json:"apiKey"`
	Critic     string `json:"critic"`
	Audience   string `json:"audience"`
	CacheHours int    `json:"cacheHours"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL