tvdb

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2026 License: MIT Imports: 18 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 tvdb 从 TheTVDB(API v4)取剧集每一集的标题、简介、播出日期与截图,刮削时补 TMDB 没有的。

先用 API Key 登录(POST /login;user-supported 的 Key 还要带订阅 PIN,没有 PIN 时请求里不带 pin),拿到的令牌 有效一个月,存在内存里(同一个服务建的客户端共用),之后的请求带 Authorization: Bearer。令牌过期或失效(401)时 重新登录一次。每一集按播出顺序(official,即 Aired Order)与语言取,按页翻,每页缓存一天。 错误里不带 Key、PIN 与令牌。图片只从 TheTVDB 自己的图片服务器下载,不带令牌,最多 10 MiB。

Index

Constants

View Source
const (
	DefaultBaseURL = "https://api4.thetvdb.com/v4"

	// MaxPages 是一部剧最多取几页(第 0 到第 19 页)
	MaxPages = 20

	// DefaultRatePerSecond 是默认限速(每秒请求数,读缓存不算)
	DefaultRatePerSecond = 4
)

默认的接口地址、图片地址与几个上限。

Variables

View Source
var (
	ErrNoKey = errors.New("没有填写 TheTVDB API Key")
	// ErrUnauthorized 表示登录不了:Key 不对,或者是要订阅 PIN 的 Key 没填 PIN、PIN 不对
	ErrUnauthorized = errors.New("TheTVDB 登录不了:API Key 或订阅 PIN 不对")
	ErrRateLimited  = errors.New("TheTVDB 的请求太频繁,稍后再试")
	// ErrUnavailable 表示连不上 TheTVDB,或它回了别的错误
	ErrUnavailable = errors.New("TheTVDB 暂时不能访问")
	// ErrNotImage 表示图片地址回的不是图片
	ErrNotImage = errors.New("TheTVDB 图片地址返回的不是图片")
	// ErrTooMany 表示一部剧的集太多,MaxPages 页还没取完
	ErrTooMany = fmt.Errorf("TheTVDB 上这部的集太多(超过 %d 页)", MaxPages)
)

Functions

func NewLimiter

func NewLimiter(perSec float64) *rate.Limiter

NewLimiter 建一个限速器(perSec 为 0 时用 DefaultRatePerSecond)。

Types

type Cache

type Cache interface {
	Get(key string) ([]byte, bool)
	Set(key string, value []byte, ttl time.Duration)
}

Cache 是响应缓存(和 TMDB 共用 media_cache 表)。

type Client

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

Client 是 TheTVDB 客户端。

func New

func New(o Options) (*Client, error)

New 建一个客户端;没有 API Key 时返回 ErrNoKey。

func (*Client) Download

func (c *Client) Download(ctx context.Context, raw string) ([]byte, error)

Download 下载一张图片(地址先按 ImageURL 规整):只认 TheTVDB 自己的图片服务器,不带令牌,只接受 image/* 响应,最多 10 MiB。

func (*Client) Episodes

func (c *Client) Episodes(ctx context.Context, seriesID int, lang string) (*SeriesEpisodes, error)

Episodes 取一部剧按播出顺序(official)的每一集,lang 是三个字母的语言代码(例如 zho、eng)。从第 0 页翻起: 响应顶层有 links 时按 links.next 判断还有没有下一页(不跟它给的地址);没有 links 时这一页有没见过的集就接着翻。 后面的页 404 就停。带语言的列表第 0 页就 404 时(TheTVDB 有剧集明明在、这个接口却回 404 的问题,thetvdb/v4-api#324), 改取不带语言的列表(Untranslated 为真);它也 404 才是没有这部(返回空的结果)。超过 MaxPages 页时返回 ErrTooMany。

func (*Client) ImageURL

func (c *Client) ImageURL(raw string) string

ImageURL 把接口给的图片地址规整成完整的地址:/banners/… 这样的路径补上 https://artworks.thetvdb.com; 没有配置图片主机时 http:// 换成 https://。空串还是空串。

func (*Client) Validate

func (c *Client) Validate(ctx context.Context) error

Validate 检查 Key 与 PIN 能不能用:不用内存里的令牌,重新登录一次,再取一部固定的剧(不读缓存)。

type Episode

type Episode struct {
	ID     int
	Season int
	Number int
	Name   string
	// Overview 是简介
	Overview string
	// Aired 是播出日期(yyyy-mm-dd,没有时为空)
	Aired string
	// Image 是截图的完整地址(见 ImageURL;没有时为空)
	Image string
	// NameLangs、OverviewLangs 是 TheTVDB 上这一集有标题、简介的语言(三个字母的代码,例如 zho、eng)
	NameLangs     []string
	OverviewLangs []string
}

Episode 是一集。Name 与 Overview 是请求的语言的(没有这种语言时接口可能给原语言的,见 NameLangs)。

type Options

type Options struct {
	APIKey string
	// PIN 是订阅 PIN(user-supported 的 Key 才要),可以为空
	PIN     string
	BaseURL string
	// ImageHosts 是允许下载图片的主机;为空时只认 https 的 artworks.thetvdb.com 与 *.thetvdb.com
	ImageHosts []string
	HTTP       *http.Client
	Cache      Cache
	// Tokens 为空时这个客户端自己建一个
	Tokens *Tokens
	// Limiter 为空时这个客户端自己建一个(DefaultRatePerSecond)
	Limiter *rate.Limiter
	// Now 为空时用 time.Now
	Now func() time.Time
}

Options 是客户端的选项。

type SeriesEpisodes

type SeriesEpisodes struct {
	SeriesID int
	// OriginalLanguage 是剧集的原语言(三个字母的代码)
	OriginalLanguage string
	Episodes         []Episode
	// Untranslated 表示带语言的列表回了 404、这是不带语言的列表:Name 与 Overview 是原语言的(截图照样能用)
	Untranslated bool
}

SeriesEpisodes 是一部剧按播出顺序的每一集。没有这部时 Episodes 为空。

type Tokens

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

Tokens 是登录令牌的内存表:同一个服务建的客户端共用,不用每次刮削都登录。按接口地址、Key 与 PIN 分开存。

func NewTokens

func NewTokens() *Tokens

NewTokens 建一个空的令牌表。

Jump to

Keyboard shortcuts

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