@ ctrl/ 复杂 
类型脚本 缩写 使用 API 客户端 函数 基于 pkkid/python-plexapi (英语)
安装
npm install @ctrl/plex
文档
https://plex.ep. works.dev (中文(简体) )
使用
创建复式连接
import { MyPlexAccount } from '@ctrl/plex';
const account = await new MyPlexAccount({
baseUrl: 'http://localhost:32400',
// Use username and password based auth
username: 'username',
password: 'password',
// OR Token based auth
token: 'token',
}).connect();
// List all available servers (resources) attached to your account
const resources = await account.resources();
// Connect to a specific server by name
// You can find the server name in the Plex Web UI settings or by listing resources above
const resource = await account.resource('<SERVERNAME>');
const plex = await resource.connect();
const library = await plex.library();
// List all libraries (Movies, TV Shows, Music, etc.)
const sections = await library.sections();
识别您的应用程序。 Plex 按其产品名称和客户身份标识列出每个客户端, 因此设置 在连接和坚持标识符之前,您自己可以使用,所以Plex不断看到同一设备.
import { setPlexHeaders } from '@ctrl/plex';
setPlexHeaders({
product: 'My App',
version: '1.0.0',
identifier: 'replace-with-a-persisted-client-identifier',
});
不收集 Plex 用户名或密码而认证 。 坚持客户端标识符,所以 整个登录点保持稳定.
import { MyPlexAccount, MyPlexPinLogin } from '@ctrl/plex';
const clientIdentifier = 'replace-with-a-persisted-client-identifier';
const login = await MyPlexPinLogin.create({ clientIdentifier, mode: 'oauth' });
console.log(`Open ${login.oauthUrl()} to sign in`);
const authentication = await login.wait({ timeout: 120_000 });
const account = await new MyPlexAccount({ token: authentication.token }).connect();
创建并刷新已签名的 Plex JWT 。 靴子纹章一定是为同一个 客户端标识符 。 安全存储返回的证书; 它们包含私人签名密钥 .
import { MyPlexAccount, refreshPlexJwt, registerPlexJwt } from '@ctrl/plex';
let credentials = await registerPlexJwt({
token: authentication.token,
clientIdentifier,
scopes: ['username', 'email', 'friendly_name'],
});
credentials = await refreshPlexJwt({ credentials });
const jwtAccount = await new MyPlexAccount({ token: credentials.token }).connect();
例1:列出所有未观看的电影.
import { MovieSection } from '@ctrl/plex';
// Pass MovieSection generic because the section title doesn't imply a section type.
const section = await library.section<MovieSection>('Movies');
// Get an array of Movie objects
const results = await section.search({ unwatched: true });
例2:搜索包含标题的电影列表
const library = await plex.library();
const section = await library.section<MovieSection>('Movies');
const results = await section.search({ title: 'Rush Hour' });
例3:列出所有包含特定查询的内容
const hubs = await plex.search('Arnold');
// Each hub is one category of results (movies, episodes, actors, ...)
for (const hub of hubs) {
const [first] = await hub.items();
console.log(hub.title, first?.title);
}
例4:列出tv节目的所有剧集.
import { ShowSection } from '@ctrl/plex';
// Pass ShowSection generic because the section title doesn't imply a section type.
const section = await library.section<ShowSection>('TV Shows');
// Get an array of Show objects
const results = await section.search({ title: 'Silicon Valley' });
const episodes = await results[0].episodes();
例5:根据搜索结果创建播放列表
import { Playlist } from '@ctrl/plex';
// The ten highest rated movies
const topMovies = await section.search({ sort: 'rating:desc', maxresults: 10 });
// Create a new playlist
const bestMovies = await Playlist.create(plex, 'Best Movies', { items: topMovies });
从 v7 升级
v8是大型发布. 完整列表在发布注释中,最有可能影响您的变化:
- 需要24号节点或更新.
- 类型很严格: 字段Plex可以省略是可选的,以及
PlexServer.query()编号 :MyPlexAccount.query()返回时unknown除非你通过一个类型争论. - 位置参数成为选项对象, 例如
connect({ timeout }), (中文(简体) ),removeItem(item, { refresh }), (中文(简体) ),updateProgress({ time })财务报告和审定财务报表account.device({ name })。 。 。 . PlexClient命令带走{ type }而不是立场mtype,以及proxyThroughServer({ server })替换_server编号 :_proxyThroughServer字段.- 音乐段落默认列出艺术家列表
onDeck()返回事件和音轨. - 音频
art财务报告和审定财务报表thumb是服务器路径, 使用artUrl编号 :thumbUrl用于符号 URL. - plex.tv设备来自JSON API,
MyPlexDevice.id是一个数字.
与 python 复杂客户端的差异
联合来文是另一种语言,因此不可能采用某些方法. 必须主要单独等待与请求挂钩的功能。 JS的建筑师通常不会提出要求 并且访问属性通常也不能提出请求。 方法选择对象而不是蟒蛇样式 关键词参数和答复作为JSON请求.
测试
测试是针对一个真正的复杂情况进行的.
创建一个复杂的账户仅用于测试,使用一个真实账户会打破一切. 将其全权证书放入 .env
文件在 repo 的根部,测试和脚本自动加载.
PLEX_USERNAME=email
PLEX_PASSWORD=password
PLEXAPI_AUTH_SERVER_TOKEN=token
设置测试内容( 一次)
pnpm run add-media
运行测试
pnpm test
后测试, 从账户中删除 plex 服务器 。 警告,这是破坏性的。 不要在真实的复杂账户上使用它.
pnpm run test-cleanup
正在本地测试
步骤1
从 https://www.plex.tv/claim/ (中文(简体) ) 导出 PLEQCLAIM TOKEN = 声明脚本
步骤2
启动复方容器进行测试。 替换 /Users/scooper/gh/plex 带有此 repo 目录的路径.
docker run -d \
--name=plex \
--net=host \
-h orbstack \
-p 32400:32400/tcp \
-p 32400:32400 \
-p 1900:1900/udp \
-p 5353:5353/udp \
-p 8324:8324 \
-p 32410:32410/udp \
-p 32412:32412/udp \
-p 32413:32413/udp \
-p 32414:32414/udp \
-p 32469:32469 \
-e PUID=1000 \
-e PGID=1000 \
-e TZ=Etc/UTC \
-e VERSION=docker \
-e PLEX_CLAIM=$PLEX_CLAIM_TOKEN \
-v /Users/scooper/gh/plex/plex/media/db:/config \
-v /Users/scooper/gh/plex/plex/media/transcode:/transcode \
-v /Users/scooper/gh/plex/plex/media:/data \
--restart unless-stopped \
lscr.io/linuxserver/plex:latest
必要时拉出最新的复方容器
docker pull lscr.io/linuxserver/plex:latest
有测试介质的靴子复合服务器 。 这个用 PLEXAPI_AUTH_SERVER_TOKEN 从 .env。 。 。 .
pnpm run bootstraptest -- --no-docker --server-name=orbstack