download anime episodes choosing quality, audio, subs...
This commit is contained in:
@@ -17,20 +17,52 @@ type MatchBody = {
|
||||
matched_id: number | null;
|
||||
};
|
||||
|
||||
type DownloadAnimeBody = {
|
||||
type DownloadAnimeBody =
|
||||
| {
|
||||
anilist_id: number;
|
||||
episode_number: number;
|
||||
stream_url: string;
|
||||
quality?: string;
|
||||
subtitles?: Array<{
|
||||
stream_url: string; // media playlist FINAL
|
||||
is_master?: false;
|
||||
subtitles?: {
|
||||
language: string;
|
||||
url: string;
|
||||
}>;
|
||||
chapters?: Array<{
|
||||
}[];
|
||||
chapters?: {
|
||||
title: string;
|
||||
start_time: number;
|
||||
end_time: number;
|
||||
}>;
|
||||
}[];
|
||||
}
|
||||
| {
|
||||
anilist_id: number;
|
||||
episode_number: number;
|
||||
stream_url: string; // master.m3u8
|
||||
is_master: true;
|
||||
|
||||
variant: {
|
||||
resolution: string;
|
||||
bandwidth?: number;
|
||||
codecs?: string;
|
||||
playlist_url: string;
|
||||
};
|
||||
|
||||
audio?: {
|
||||
group?: string;
|
||||
language?: string;
|
||||
name?: string;
|
||||
playlist_url: string;
|
||||
}[];
|
||||
|
||||
subtitles?: {
|
||||
language: string;
|
||||
url: string;
|
||||
}[];
|
||||
|
||||
chapters?: {
|
||||
title: string;
|
||||
start_time: number;
|
||||
end_time: number;
|
||||
}[];
|
||||
};
|
||||
|
||||
type DownloadBookBody = {
|
||||
@@ -212,17 +244,30 @@ export async function getPage(request: FastifyRequest, reply: FastifyReply) {
|
||||
return reply.status(400).send();
|
||||
}
|
||||
|
||||
function buildProxyUrl(rawUrl: string, headers: Record<string, string>) {
|
||||
const params = new URLSearchParams({ url: rawUrl });
|
||||
|
||||
for (const [key, value] of Object.entries(headers)) {
|
||||
params.set(key.toLowerCase(), value);
|
||||
}
|
||||
|
||||
return `http://localhost:54322/api/proxy?${params.toString()}`;
|
||||
}
|
||||
|
||||
export async function downloadAnime(request: FastifyRequest<{ Body: DownloadAnimeBody }>, reply: FastifyReply) {
|
||||
try {
|
||||
const {
|
||||
anilist_id,
|
||||
episode_number,
|
||||
stream_url,
|
||||
quality,
|
||||
is_master,
|
||||
subtitles,
|
||||
chapters
|
||||
} = request.body;
|
||||
|
||||
const clientHeaders = (request.body as any).headers || {};
|
||||
|
||||
// Validación básica
|
||||
if (!anilist_id || !episode_number || !stream_url) {
|
||||
return reply.status(400).send({
|
||||
error: 'MISSING_REQUIRED_FIELDS',
|
||||
@@ -230,14 +275,58 @@ export async function downloadAnime(request: FastifyRequest<{ Body: DownloadAnim
|
||||
});
|
||||
}
|
||||
|
||||
const result = await downloadService.downloadAnimeEpisode({
|
||||
// Proxy del stream URL principal
|
||||
const proxyUrl = buildProxyUrl(stream_url, clientHeaders);
|
||||
console.log('Stream URL:', proxyUrl);
|
||||
|
||||
// Proxy de subtítulos
|
||||
const proxiedSubs = subtitles?.map(sub => ({
|
||||
...sub,
|
||||
url: buildProxyUrl(sub.url, clientHeaders)
|
||||
}));
|
||||
|
||||
// Preparar parámetros base
|
||||
const downloadParams: any = {
|
||||
anilistId: anilist_id,
|
||||
episodeNumber: episode_number,
|
||||
streamUrl: stream_url,
|
||||
quality,
|
||||
subtitles,
|
||||
streamUrl: proxyUrl,
|
||||
subtitles: proxiedSubs,
|
||||
chapters
|
||||
});
|
||||
};
|
||||
|
||||
// Si es master playlist, agregar campos adicionales
|
||||
if (is_master === true) {
|
||||
const { variant, audio } = request.body as any;
|
||||
|
||||
if (!variant || !variant.playlist_url) {
|
||||
return reply.status(400).send({
|
||||
error: 'MISSING_VARIANT',
|
||||
message: 'variant with playlist_url is required when is_master is true'
|
||||
});
|
||||
}
|
||||
|
||||
downloadParams.is_master = true;
|
||||
|
||||
// Proxy del variant playlist
|
||||
downloadParams.variant = {
|
||||
...variant,
|
||||
playlist_url: buildProxyUrl(variant.playlist_url, clientHeaders)
|
||||
};
|
||||
|
||||
// Proxy de audio tracks si existen
|
||||
if (audio && audio.length > 0) {
|
||||
downloadParams.audio = audio.map((a: any) => ({
|
||||
...a,
|
||||
playlist_url: buildProxyUrl(a.playlist_url, clientHeaders)
|
||||
}));
|
||||
}
|
||||
|
||||
console.log('Master playlist detected');
|
||||
console.log('Variant:', downloadParams.variant.resolution);
|
||||
console.log('Audio tracks:', downloadParams.audio?.length || 0);
|
||||
}
|
||||
|
||||
const result = await downloadService.downloadAnimeEpisode(downloadParams);
|
||||
|
||||
if (result.status === 'ALREADY_EXISTS') {
|
||||
return reply.status(409).send(result);
|
||||
@@ -251,6 +340,10 @@ export async function downloadAnime(request: FastifyRequest<{ Body: DownloadAnim
|
||||
return reply.status(404).send({ error: 'ANIME_NOT_FOUND_IN_ANILIST' });
|
||||
}
|
||||
|
||||
if (err.message === 'VARIANT_REQUIRED_FOR_MASTER') {
|
||||
return reply.status(400).send({ error: 'VARIANT_REQUIRED_FOR_MASTER' });
|
||||
}
|
||||
|
||||
if (err.message === 'DOWNLOAD_FAILED') {
|
||||
return reply.status(500).send({ error: 'DOWNLOAD_FAILED', details: err.details });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user