mirror of
https://github.com/ngthanhvu/kr-phim.git
synced 2026-08-10 16:47:46 +00:00
feat: implement admin interface for managing custom movie sources and servers with new API endpoint and database schema updates
This commit is contained in:
@@ -32,6 +32,34 @@ export default defineEventHandler(async (event) => {
|
||||
: null
|
||||
}
|
||||
|
||||
if ('actors' in body) {
|
||||
updates.actors = Array.isArray(body.actors) && body.actors.length
|
||||
? body.actors.filter((a: any) => a.name?.trim()).map((a: any) => ({
|
||||
name: a.name.trim(),
|
||||
originalName: a.originalName?.trim() || undefined,
|
||||
role: a.role?.trim() || undefined,
|
||||
avatar: a.avatar?.trim() || undefined,
|
||||
}))
|
||||
: null
|
||||
}
|
||||
|
||||
if ('customServers' in body) {
|
||||
updates.customServers = Array.isArray(body.customServers) && body.customServers.length
|
||||
? body.customServers
|
||||
.filter((server: any) => server.name?.trim())
|
||||
.map((server: any) => ({
|
||||
name: server.name.trim(),
|
||||
episodes: Array.isArray(server.episodes)
|
||||
? server.episodes.filter((ep: any) => ep.name?.trim()).map((ep: any) => ({
|
||||
name: ep.name.trim(),
|
||||
linkEmbed: ep.linkEmbed?.trim() || null,
|
||||
linkM3u8: ep.linkM3u8?.trim() || null,
|
||||
}))
|
||||
: [],
|
||||
}))
|
||||
: null
|
||||
}
|
||||
|
||||
if (!Object.keys(updates).length) {
|
||||
throw createError({ statusCode: 400, message: 'Không có dữ liệu cập nhật' })
|
||||
}
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
import { movies } from '../../../../database/schema'
|
||||
import { eq } from 'drizzle-orm'
|
||||
import { getOphimDetail, getNguoncDetail, getKkphimDetail, type NormalizedServer, type NormalizedActor } from '../../../../utils/movies'
|
||||
|
||||
const detailFetchers = {
|
||||
ophim: getOphimDetail,
|
||||
nguonc: getNguoncDetail,
|
||||
kkphim: getKkphimDetail,
|
||||
}
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const db = useDb()
|
||||
const id = Number(getRouterParam(event, 'id'))
|
||||
|
||||
if (!id) {
|
||||
throw createError({ statusCode: 400, message: 'Thiếu ID phim' })
|
||||
}
|
||||
|
||||
const result = await db.select().from(movies).where(eq(movies.id, id)).limit(1)
|
||||
if (!result.length) {
|
||||
throw createError({ statusCode: 404, message: 'Không tìm thấy phim' })
|
||||
}
|
||||
|
||||
const movie = result[0]
|
||||
const sourceRefs = (movie.sources || []).filter((ref: any) => ref.source && ref.slug)
|
||||
|
||||
if (!sourceRefs.length) {
|
||||
return { movie, sources: [] }
|
||||
}
|
||||
|
||||
const results = await Promise.allSettled(
|
||||
sourceRefs.map((ref: any) => detailFetchers[ref.source as keyof typeof detailFetchers](ref.slug)),
|
||||
)
|
||||
|
||||
const sourceServers: { source: string, slug: string, name: string, content: string, actors: NormalizedActor[], servers: NormalizedServer[] }[] = []
|
||||
|
||||
results.forEach((result, index) => {
|
||||
const ref = sourceRefs[index]
|
||||
if (result.status === 'fulfilled') {
|
||||
sourceServers.push({
|
||||
source: ref.source,
|
||||
slug: ref.slug,
|
||||
name: ref.name || result.value.name,
|
||||
content: result.value.content || '',
|
||||
actors: result.value.actors || [],
|
||||
servers: (result.value.servers || []).map((server: NormalizedServer) => ({
|
||||
...server,
|
||||
source: ref.source,
|
||||
sourceSlug: ref.slug,
|
||||
})),
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
return {
|
||||
movie,
|
||||
sources: sourceServers,
|
||||
}
|
||||
})
|
||||
@@ -1,5 +1,5 @@
|
||||
import { movies } from '../../database/schema'
|
||||
import { eq, and } from 'drizzle-orm'
|
||||
import { eq, and, or } from 'drizzle-orm'
|
||||
import {
|
||||
getOphimKoreanMovies,
|
||||
getNguoncKoreanMovies,
|
||||
@@ -7,6 +7,8 @@ import {
|
||||
getOphimDetail,
|
||||
getNguoncDetail,
|
||||
getKkphimDetail,
|
||||
groupMovies,
|
||||
type NormalizedMovie,
|
||||
} from '../../utils/movies'
|
||||
|
||||
const SYNC_PAGES = 10
|
||||
@@ -19,28 +21,41 @@ const detailFetchers = {
|
||||
kkphim: getKkphimDetail,
|
||||
}
|
||||
|
||||
async function fetchEpisodeTotal(source: string, slug: string): Promise<string | undefined> {
|
||||
async function fetchMovieDetail(source: string, slug: string): Promise<{ episodeTotal?: string, actors?: { name: string, originalName?: string, role?: string, avatar?: string }[] } | undefined> {
|
||||
const fetcher = detailFetchers[source as keyof typeof detailFetchers]
|
||||
if (!fetcher) return undefined
|
||||
try {
|
||||
const detail = await fetcher(slug)
|
||||
return detail.episodeTotal || undefined
|
||||
return {
|
||||
episodeTotal: detail.episodeTotal || undefined,
|
||||
actors: detail.actors?.length ? detail.actors.map((a) => ({
|
||||
name: a.name,
|
||||
originalName: a.originalName,
|
||||
role: a.role,
|
||||
avatar: a.avatar,
|
||||
})) : undefined,
|
||||
}
|
||||
} catch {
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
||||
async function enrichWithEpisodeTotal(moviesList: any[]): Promise<void> {
|
||||
const moviesNeedTotal = moviesList.filter((m) => m.type !== 'single' && !m.episodeTotal && m.slug)
|
||||
async function enrichMovieDetails(moviesList: any[]): Promise<void> {
|
||||
const moviesNeedDetail = moviesList.filter((m) => m.slug && (m.type !== 'single' || !m.actors))
|
||||
|
||||
for (let i = 0; i < moviesNeedTotal.length; i += DETAIL_CONCURRENCY) {
|
||||
const batch = moviesNeedTotal.slice(i, i + DETAIL_CONCURRENCY)
|
||||
for (let i = 0; i < moviesNeedDetail.length; i += DETAIL_CONCURRENCY) {
|
||||
const batch = moviesNeedDetail.slice(i, i + DETAIL_CONCURRENCY)
|
||||
const results = await Promise.allSettled(
|
||||
batch.map((movie) => fetchEpisodeTotal(movie.source, movie.slug)),
|
||||
batch.map((movie) => fetchMovieDetail(movie.source, movie.slug)),
|
||||
)
|
||||
results.forEach((result, idx) => {
|
||||
if (result.status === 'fulfilled' && result.value) {
|
||||
batch[idx].episodeTotal = result.value
|
||||
if (result.value.episodeTotal) {
|
||||
batch[idx].episodeTotal = result.value.episodeTotal
|
||||
}
|
||||
if (result.value.actors) {
|
||||
batch[idx].actors = result.value.actors
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -62,79 +77,94 @@ export default defineEventHandler(async (event) => {
|
||||
nguonc: getNguoncKoreanMovies,
|
||||
kkphim: getKkphimKoreanMovies,
|
||||
}
|
||||
const sourceStats: Record<string, { fetched: number, error?: string }> = {}
|
||||
|
||||
for (const sourceName of sources) {
|
||||
const fetcher = fetchers[sourceName]
|
||||
if (!fetcher) continue
|
||||
if (!fetcher) {
|
||||
sourceStats[sourceName] = { fetched: 0, error: 'Không tìm thấy fetcher' }
|
||||
continue
|
||||
}
|
||||
let fetched = 0
|
||||
for (let page = 1; page <= SYNC_PAGES; page++) {
|
||||
try {
|
||||
const result = await fetcher(page)
|
||||
for (const movie of result.items) {
|
||||
allMovies.push({ ...movie, source: sourceName })
|
||||
}
|
||||
fetched += result.items.length
|
||||
if (!result.items.length) break
|
||||
} catch {
|
||||
} catch (err: any) {
|
||||
sourceStats[sourceName] = { fetched, error: err?.message || `Lỗi ở trang ${page}` }
|
||||
break
|
||||
}
|
||||
}
|
||||
if (!sourceStats[sourceName]) {
|
||||
sourceStats[sourceName] = { fetched }
|
||||
}
|
||||
}
|
||||
|
||||
await enrichWithEpisodeTotal(allMovies)
|
||||
await enrichMovieDetails(allMovies)
|
||||
|
||||
// Group movies across sources using the existing grouping logic
|
||||
const grouped = groupMovies(allMovies as NormalizedMovie[])
|
||||
|
||||
let created = 0
|
||||
let updated = 0
|
||||
|
||||
for (const movie of allMovies) {
|
||||
const existing = await db
|
||||
.select()
|
||||
.from(movies)
|
||||
.where(and(eq(movies.source, movie.source), eq(movies.slug, movie.slug)))
|
||||
.limit(1)
|
||||
for (const primary of grouped) {
|
||||
const sourceRefs = (primary.sources || []).map((ref: any) => ({ source: ref.source, slug: ref.slug, name: ref.name || primary.name }))
|
||||
const sourceSlugConditions = sourceRefs
|
||||
.filter((ref: any) => ref.source && ref.slug)
|
||||
.map((ref: any) => and(eq(movies.source, ref.source), eq(movies.slug, ref.slug)))
|
||||
|
||||
if (existing.length) {
|
||||
await db
|
||||
.update(movies)
|
||||
.set({
|
||||
name: movie.name,
|
||||
originName: movie.originName || null,
|
||||
thumb: movie.thumb || null,
|
||||
poster: movie.poster || null,
|
||||
year: movie.year || null,
|
||||
time: movie.time || null,
|
||||
episode: movie.episode || null,
|
||||
episodeTotal: movie.episodeTotal || null,
|
||||
quality: movie.quality || null,
|
||||
lang: movie.lang || null,
|
||||
type: movie.type || null,
|
||||
rating: movie.rating || null,
|
||||
categories: movie.categories || null,
|
||||
countries: movie.countries || null,
|
||||
sources: movie.sources || null,
|
||||
apiUpdatedAt: movie.updatedAt ? new Date(movie.updatedAt) : null,
|
||||
syncedAt: new Date(),
|
||||
})
|
||||
.where(eq(movies.id, existing[0].id))
|
||||
// Find existing record by any (source, slug) in the group, or same name/year
|
||||
let existing = null
|
||||
if (sourceSlugConditions.length) {
|
||||
const existingBySource = await db.select().from(movies).where(or(...sourceSlugConditions)).limit(1)
|
||||
if (existingBySource.length) existing = existingBySource[0]
|
||||
}
|
||||
|
||||
if (!existing) {
|
||||
const nameConditions: any[] = [eq(movies.name, primary.name)]
|
||||
if (primary.year) nameConditions.push(eq(movies.year, primary.year))
|
||||
const existingByName = await db
|
||||
.select()
|
||||
.from(movies)
|
||||
.where(and(...nameConditions))
|
||||
.limit(1)
|
||||
if (existingByName.length) existing = existingByName[0]
|
||||
}
|
||||
|
||||
const movieData = {
|
||||
name: primary.name,
|
||||
originName: primary.originName || null,
|
||||
thumb: primary.thumb || null,
|
||||
poster: primary.poster || null,
|
||||
year: primary.year || null,
|
||||
time: primary.time || null,
|
||||
episode: primary.episode || null,
|
||||
episodeTotal: primary.episodeTotal || null,
|
||||
quality: primary.quality || null,
|
||||
lang: primary.lang || null,
|
||||
type: primary.type || null,
|
||||
rating: primary.rating || null,
|
||||
categories: primary.categories || null,
|
||||
countries: primary.countries || null,
|
||||
actors: (primary as any).actors || null,
|
||||
sources: sourceRefs,
|
||||
apiUpdatedAt: primary.updatedAt ? new Date(primary.updatedAt) : null,
|
||||
syncedAt: new Date(),
|
||||
}
|
||||
|
||||
if (existing) {
|
||||
await db.update(movies).set(movieData).where(eq(movies.id, existing.id))
|
||||
updated++
|
||||
} else {
|
||||
await db.insert(movies).values({
|
||||
source: movie.source,
|
||||
slug: movie.slug,
|
||||
name: movie.name,
|
||||
originName: movie.originName || null,
|
||||
thumb: movie.thumb || null,
|
||||
poster: movie.poster || null,
|
||||
year: movie.year || null,
|
||||
time: movie.time || null,
|
||||
episode: movie.episode || null,
|
||||
episodeTotal: movie.episodeTotal || null,
|
||||
quality: movie.quality || null,
|
||||
lang: movie.lang || null,
|
||||
type: movie.type || null,
|
||||
rating: movie.rating || null,
|
||||
categories: movie.categories || null,
|
||||
countries: movie.countries || null,
|
||||
sources: movie.sources || null,
|
||||
apiUpdatedAt: movie.updatedAt ? new Date(movie.updatedAt) : null,
|
||||
source: primary.source,
|
||||
slug: primary.slug,
|
||||
...movieData,
|
||||
active: false,
|
||||
})
|
||||
created++
|
||||
@@ -147,5 +177,6 @@ export default defineEventHandler(async (event) => {
|
||||
total: allMovies.length,
|
||||
created,
|
||||
updated,
|
||||
sourceStats,
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user