Documentation
¶
Overview ¶
Package server 是媒体服务器的客户端:Emby、Jellyfin 与 Plex。整理入库后通知媒体服务器扫描新目录 (按路径或刷新整个媒体库),也能测试连通性、按 TMDB 或 IMDb 编号查一个条目是不是已经入库, 分页列出服务器里的电影、剧与集(索引用),取用户与没看完的(继续观看,Emby 与 Jellyfin), 拼条目在服务器网页里的打开链接,解析三家发来的 Webhook。
Index ¶
Constants ¶
const ( WebhookAdded = "added" WebhookRemoved = "removed" WebhookPlaybackStart = "playback_start" WebhookPlaybackStop = "playback_stop" WebhookOther = "other" )
Webhook 事件归成的几类。
const WebhookMaxBody = 1 << 20
WebhookMaxBody 是一次 Webhook 最多读多少字节(Plex 的 multipart 里 payload 在缩略图前面,读到 payload 就不往下读)。
Variables ¶
var ( ErrUnauthorized = errors.New("媒体服务器拒绝了请求:API Key 或 Token 不对,或者没有管理员权限") ErrUnavailable = errors.New("连不上媒体服务器") // ErrRedirect 表示媒体服务器回了跳转:不跟着跳(会把 Token 发到别的地址)。 ErrRedirect = errors.New("媒体服务器回了跳转(3xx):地址要填完整的最终地址,例如 https:// 与端口") // ErrNoLibrary 表示 Plex 里没有包含这个路径的媒体库。 ErrNoLibrary = errors.New("Plex 里没有包含这个路径的媒体库:检查媒体服务器的路径映射") // ErrBadConfig 表示配置不对(错误信息写明哪一项)。 ErrBadConfig = errors.New("媒体服务器配置无效") // ErrUnsupported 表示这种媒体服务器没有这项功能(Plex 没有继续观看的用户接口)。 ErrUnsupported = errors.New("这种媒体服务器不支持") )
var ErrBadWebhook = errors.New("看不懂的 Webhook 请求")
ErrBadWebhook 表示请求体不是认得的 Webhook(不是 JSON,或 multipart 里没有 data、payload)。
Functions ¶
func NewHTTPClient ¶
NewHTTPClient 是访问媒体服务器用的 http.Client:15 秒超时,不跟跳转。媒体服务器一般在局域网里,不走代理。
Types ¶
type Client ¶
type Client interface {
// Test 用 API Key 或 Token 访问一次,返回服务器名称与版本。
Test(ctx context.Context) (Info, error)
// RefreshPaths 通知媒体服务器这些目录(媒体服务器里看到的路径)有新内容。
RefreshPaths(ctx context.Context, paths []string) error
// RefreshAll 刷新整个媒体库。
RefreshAll(ctx context.Context) error
// Exists 报告条目是不是已经入库。
Exists(ctx context.Context, q Query) (bool, error)
// ListItems 列出服务器里的电影、剧与集,一次一页:cursor 为空时从头开始,返回的 Next 为空时列完了。
ListItems(ctx context.Context, cursor string) (ItemPage, error)
// Users 列出服务器的用户(选继续观看用哪个用户);Plex 回 ErrUnsupported。
Users(ctx context.Context) ([]User, error)
// Resume 是一个用户没看完的电影与集,最近看的在前,最多 limit 项;Plex 回 ErrUnsupported。
Resume(ctx context.Context, userID string, limit int) ([]ResumeItem, error)
}
Client 是一个媒体服务器。
type Config ¶
type Config struct {
Kind string
URL string
Token string
// HTTPClient 为空时用默认的(15 秒超时、不跟跳转)
HTTPClient *http.Client
}
Config 是建客户端用的配置。
type Info ¶
type Info struct {
Name string `json:"name"`
Version string `json:"version"`
Libraries int `json:"libraries,omitempty"`
ID string `json:"-"`
}
Info 是测试连通性时读到的服务器信息。ID 是服务器自己的编号(Emby、Jellyfin 的 Id,Plex 的 machineIdentifier)。
type Item ¶
type Item struct {
ID string
Kind string
SeriesID string
TMDBID int
IMDbID string
TVDBID string
Season int
Episode int
EpisodeEnd int
Title string
Year int
Path string
AddedAt time.Time
}
Item 是服务器里的一项:Kind 是 models.MediaServerItemMovie、Series 或 Episode。编号是这一项自己的 (集的 TMDB 编号是这一集的,索引里换成所在剧的);Season、Episode、EpisodeEnd 只有集才有;AddedAt 是入库时间,不知道时是零值。
type ItemPage ¶
ItemPage 是列条目的一页。Next 为空表示列完了;Changed 为真表示列的过程中服务器里的条目数变了(有新加或删除, 分页可能错位),这一轮列到的可能不全。
type ResumeItem ¶
type ResumeItem struct {
ID string
Kind string
SeriesID string
SeriesName string
Title string
Season int
Episode int
Year int
TMDBID int
IMDbID string
Percent float64
RemainingSeconds int64
}
ResumeItem 是一个没看完的:Kind 是 movie 或 episode;集带剧的条目编号、剧名、季与集。TMDBID、IMDbID 是这一项自己的 (集的是这一集的);Percent 是看到百分之几,RemainingSeconds 是还剩几秒(不知道片长时是 0)。
type WebhookEvent ¶
type WebhookEvent struct {
Action string
Name string
ItemID string
Kind string
Title string
SeriesTitle string
Season int
Episode int
Year int
User string
Device string
Client string
}
WebhookEvent 是解析出的一次 Webhook。Action 是上面几类之一,Name 是服务器写的原名(ItemAdded、library.new、media.play 等); Kind 是 movie、episode、series 或空串(不是这几种);Title 是这一项的名字,剧集另有 SeriesTitle、Season、Episode。
func ParseWebhook ¶
func ParseWebhook(contentType string, body io.Reader) (WebhookEvent, error)
ParseWebhook 解析一次 Webhook:请求体是 JSON(Emby 选 application/json、Jellyfin 插件的 Generic 目标,Content-Type 常是 text/plain),或表单(Emby 的 data 字段)、multipart(Emby 的 data、Plex 的 payload)。按里面的字段认是哪一家,不看服务器的种类。 最多读 WebhookMaxBody 字节。