server

package
v1.0.0-rc.58 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package server 是媒体服务器的客户端:Emby、Jellyfin 与 Plex。整理入库后通知媒体服务器扫描新目录 (按路径或刷新整个媒体库),也能测试连通性、按 TMDB 或 IMDb 编号查一个条目是不是已经入库, 分页列出服务器里的电影、剧与集(索引用),取用户与没看完的(继续观看,Emby 与 Jellyfin), 拼条目在服务器网页里的打开链接,解析三家发来的 Webhook。

Index

Constants

View Source
const (
	WebhookAdded         = "added"
	WebhookRemoved       = "removed"
	WebhookPlaybackStart = "playback_start"
	WebhookPlaybackStop  = "playback_stop"
	WebhookOther         = "other"
)

Webhook 事件归成的几类。

View Source
const WebhookMaxBody = 1 << 20

WebhookMaxBody 是一次 Webhook 最多读多少字节(Plex 的 multipart 里 payload 在缩略图前面,读到 payload 就不往下读)。

Variables

View Source
var (
	// ErrUnauthorized 表示 API Key 或 Token 不对,或者没有管理员权限。
	ErrUnauthorized = errors.New("媒体服务器拒绝了请求:API Key 或 Token 不对,或者没有管理员权限")
	// ErrUnavailable 表示连不上媒体服务器,或它回了其它错误。
	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("这种媒体服务器不支持")
)
View Source
var ErrBadWebhook = errors.New("看不懂的 Webhook 请求")

ErrBadWebhook 表示请求体不是认得的 Webhook(不是 JSON,或 multipart 里没有 data、payload)。

Functions

func CheckURL

func CheckURL(raw string) (string, error)

CheckURL 检查媒体服务器地址:http 或 https,有主机,不带查询参数与片段;返回去掉末尾 / 的地址。

func HomeURL

func HomeURL(kind, base string) string

HomeURL 是媒体服务器网页的首页(首页状态行的「打开」):base 是浏览器能打开的地址;种类不认得或没有地址时为空。

func ItemURL

func ItemURL(kind, base, uid, itemID string) string

ItemURL 是一项在媒体服务器网页里的打开链接。base 是浏览器能打开的服务器地址,uid 是服务器编号(Plex 没有编号时拼不出)。

func NewHTTPClient

func NewHTTPClient() *http.Client

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 是一个媒体服务器。

func New

func New(cfg Config) (Client, error)

New 按种类建客户端。

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

type ItemPage struct {
	Items   []Item
	Next    string
	Changed bool
}

ItemPage 是列条目的一页。Next 为空表示列完了;Changed 为真表示列的过程中服务器里的条目数变了(有新加或删除, 分页可能错位),这一轮列到的可能不全。

type Query

type Query struct {
	Kind   string // movie 或 tv
	TMDBID int
	IMDbID string
	Title  string
}

Query 是「是不是已经入库」的查询:按名字找,再比对 TMDB 或 IMDb 编号。

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 User

type User struct {
	ID   string `json:"id"`
	Name string `json:"name"`
}

User 是服务器的一个用户。

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 字节。

Jump to

Keyboard shortcuts

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