recognize

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: MIT Imports: 29 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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 (
	KeyUser    = "user"    // 自己填的
	KeyBuiltin = "builtin" // 没填,用构建时内置的
	KeyNone    = "none"    // 没有能用的:没填,这个版本也没有内置的,或者换了接口地址(内置的只发给官方地址)
)

识别现在用的 TMDB API Key(Settings.KeySource)。

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
	// FanartBaseURL、OMDbBaseURL 是 Fanart.tv 与 OMDb 的接口地址;为空时用官方地址(测试与 qa 构建可指向假服务)。
	FanartBaseURL, OMDbBaseURL string
	// FanartImageHosts 是允许下载 Fanart.tv 图片的主机;为空时只认 fanart.tv 自己的(测试里换成假服务的)。
	FanartImageHosts []string
	// TVDBBaseURL 是 TheTVDB 的接口地址(到 /v4 为止);为空时用官方地址(测试与 qa 构建可指向假服务)。
	TVDBBaseURL string
	// TVDBImageHosts 是允许下载 TheTVDB 图片的主机;为空时只认 thetvdb.com 自己的(测试与 qa 构建换成假服务的)。
	TVDBImageHosts []string
}

Config 是服务的依赖。

type Correction

type Correction struct {
	MediaType  string
	TMDBID     int
	OverrideID uint
}

Correction 是一个标题的手动纠正:类型、TMDB 编号与纠正的编号。

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) Candidates

func (s *Service) Candidates(ctx context.Context, in Input, limit int) ([]Candidate, error)

Candidates 是一个标题在 TMDB 上的候选:套用识别词解析以后电影与剧集都搜,按分数排,最多 limit 个(1 到 20)。 不看手动纠正与 IMDb 编号:给自动识别认不出的标题让人或 AI 挑。解析出的名字为空时没有候选;没填 TMDB API Key 时返回 tmdb.ErrNoKey。

func (*Service) Correction

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

Correction 找这个标题的手动纠正(套用识别词解析以后按名字找,同 Recognize 的第一步,见 findOverride);没有时返回 nil、nil。 不访问 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) Fanart

func (s *Service) Fanart(ctx context.Context) (*fanart.Client, error)

Fanart 按当前设置建 Fanart.tv 客户端(刮削时取图片,走媒体识别的代理,和 TMDB 共用缓存):项目的 Key,填了个人 Key 时一起发; 没填项目的 Key 时返回 fanart.ErrNoKey。

func (*Service) Images

func (s *Service) Images(ctx context.Context) (*tmdb.ImageClient, error)

Images 按当前设置(图片地址与代理)建只取图片的客户端:图片服务器不用 API Key。

func (*Service) OMDb

func (s *Service) OMDb(ctx context.Context) (*omdb.Client, error)

OMDb 按当前设置建 OMDb 客户端(刮削时取 IMDb 等评分,走媒体识别的代理,和 TMDB 共用缓存);没填 API Key 时返回 omdb.ErrNoKey。

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) TVDB

func (s *Service) TVDB(ctx context.Context) (*tvdb.Client, error)

TVDB 按当前设置建 TheTVDB 客户端(刮削时取每一集的标题、简介与截图,走媒体识别的代理,和 TMDB 共用缓存,客户端之间共用 登录令牌与限速):填的 Key,填了订阅 PIN 时一起登录;没填 Key 时返回 tvdb.ErrNoKey。

func (*Service) TestFanart

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

TestFanart 用当前设置访问一次 Fanart.tv,确认 API Key 与代理可用。

func (*Service) TestOMDb

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

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

func (*Service) TestTMDB

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

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

func (*Service) TestTVDB

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

TestTVDB 用当前设置登录一次 TheTVDB 并取一部剧,确认 API Key、订阅 PIN 与代理可用。

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"`
	// KeySource 是识别现在用哪个 Key(KeyUser、KeyBuiltin、KeyNone)
	KeySource string `json:"key_source"`
	// BuiltinKey 表示这个版本内置了 Key(发布的 Release 与 Docker 镜像有,从源码编译的没有)
	BuiltinKey   bool   `json:"builtin_key"`
	Language     string `json:"language"`
	ProxyURL     string `json:"proxy_url"`
	APIBaseURL   string `json:"api_base_url"`
	ImageBaseURL string `json:"image_base_url"`
	// HasFanartKey、HasOMDbKey 说刮削用的 Fanart.tv(项目的 Key)与 OMDb 的 API Key 填了没有,HasFanartClientKey 说
	// Fanart.tv 的个人 Key 填了没有
	HasFanartKey       bool `json:"has_fanart_key"`
	HasFanartClientKey bool `json:"has_fanart_client_key"`
	HasOMDbKey         bool `json:"has_omdb_key"`
	// HasTVDBKey、HasTVDBPIN 说 TheTVDB 的 API Key 与订阅 PIN 填了没有
	HasTVDBKey bool `json:"has_tvdb_key"`
	HasTVDBPIN bool `json:"has_tvdb_pin"`
}

Settings 是给界面看的设置:API Key 只说有没有(HasTMDBKey 是自己填的那个);代理地址里的密码换成 ***。 APIBaseURL、ImageBaseURL 是换掉的地址,空 = 官方地址。

type SettingsInput

type SettingsInput struct {
	TMDBKey      *string `json:"tmdb_api_key,omitempty"`
	Language     string  `json:"language"`
	ProxyURL     *string `json:"proxy_url,omitempty"`
	APIBaseURL   *string `json:"api_base_url,omitempty"`
	ImageBaseURL *string `json:"image_base_url,omitempty"`
	// FanartKey、OMDbKey 是刮削用的 Fanart.tv(项目的 Key)与 OMDb 的 API Key,FanartClientKey 是 Fanart.tv 的个人 Key
	// (可选;加上以后新上传的图片更快能用,见 Fanart、OMDb)
	FanartKey       *string `json:"fanart_api_key,omitempty"`
	FanartClientKey *string `json:"fanart_client_key,omitempty"`
	OMDbKey         *string `json:"omdb_api_key,omitempty"`
	// TVDBKey、TVDBPIN 是 TheTVDB 的 API Key 与订阅 PIN(user-supported 的 Key 才要 PIN),为 nil 时不改,为空串时清除
	TVDBKey *string `json:"tvdb_api_key,omitempty"`
	TVDBPIN *string `json:"tvdb_pin,omitempty"`
}

SettingsInput 是保存设置的参数。TMDBKey、ProxyURL、APIBaseURL、ImageBaseURL、FanartKey、OMDbKey、TVDBKey、TVDBPIN 为 nil 时不改, 为空串时清除(两个地址清除以后用官方地址)。ProxyURL 与 Settings 返回的(密码换成 *** 的)地址相同时也不改。

Jump to

Keyboard shortcuts

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