Documentation
¶
Overview ¶
Package tmdb 是 TMDB v3 接口的客户端:搜索电影与剧集、按 IMDb 编号查找、取详情。 API Key 用用户自己填的(v3 的 API Key 或 v4 的读取令牌都可以),没填时用构建时内置的(BuiltinKey); 默认中文,中文简介为空时取英文的。 走设置里的代理,没有时用环境变量里的代理;响应缓存在 SQLite 里;自己限速。错误里不带请求地址(API Key 在地址里)。
Index ¶
- Constants
- Variables
- func Builtin() string
- func HasList(kind, list string) bool
- func IsOfficialAPIBase(base string) bool
- func NewHTTPClient(proxyURL string) (*http.Client, error)
- func NewLimiter(perSec float64) *rate.Limiter
- func NormalizeAPIBaseURL(raw string) (string, error)
- func NormalizeImageBaseURL(raw string) (string, error)
- func ParseProxyURL(s string) (*url.URL, error)
- func PosterURL(path, size string) string
- func ValidEpisodeGroupID(id string) bool
- func ValidKey(key string) bool
- type Cache
- type CastMember
- type Client
- func (c *Client) Details(ctx context.Context, kind string, id int) (*Result, error)
- func (c *Client) Discover(ctx context.Context, kind string, f DiscoverFilter, page int) (*ListPage, error)
- func (c *Client) EpisodeGroup(ctx context.Context, id string) (*EpisodeGroup, error)
- func (c *Client) EpisodeGroups(ctx context.Context, tvID int) ([]EpisodeGroupSummary, error)
- func (c *Client) Extras(ctx context.Context, kind string, id int) (*Extras, error)
- func (c *Client) Find(ctx context.Context, imdb string) ([]Result, error)
- func (c *Client) FindByTVDB(ctx context.Context, tvdbID int) (*Result, error)
- func (c *Client) Image(ctx context.Context, path, size string) ([]byte, error)
- func (c *Client) Images(ctx context.Context, kind string, id int) (*Images, error)
- func (c *Client) Language() string
- func (c *Client) List(ctx context.Context, kind, list string, page int) (*ListPage, error)
- func (c *Client) PersonChineseName(ctx context.Context, id int) (string, error)
- func (c *Client) PosterURL(path, size string) string
- func (c *Client) ProfileURL(path string) string
- func (c *Client) Recommendations(ctx context.Context, kind string, id int) ([]Result, error)
- func (c *Client) Search(ctx context.Context, kind, query string, year int) ([]Result, error)
- func (c *Client) Season(ctx context.Context, tvID, number int) (*Season, error)
- func (c *Client) Seasons(ctx context.Context, tvID int) ([]SeasonSummary, error)
- func (c *Client) ServerTime(ctx context.Context) (time.Time, error)
- func (c *Client) Titles(ctx context.Context, kind string, id int) ([]string, error)
- func (c *Client) Validate(ctx context.Context) error
- type Collection
- type DBCache
- type DiscoverFilter
- type Episode
- type EpisodeGroup
- type EpisodeGroupEpisode
- type EpisodeGroupPart
- type EpisodeGroupSummary
- type Extras
- type Image
- type ImageClient
- type Images
- type ListPage
- type Options
- type Result
- type Season
- type SeasonSummary
Constants ¶
const ( EpisodeGroupAirDate = 1 EpisodeGroupAbsolute = 2 EpisodeGroupDVD = 3 EpisodeGroupDigital = 4 EpisodeGroupStoryArc = 5 EpisodeGroupProduction = 6 EpisodeGroupTV = 7 )
剧集组的类型(type)。
const ( // ListTrending 是本周趋势 ListTrending = "trending" // ListPopular 是热门 ListPopular = "popular" // ListTopRated 是高分 ListTopRated = "top_rated" // ListNowPlaying、ListUpcoming 是电影的正在上映、即将上映 ListNowPlaying = "now_playing" ListUpcoming = "upcoming" // ListOnTheAir、ListAiringToday 是剧集的正在播出(最近一周有新的一集)、今天播出 ListOnTheAir = "on_the_air" ListAiringToday = "airing_today" // ListTTL 是列表的缓存时间 ListTTL = 6 * time.Hour )
探索页用的列表。
const ( SizePoster = "w780" SizeBackdrop = "w1280" SizeStill = "w300" SizeLogo = "w500" )
图片的尺寸(TMDB 图片地址里的一段)。
const ( DefaultBaseURL = "https://api.themoviedb.org/3" ImageBaseURL = "https://image.tmdb.org/t/p/" )
DefaultBaseURL 是 TMDB v3 接口地址;ImageBaseURL 是图片地址。
const ( KindMovie = "movie" KindTV = "tv" )
Kind 是条目类型。
const ( SearchTTL = 24 * time.Hour DetailsTTL = 7 * 24 * time.Hour )
缓存时长:搜索结果一天,查找与详情一周。
const DefaultMaxCacheRows = 5000
DefaultMaxCacheRows 是缓存条数的默认上限。
const DefaultRatePerSecond = 8
DefaultRatePerSecond 是默认限速(每秒请求数)。
const EpisodeGroupTTL = 24 * time.Hour
EpisodeGroupTTL 是剧集组的缓存时间。
const FindTVDBTTL = 30 * 24 * time.Hour
FindTVDBTTL 是按 TVDB 编号找到的剧集的缓存时间:一部剧的 TVDB 编号与 TMDB 编号对应好以后不会变。
const GenreAnimation = 16
GenreAnimation 是 TMDB 的「动画」类型编号(电影与剧集相同)。
const PersonTTL = 30 * 24 * time.Hour
PersonTTL 是演员译名的缓存时间。
const SeasonsTTL = 24 * time.Hour
SeasonsTTL 是剧集各季列表的缓存时间:新的一季宣布了要能看到,比详情的一周短。
Variables ¶
var ( ErrNoKey = errors.New("没有填写 TMDB API Key") ErrNotFound = errors.New("TMDB 上没有这个条目") ErrRateLimited = errors.New("TMDB 请求太频繁,稍后再试") ErrUnavailable = errors.New("TMDB 暂时不能访问") )
var BuiltinKey = ""
BuiltinKey 是构建时用 -ldflags 注入的 TMDB API Key(发布流程从仓库 secret PT_TOOLS_TMDB_KEY 来,源码里没有): 用户没填自己的 Key 时用它,像 Jellyfin、tinyMediaManager 那样装好就能识别。为空(从源码编译)时要自己填。
var ( // ErrNotImage 表示图片地址回的不是图片。 ErrNotImage = errors.New("TMDB 图片地址返回的不是图片") )
Functions ¶
func IsOfficialAPIBase ¶
IsOfficialAPIBase 报告接口地址是不是 TMDB 官方的(https 加官方域名,不带端口)。内置 Key 是所有用户共用的, 不发给镜像与自建的中转,免得被人收集了去滥用、连累大家。
func NewHTTPClient ¶
NewHTTPClient 建一个访问 TMDB 的 http.Client:走 proxyURL,为空时用环境变量里的代理。
func NewLimiter ¶
NewLimiter 建一个限速器(perSec 为 0 时用 DefaultRatePerSecond)。
func NormalizeAPIBaseURL ¶
NormalizeAPIBaseURL 规整用户填的 TMDB 接口地址:只写域名时补上 https:// 与 /3,去掉末尾的 /。 空串表示用官方地址(DefaultBaseURL)。可以是镜像或自建的中转,带不带子路径都行。
func NormalizeImageBaseURL ¶
NormalizeImageBaseURL 规整用户填的 TMDB 图片地址(到尺寸之前为止,例如 https://image.tmdb.org/t/p/): 只写域名时补上 https:// 与 /t/p/,末尾总是带 /。空串表示用官方地址(ImageBaseURL)。
func ParseProxyURL ¶
ParseProxyURL 校验代理地址:http、https、socks5 或 socks5h,不能带路径以外的多余部分。
func ValidEpisodeGroupID ¶
ValidEpisodeGroupID 报告 id 是不是剧集组编号的写法。
Types ¶
type Cache ¶
type Cache interface {
Get(key string) ([]byte, bool)
Set(key string, value []byte, ttl time.Duration)
}
Cache 是响应缓存。
type CastMember ¶
type CastMember struct {
ID int `json:"id,omitempty"`
Name string `json:"name"`
Character string `json:"character,omitempty"`
Order int `json:"order"`
ProfilePath string `json:"profile_path,omitempty"`
}
CastMember 是一位演员。ID 是 TMDB 上这个人的编号(取中文译名用),ProfilePath 是头像图片的路径(/xxx.jpg),没有时为空。
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client 是 TMDB 客户端。
func (*Client) Discover ¶
func (c *Client) Discover(ctx context.Context, kind string, f DiscoverFilter, page int) (*ListPage, error)
Discover 按出品国家与类型发现电影或剧集(按热度排,不含成人内容)。page 从 1 开始,最多到第 20 页。
func (*Client) EpisodeGroup ¶
EpisodeGroup 取一个剧集组的详情(缓存 24 小时):组按 order 排、组里的集按 order 排(order 一样时按原来的先后)。 编号不是剧集组编号的写法时是 ErrNotFound(不发请求)。
func (*Client) EpisodeGroups ¶
EpisodeGroups 列出一部剧的剧集组(缓存 24 小时);没有时是空的。
func (*Client) FindByTVDB ¶
FindByTVDB 按 TVDB 编号找 TMDB 上的剧集(GET /find/{tvdb}?external_source=tvdb_id,缓存 30 天)。Servarr 兼容入口用: Seerr 请求剧集时只给 TVDB 编号。找不到或编号不对时是 ErrNotFound。
func (*Client) Image ¶
Image 下载一张 TMDB 图片(path 是条目里的 poster_path 一类,size 如 w780)。 只接受 image/* 响应,最多 10 MiB;图片走图片服务器,不占接口的限速额度。
func (*Client) List ¶
List 取电影或剧集的一个列表(见 HasList)。page 从 1 开始,最多到第 20 页。今天播出按服务器的时区算 (TZ 写了时区名时;没写时是 TMDB 默认的时区)。
func (*Client) PersonChineseName ¶
PersonChineseName 是演员在 TMDB 上的中文译名(/person/{id}/translations,缓存 30 天):按 zh-CN、zh-SG、zh-TW、zh-HK 的顺序取, 都没有时回空串。TMDB 上没有这个人时也回空串。
func (*Client) ProfileURL ¶
ProfileURL 是演员头像的地址(w185):用这个客户端的图片地址。路径格式不对时为空。
func (*Client) Recommendations ¶
Recommendations 取 TMDB 按这个条目推荐的电影或剧集(第 1 页,同类),缓存 ListTTL。
func (*Client) ServerTime ¶
ServerTime 同 Validate,另返回 TMDB 那边现在的时间(响应头的 Date 加上 Age,CDN 缓存的响应 Date 是生成时的): 系统体检拿它比本机的时钟。响应里没有 Date 时返回零值。
type Collection ¶
Collection 是电影所属的系列。
type DBCache ¶
type DBCache struct {
// contains filtered or unexported fields
}
DBCache 把缓存放在 SQLite 的 media_cache 表里。读写失败时当作没有缓存,不影响请求。 条数有上限(MaxRows):超出时按到期时间删掉最早的。
type DiscoverFilter ¶
DiscoverFilter 是按条件发现的筛选:Country 是出品国家或地区(ISO 3166-1 的两个大写字母,例如 CN),Genres 是 TMDB 的类型编号(几个用 | 隔开时是其中任意一个,例如综艺的 10764|10767)。空的不限。
type Episode ¶
type Episode struct {
ID int `json:"id,omitempty"`
Number int `json:"episode_number"`
Name string `json:"name,omitempty"`
Overview string `json:"overview,omitempty"`
AirDate string `json:"air_date,omitempty"`
StillPath string `json:"still_path,omitempty"`
Runtime int `json:"runtime,omitempty"`
VoteAverage float64 `json:"vote_average,omitempty"`
VoteCount int `json:"vote_count,omitempty"`
Directors []string `json:"directors,omitempty"`
Writers []string `json:"writers,omitempty"`
}
Episode 是剧集一季里的一集。Directors 与 Writers 是这一集的导演与编剧。
type EpisodeGroup ¶
type EpisodeGroup struct {
EpisodeGroupSummary
Groups []EpisodeGroupPart `json:"groups"`
}
EpisodeGroup 是剧集组的详情:Groups 按 Order 排好。
type EpisodeGroupEpisode ¶
type EpisodeGroupEpisode struct {
Season int `json:"season_number"`
Episode int `json:"episode_number"`
Order int `json:"order"`
Name string `json:"name,omitempty"`
AirDate string `json:"air_date,omitempty"`
}
EpisodeGroupEpisode 是剧集组里的一集:Season、Episode 是它在默认排法里的季与集。
type EpisodeGroupPart ¶
type EpisodeGroupPart struct {
ID string `json:"id"`
Name string `json:"name"`
Order int `json:"order"`
Episodes []EpisodeGroupEpisode `json:"episodes"`
}
EpisodeGroupPart 是剧集组里的一组(相当于一季):Episodes 按 Order 排好。
type EpisodeGroupSummary ¶
type EpisodeGroupSummary struct {
ID string `json:"id"`
Name string `json:"name"`
Type int `json:"type"`
Description string `json:"description,omitempty"`
GroupCount int `json:"group_count"`
EpisodeCount int `json:"episode_count"`
Network string `json:"network,omitempty"`
}
EpisodeGroupSummary 是一部剧的一个剧集组(列表里的一项)。Network 是这种排法所属的电视台或平台(没有时为空)。
type Extras ¶
type Extras struct {
Tagline string `json:"tagline,omitempty"`
// Status 是 TMDB 的状态原文(剧集:Returning Series、Ended、Canceled……)
Status string `json:"status,omitempty"`
VoteCount int `json:"vote_count,omitempty"`
IMDbID string `json:"imdb_id,omitempty"`
TVDBID int `json:"tvdb_id,omitempty"`
// Studios 是电影的出品公司、剧集的电视网
Studios []string `json:"studios,omitempty"`
// Certifications 是各国家或地区的分级(ISO 3166-1 → 分级,例如 US → PG-13)
Certifications map[string]string `json:"certifications,omitempty"`
Collection *Collection `json:"collection,omitempty"`
Directors []string `json:"directors,omitempty"`
Writers []string `json:"writers,omitempty"`
Cast []CastMember `json:"cast,omitempty"`
}
Extras 是刮削写 NFO 用的、详情里没有的那些:演职员、分级、出品公司(剧集是电视网)、所属系列、剧集的状态、 宣传语、评分人数与 TVDB 编号。用一次带 append_to_response 的详情请求取到,缓存一周。
func (*Extras) TVDBIDString ¶
TVDBIDString 是 TVDB 编号的字符串(没有时为空)。
type Image ¶
type Image struct {
Path string `json:"path"`
Lang string `json:"lang,omitempty"`
Vote float64 `json:"vote,omitempty"`
Votes int `json:"votes,omitempty"`
}
Image 是条目的一张图片。Lang 是图上文字的语言(ISO 639-1),空串是没有文字。
type ImageClient ¶
type ImageClient struct {
// contains filtered or unexported fields
}
ImageClient 只取图片:图片服务器不用 API Key,没填 Key 时 App 的海报也能经 pt-tools 取。
func NewImageClient ¶
func NewImageClient(imageBaseURL string, hc *http.Client) *ImageClient
NewImageClient 建图片客户端:imageBaseURL 为空时用官方地址,hc 是发请求用的客户端(带代理)。
type Images ¶
type Images struct {
Posters []Image `json:"posters,omitempty"`
Backdrops []Image `json:"backdrops,omitempty"`
Logos []Image `json:"logos,omitempty"`
}
Images 是条目的海报、背景与 Logo,每类都按评分从高到低排好。 只取中文、英文、日文与没有文字的,每种语言最多 maxImagesPerLang 张;SVG 一类不能直接用的格式不要。
type ListPage ¶
type ListPage struct {
Results []Result `json:"results"`
Page int `json:"page"`
TotalPages int `json:"total_pages"`
}
ListPage 是列表的一页。
type Options ¶
type Options struct {
APIKey string
BaseURL string // 为空时用 DefaultBaseURL
// ImageBaseURL 是图片地址(到 /t/p/ 为止);为空时用 ImageBaseURL
ImageBaseURL string
Language string // 为空时用 zh-CN
ProxyURL string // 为空时用环境变量里的代理
Cache Cache // 为空时不缓存
// RatePerSecond 是每秒最多几个请求(为 0 时 8 个);读缓存不算。Limiter 不为空时不用它。
RatePerSecond float64
// Limiter 不为空时用它限速:同一个服务建的多个客户端共用一份额度。
Limiter *rate.Limiter
// HTTPClient 不为空时用它发请求(ProxyURL 不再生效):同一个代理的客户端共用连接池。
HTTPClient *http.Client
}
Options 是客户端的选项。
type Result ¶
type Result struct {
ID int `json:"id"`
MediaType string `json:"media_type"`
Title string `json:"title"`
OriginalTitle string `json:"original_title"`
Year int `json:"year,omitempty"`
Date string `json:"date,omitempty"`
Overview string `json:"overview,omitempty"`
PosterPath string `json:"poster_path,omitempty"`
Popularity float64 `json:"popularity,omitempty"`
VoteAverage float64 `json:"vote_average,omitempty"`
OriginalLanguage string `json:"original_language,omitempty"`
IMDbID string `json:"imdb_id,omitempty"`
Seasons int `json:"seasons,omitempty"`
BackdropPath string `json:"backdrop_path,omitempty"`
// GenreIDs 是 TMDB 的类型编号(16 是动画);Genres 是类型名称,只有详情里有
GenreIDs []int `json:"genre_ids,omitempty"`
Genres []string `json:"genres,omitempty"`
// Runtime 是片长(分钟):电影是整部,剧集是一集,只有详情里有
Runtime int `json:"runtime,omitempty"`
// Countries 是出品国家或地区(ISO 3166-1,例如 CN、US):origin_country 与电影的 production_countries 合在一起;
// 详情里有,剧集的搜索结果里也有
Countries []string `json:"countries,omitempty"`
}
Result 是一个电影或剧集条目(搜索、查找与详情共用)。
type Season ¶
type Season struct {
Number int `json:"season_number"`
Name string `json:"name,omitempty"`
Overview string `json:"overview,omitempty"`
AirDate string `json:"air_date,omitempty"`
PosterPath string `json:"poster_path,omitempty"`
Episodes []Episode `json:"episodes,omitempty"`
}
Season 是剧集的一季(含各集)。
type SeasonSummary ¶
type SeasonSummary struct {
Number int `json:"season_number"`
Name string `json:"name,omitempty"`
EpisodeCount int `json:"episode_count"`
AirDate string `json:"air_date,omitempty"`
PosterPath string `json:"poster_path,omitempty"`
}
SeasonSummary 是剧集详情里列的一季:季号(0 是特别篇)、名字、集数、首播日期与季海报。