emby分类获取
This commit is contained in:
+37
-6
@@ -8,13 +8,15 @@ export interface EmbyCachedEntry<T> {
|
||||
|
||||
// 缓存配置
|
||||
const EMBY_CACHE_TTL_MS = 6 * 60 * 60 * 1000; // 6小时
|
||||
const EMBY_VIEWS_CACHE_TTL_MS = 24 * 60 * 60 * 1000; // 1天
|
||||
const EMBY_CACHE: Map<string, EmbyCachedEntry<any>> = new Map();
|
||||
const EMBY_VIEWS_CACHE_KEY = 'emby:views';
|
||||
|
||||
/**
|
||||
* 生成 Emby 列表缓存键
|
||||
*/
|
||||
function makeListCacheKey(page: number, pageSize: number): string {
|
||||
return `emby:list:${page}:${pageSize}`;
|
||||
function makeListCacheKey(page: number, pageSize: number, parentId?: string): string {
|
||||
return parentId ? `emby:list:${page}:${pageSize}:${parentId}` : `emby:list:${page}:${pageSize}`;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -22,9 +24,10 @@ function makeListCacheKey(page: number, pageSize: number): string {
|
||||
*/
|
||||
export function getCachedEmbyList(
|
||||
page: number,
|
||||
pageSize: number
|
||||
pageSize: number,
|
||||
parentId?: string
|
||||
): any | null {
|
||||
const key = makeListCacheKey(page, pageSize);
|
||||
const key = makeListCacheKey(page, pageSize, parentId);
|
||||
const entry = EMBY_CACHE.get(key);
|
||||
if (!entry) return null;
|
||||
|
||||
@@ -43,10 +46,11 @@ export function getCachedEmbyList(
|
||||
export function setCachedEmbyList(
|
||||
page: number,
|
||||
pageSize: number,
|
||||
data: any
|
||||
data: any,
|
||||
parentId?: string
|
||||
): void {
|
||||
const now = Date.now();
|
||||
const key = makeListCacheKey(page, pageSize);
|
||||
const key = makeListCacheKey(page, pageSize, parentId);
|
||||
EMBY_CACHE.set(key, {
|
||||
expiresAt: now + EMBY_CACHE_TTL_MS,
|
||||
data,
|
||||
@@ -62,6 +66,33 @@ export function clearEmbyCache(): { cleared: number } {
|
||||
return { cleared: size };
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取缓存的 Emby 媒体库列表
|
||||
*/
|
||||
export function getCachedEmbyViews(): any | null {
|
||||
const entry = EMBY_CACHE.get(EMBY_VIEWS_CACHE_KEY);
|
||||
if (!entry) return null;
|
||||
|
||||
// 检查是否过期
|
||||
if (entry.expiresAt <= Date.now()) {
|
||||
EMBY_CACHE.delete(EMBY_VIEWS_CACHE_KEY);
|
||||
return null;
|
||||
}
|
||||
|
||||
return entry.data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置缓存的 Emby 媒体库列表
|
||||
*/
|
||||
export function setCachedEmbyViews(data: any): void {
|
||||
const now = Date.now();
|
||||
EMBY_CACHE.set(EMBY_VIEWS_CACHE_KEY, {
|
||||
expiresAt: now + EMBY_VIEWS_CACHE_TTL_MS,
|
||||
data,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取缓存统计信息
|
||||
*/
|
||||
|
||||
@@ -51,6 +51,12 @@ interface GetItemsParams {
|
||||
searchTerm?: string;
|
||||
}
|
||||
|
||||
interface EmbyView {
|
||||
Id: string;
|
||||
Name: string;
|
||||
CollectionType?: string;
|
||||
}
|
||||
|
||||
export class EmbyClient {
|
||||
private serverUrl: string;
|
||||
private apiKey?: string;
|
||||
@@ -165,6 +171,49 @@ export class EmbyClient {
|
||||
}
|
||||
}
|
||||
|
||||
async getUserViews(): Promise<EmbyView[]> {
|
||||
await this.ensureAuthenticated();
|
||||
|
||||
if (!this.userId) {
|
||||
throw new Error('未配置 Emby 用户 ID,请在管理面板重新保存 Emby 配置');
|
||||
}
|
||||
|
||||
const token = this.apiKey || this.authToken;
|
||||
const url = `${this.serverUrl}/Users/${this.userId}/Views${token ? `?api_key=${token}` : ''}`;
|
||||
|
||||
console.log('[EmbyClient] getUserViews - URL:', url);
|
||||
|
||||
const response = await fetch(url);
|
||||
|
||||
// 如果是 401 错误且有用户名密码,尝试重新认证
|
||||
if (response.status === 401 && this.username && this.password && !this.apiKey) {
|
||||
console.log('[EmbyClient] Token expired, re-authenticating...');
|
||||
const authResult = await this.authenticate(this.username, this.password);
|
||||
this.authToken = authResult.AccessToken;
|
||||
this.userId = authResult.User.Id;
|
||||
|
||||
// 重试请求
|
||||
const retryUrl = `${this.serverUrl}/Users/${this.userId}/Views?api_key=${this.authToken}`;
|
||||
const retryResponse = await fetch(retryUrl);
|
||||
|
||||
if (!retryResponse.ok) {
|
||||
const errorText = await retryResponse.text();
|
||||
throw new Error(`获取 Emby 媒体库列表失败 (${retryResponse.status}): ${errorText}`);
|
||||
}
|
||||
|
||||
const retryData = await retryResponse.json();
|
||||
return retryData.Items || [];
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
throw new Error(`获取 Emby 媒体库列表失败 (${response.status}): ${errorText}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
return data.Items || [];
|
||||
}
|
||||
|
||||
async getItems(params: GetItemsParams): Promise<EmbyItemsResult> {
|
||||
await this.ensureAuthenticated();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user