recognize

package
v1.0.0-rc.21 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2026 License: MIT Imports: 26 Imported by: 0

Documentation

Overview

Package recognize 是媒体识别服务:套用识别词、解析标题,再到 TMDB 找对应的条目 (有 IMDb 编号时直接查找,否则按中英文名和年份搜索打分),支持按 TMDB 编号手动纠正。 也管 TMDB 的设置(API Key 与代理加密保存、只写不读)与识别词。

Index

Constants

View Source
const (
	AcceptScore  = 0.95
	AcceptMargin = 0.1
)

AcceptScore 是认定为匹配的最低分;名字至少要部分相同(titleScore ≥ 0.75)。 名字只是部分相同时,第一名还要比第二名高出 AcceptMargin,否则只列候选。

View Source
const (
	SourceNone     = "none"
	SourceOverride = "override"
	SourceIMDb     = "imdb"
	SourceSearch   = "search"
)

识别结果的来源。

Variables

View Source
var (
	// ErrInvalid 表示参数不对(错误信息写明哪一项)。
	ErrInvalid = errors.New("参数无效")
	// ErrNotFound 表示要改或删的记录不存在。
	ErrNotFound = errors.New("记录不存在")
)
View Source
var Languages = []string{"zh-CN", "zh-TW", "zh-HK", "en-US", "ja-JP"}

Languages 是可选的 TMDB 语言(第一个是默认值)。

Functions

func Normalize

func Normalize(s string) string

Normalize 规整名字用于比较(订阅对种子标题时也用),规则见 normalize。

Types

type Candidate

type Candidate struct {
	tmdb.Result
	Score float64 `json:"score"`
}

Candidate 是一个打过分的候选条目。

type Cipher

type Cipher interface {
	Encrypt(plain string) (string, error)
	Decrypt(cipherText string) (string, error)
}

Cipher 加解密 API Key 与代理地址(生产环境是 ConfigStore 的 EncryptCookie / DecryptCookie)。

type Config

type Config struct {
	DB     *gorm.DB
	Cipher Cipher
	// BaseURL 是 TMDB 接口地址;为空时用官方地址(qa 构建可指向假服务)。
	BaseURL string
	// ImageBaseURL 是 TMDB 图片地址(到 /t/p/ 为止);为空时用官方地址(qa 构建可指向假服务)。
	ImageBaseURL string
	// RatePerSecond 是 TMDB 请求的限速(为 0 时用客户端的默认值)。
	RatePerSecond float64
}

Config 是服务的依赖。

type Input

type Input struct {
	Title    string `json:"title"`
	Subtitle string `json:"subtitle"`
	IMDbID   string `json:"imdb_id"`
}

Input 是识别的输入。

type OverrideInput

type OverrideInput struct {
	Title     string `json:"title"`
	Subtitle  string `json:"subtitle"`
	TMDBID    int    `json:"tmdb_id"`
	MediaType string `json:"media_type"`
}

OverrideInput 是手动纠正的参数:这个标题(按解析结果)固定识别成 TMDB 上的这个条目。

type Result

type Result struct {
	Meta       meta.Meta    `json:"meta"`
	Summary    string       `json:"summary"`
	RuleHits   []uint       `json:"rule_hits,omitempty"`
	Match      *tmdb.Result `json:"match,omitempty"`
	Source     string       `json:"source"`
	Score      float64      `json:"score,omitempty"`
	Candidates []Candidate  `json:"candidates,omitempty"`
	OverrideID uint         `json:"override_id,omitempty"`
	Message    string       `json:"message,omitempty"`
	Error      string       `json:"error,omitempty"`
}

Result 是识别结果。TMDB 出错时 Meta 照样有,Error 写明原因。

type Service

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

Service 是媒体识别服务。

func New

func New(cfg Config) *Service

New 建一个服务,顺便清掉过期的 TMDB 缓存。

func (*Service) DeleteOverride

func (s *Service) DeleteOverride(ctx context.Context, id uint) error

DeleteOverride 删除一条手动纠正。

func (*Service) DeleteWord

func (s *Service) DeleteWord(ctx context.Context, id uint) error

DeleteWord 删除一条识别词。

func (*Service) Overrides

func (s *Service) Overrides(ctx context.Context) ([]models.MediaOverride, error)

Overrides 列出手动纠正(新的在前)。

func (*Service) Parse

func (s *Service) Parse(ctx context.Context, title, subtitle string) (meta.Meta, error)

Parse 套用启用的识别词后解析一个标题(整理入库时解析每个文件名)。

func (*Service) Recognize

func (s *Service) Recognize(ctx context.Context, in Input) (*Result, error)

Recognize 识别一个标题:先看手动纠正,再按 IMDb 编号查找,最后按名字搜索打分。

func (*Service) SaveSettings

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

SaveSettings 保存设置。

func (*Service) SaveWord

SaveWord 新建(ID 为 0)或修改一条识别词。

func (*Service) SetOverride

func (s *Service) SetOverride(ctx context.Context, in OverrideInput) (*models.MediaOverride, error)

SetOverride 保存一条手动纠正:先到 TMDB 确认条目存在。同一个解析结果只保留最新的一条。

func (*Service) Settings

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

Settings 读设置。

func (*Service) TMDB

func (s *Service) TMDB(ctx context.Context) (*tmdb.Client, error)

TMDB 按当前设置建 TMDB 客户端(整理入库时取详情、季详情与图片);没有 API Key 时返回 tmdb.ErrNoKey。

func (*Service) TestTMDB

func (s *Service) TestTMDB(ctx context.Context) error

TestTMDB 用当前设置访问一次 TMDB,确认 API Key 与代理可用。

func (*Service) Words

func (s *Service) Words(ctx context.Context) ([]models.MediaWordRule, error)

Words 列出识别词(按套用顺序)。

type Settings

type Settings struct {
	HasTMDBKey bool   `json:"has_tmdb_key"`
	Language   string `json:"language"`
	ProxyURL   string `json:"proxy_url"`
}

Settings 是给界面看的设置:API Key 只说有没有;代理地址里的密码换成 ***。

type SettingsInput

type SettingsInput struct {
	TMDBKey  *string `json:"tmdb_api_key,omitempty"`
	Language string  `json:"language"`
	ProxyURL *string `json:"proxy_url,omitempty"`
}

SettingsInput 是保存设置的参数。TMDBKey、ProxyURL 为 nil 时不改,为空串时清除。 ProxyURL 与 Settings 返回的(密码换成 *** 的)地址相同时也不改。

Jump to

Keyboard shortcuts

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