emby分类获取
This commit is contained in:
@@ -12,10 +12,11 @@ export async function GET(request: NextRequest) {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const page = parseInt(searchParams.get('page') || '1');
|
||||
const pageSize = parseInt(searchParams.get('pageSize') || '20');
|
||||
const parentId = searchParams.get('parentId') || undefined;
|
||||
|
||||
try {
|
||||
// 检查缓存
|
||||
const cached = getCachedEmbyList(page, pageSize);
|
||||
const cached = getCachedEmbyList(page, pageSize, parentId);
|
||||
if (cached) {
|
||||
return NextResponse.json(cached);
|
||||
}
|
||||
@@ -65,6 +66,7 @@ export async function GET(request: NextRequest) {
|
||||
|
||||
// 获取媒体列表
|
||||
const result = await client.getItems({
|
||||
ParentId: parentId,
|
||||
IncludeItemTypes: 'Movie,Series',
|
||||
Recursive: true,
|
||||
Fields: 'Overview,ProductionYear',
|
||||
@@ -94,7 +96,7 @@ export async function GET(request: NextRequest) {
|
||||
};
|
||||
|
||||
// 缓存结果
|
||||
setCachedEmbyList(page, pageSize, response);
|
||||
setCachedEmbyList(page, pageSize, response, parentId);
|
||||
|
||||
return NextResponse.json(response);
|
||||
} catch (error) {
|
||||
|
||||
81
src/app/api/emby/views/route.ts
Normal file
81
src/app/api/emby/views/route.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
|
||||
import { NextResponse } from 'next/server';
|
||||
|
||||
import { getConfig } from '@/lib/config';
|
||||
import { EmbyClient } from '@/lib/emby.client';
|
||||
import { getCachedEmbyViews, setCachedEmbyViews } from '@/lib/emby-cache';
|
||||
|
||||
export const runtime = 'nodejs';
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
// 检查缓存
|
||||
const cached = getCachedEmbyViews();
|
||||
if (cached) {
|
||||
return NextResponse.json(cached);
|
||||
}
|
||||
|
||||
const config = await getConfig();
|
||||
const embyConfig = config.EmbyConfig;
|
||||
|
||||
if (!embyConfig?.Enabled || !embyConfig.ServerURL) {
|
||||
return NextResponse.json({
|
||||
error: 'Emby 未配置或未启用',
|
||||
views: [],
|
||||
});
|
||||
}
|
||||
|
||||
// 创建 Emby 客户端
|
||||
const client = new EmbyClient(embyConfig);
|
||||
|
||||
// 如果使用用户名密码且没有 UserId,需要先认证
|
||||
if (!embyConfig.ApiKey && !embyConfig.UserId && embyConfig.Username && embyConfig.Password) {
|
||||
try {
|
||||
const authResult = await client.authenticate(embyConfig.Username, embyConfig.Password);
|
||||
embyConfig.UserId = authResult.User.Id;
|
||||
} catch (error) {
|
||||
return NextResponse.json({
|
||||
error: 'Emby 认证失败: ' + (error as Error).message,
|
||||
views: [],
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// 验证认证信息:必须有 ApiKey 或 UserId
|
||||
if (!embyConfig.ApiKey && !embyConfig.UserId) {
|
||||
return NextResponse.json({
|
||||
error: 'Emby 认证失败,请检查配置',
|
||||
views: [],
|
||||
});
|
||||
}
|
||||
|
||||
// 获取媒体库列表
|
||||
const views = await client.getUserViews();
|
||||
|
||||
// 过滤出电影和电视剧媒体库
|
||||
const filteredViews = views.filter(
|
||||
(view) => view.CollectionType === 'movies' || view.CollectionType === 'tvshows'
|
||||
);
|
||||
|
||||
const response = {
|
||||
success: true,
|
||||
views: filteredViews.map((view) => ({
|
||||
id: view.Id,
|
||||
name: view.Name,
|
||||
type: view.CollectionType,
|
||||
})),
|
||||
};
|
||||
|
||||
// 缓存结果
|
||||
setCachedEmbyViews(response);
|
||||
|
||||
return NextResponse.json(response);
|
||||
} catch (error) {
|
||||
console.error('获取 Emby 媒体库列表失败:', error);
|
||||
return NextResponse.json({
|
||||
error: '获取 Emby 媒体库列表失败: ' + (error as Error).message,
|
||||
views: [],
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user