empty data or errors from extensions are not cached now

This commit is contained in:
2026-01-13 00:41:52 +01:00
parent eac5613c91
commit 9ac6704bba
6 changed files with 109 additions and 25 deletions

View File

@@ -181,7 +181,9 @@ export async function getAnimeById(id: string | number): Promise<Anime | { error
`; `;
const data = await fetchAniList(query, { id: Number(id) }); const data = await fetchAniList(query, { id: Number(id) });
if (!data?.Media) return { error: "Anime not found" }; if (!data?.Media || !data.Media.title) {
return { error: "Anime not found" };
}
await queryOne( await queryOne(
"INSERT INTO anime (id, title, updatedAt, full_data) VALUES (?, ?, ?, ?)", "INSERT INTO anime (id, title, updatedAt, full_data) VALUES (?, ?, ?, ?)",
@@ -354,7 +356,11 @@ export async function getAnimeInfoExtension(ext: Extension | null, id: string):
try { try {
const match = await ext.getMetadata(id); const match = await ext.getMetadata(id);
if (match) { if (
match &&
match.title &&
match.title !== "Unknown"
) {
const normalized: any = { const normalized: any = {
title: match.title ?? "Unknown", title: match.title ?? "Unknown",
summary: match.summary ?? "No summary available", summary: match.summary ?? "No summary available",
@@ -524,8 +530,9 @@ export async function searchEpisodesInExtension(ext: Extension | null, name: str
title: ep.title title: ep.title
})); }));
// Cachear tanto el mediaId como los episodios if (result.length > 0) {
await setCache(cacheKey, { mediaId, episodes: result }, CACHE_TTL_MS); await setCache(cacheKey, { mediaId, episodes: result }, CACHE_TTL_MS);
}
return result; return result;
} catch (e) { } catch (e) {
@@ -598,10 +605,26 @@ export async function getStreamData(extension: Extension, episode: string, id: s
throw new Error("Episode not found"); throw new Error("Episode not found");
} }
const streamData = await extension.findEpisodeServer(targetEp, server, category); let streamData: StreamData;
try {
streamData = await extension.findEpisodeServer(targetEp, server, category);
} catch (e) {
console.error(`[${providerName}] findEpisodeServer failed`, e);
throw e;
}
if (
!streamData ||
!Array.isArray(streamData.videoSources) ||
streamData.videoSources.length === 0
) {
throw new Error("Empty stream data");
}
await setCache(cacheKey, streamData, CACHE_TTL_MS); await setCache(cacheKey, streamData, CACHE_TTL_MS);
return streamData; return streamData;
} }
function similarity(s1: string, s2: string): number { function similarity(s1: string, s2: string): number {

View File

@@ -275,7 +275,12 @@ export async function getBookInfoExtension(ext: Extension | null, id: string): P
try { try {
const info = await ext.getMetadata(id); const info = await ext.getMetadata(id);
if (info) { if (
info &&
info.title &&
info.title !== "" &&
info.title !== "Unknown"
) {
const normalized = { const normalized = {
id: info.id ?? id, id: info.id ?? id,
title: info.title ?? "", title: info.title ?? "",
@@ -320,6 +325,8 @@ export async function searchBooksInExtension(ext: Extension | null, name: string
}); });
if (matches?.length) { if (matches?.length) {
const clean = matches.filter(m => m.id && m.title);
if (!clean.length) return [];
return matches.map(m => ({ return matches.map(m => ({
id: m.id, id: m.id,
extensionName: name, extensionName: name,
@@ -459,10 +466,13 @@ async function searchChaptersInExtension(ext: Extension, name: string, lookupId:
language: ch.language ?? null, language: ch.language ?? null,
})); }));
if (result.length > 0) {
await setCache(cacheKey, { await setCache(cacheKey, {
mediaId, mediaId,
chapters: result chapters: result
}, CACHE_TTL_MS); }, CACHE_TTL_MS);
}
return result; return result;
} catch (e) { } catch (e) {
@@ -599,7 +609,15 @@ export async function getChapterContent(bookId: string, chapterId: string, provi
throw new Error("Unknown mediaType"); throw new Error("Unknown mediaType");
} }
if (
contentResult &&
(
(contentResult.type === "manga" && contentResult.pages?.length) ||
(contentResult.type === "ln" && contentResult.content)
)
) {
await setCache(contentCacheKey, contentResult, CACHE_TTL_MS); await setCache(contentCacheKey, contentResult, CACHE_TTL_MS);
}
return contentResult; return contentResult;
} catch (err) { } catch (err) {

View File

@@ -111,6 +111,7 @@ export interface ExtensionSettings {
} }
export interface StreamData { export interface StreamData {
videoSources: any;
url?: string; url?: string;
sources?: any[]; sources?: any[];
subtitles?: any[]; subtitles?: any[];

View File

@@ -181,7 +181,9 @@ export async function getAnimeById(id: string | number): Promise<Anime | { error
`; `;
const data = await fetchAniList(query, { id: Number(id) }); const data = await fetchAniList(query, { id: Number(id) });
if (!data?.Media) return { error: "Anime not found" }; if (!data?.Media || !data.Media.title) {
return { error: "Anime not found" };
}
await queryOne( await queryOne(
"INSERT INTO anime (id, title, updatedAt, full_data) VALUES (?, ?, ?, ?)", "INSERT INTO anime (id, title, updatedAt, full_data) VALUES (?, ?, ?, ?)",
@@ -354,7 +356,11 @@ export async function getAnimeInfoExtension(ext: Extension | null, id: string):
try { try {
const match = await ext.getMetadata(id); const match = await ext.getMetadata(id);
if (match) { if (
match &&
match.title &&
match.title !== "Unknown"
) {
const normalized: any = { const normalized: any = {
title: match.title ?? "Unknown", title: match.title ?? "Unknown",
summary: match.summary ?? "No summary available", summary: match.summary ?? "No summary available",
@@ -524,8 +530,9 @@ export async function searchEpisodesInExtension(ext: Extension | null, name: str
title: ep.title title: ep.title
})); }));
// Cachear tanto el mediaId como los episodios if (result.length > 0) {
await setCache(cacheKey, { mediaId, episodes: result }, CACHE_TTL_MS); await setCache(cacheKey, { mediaId, episodes: result }, CACHE_TTL_MS);
}
return result; return result;
} catch (e) { } catch (e) {
@@ -598,10 +605,26 @@ export async function getStreamData(extension: Extension, episode: string, id: s
throw new Error("Episode not found"); throw new Error("Episode not found");
} }
const streamData = await extension.findEpisodeServer(targetEp, server, category); let streamData: StreamData;
try {
streamData = await extension.findEpisodeServer(targetEp, server, category);
} catch (e) {
console.error(`[${providerName}] findEpisodeServer failed`, e);
throw e;
}
if (
!streamData ||
!Array.isArray(streamData.videoSources) ||
streamData.videoSources.length === 0
) {
throw new Error("Empty stream data");
}
await setCache(cacheKey, streamData, CACHE_TTL_MS); await setCache(cacheKey, streamData, CACHE_TTL_MS);
return streamData; return streamData;
} }
function similarity(s1: string, s2: string): number { function similarity(s1: string, s2: string): number {

View File

@@ -275,7 +275,12 @@ export async function getBookInfoExtension(ext: Extension | null, id: string): P
try { try {
const info = await ext.getMetadata(id); const info = await ext.getMetadata(id);
if (info) { if (
info &&
info.title &&
info.title !== "" &&
info.title !== "Unknown"
) {
const normalized = { const normalized = {
id: info.id ?? id, id: info.id ?? id,
title: info.title ?? "", title: info.title ?? "",
@@ -320,6 +325,8 @@ export async function searchBooksInExtension(ext: Extension | null, name: string
}); });
if (matches?.length) { if (matches?.length) {
const clean = matches.filter(m => m.id && m.title);
if (!clean.length) return [];
return matches.map(m => ({ return matches.map(m => ({
id: m.id, id: m.id,
extensionName: name, extensionName: name,
@@ -459,10 +466,13 @@ async function searchChaptersInExtension(ext: Extension, name: string, lookupId:
language: ch.language ?? null, language: ch.language ?? null,
})); }));
if (result.length > 0) {
await setCache(cacheKey, { await setCache(cacheKey, {
mediaId, mediaId,
chapters: result chapters: result
}, CACHE_TTL_MS); }, CACHE_TTL_MS);
}
return result; return result;
} catch (e) { } catch (e) {
@@ -599,7 +609,15 @@ export async function getChapterContent(bookId: string, chapterId: string, provi
throw new Error("Unknown mediaType"); throw new Error("Unknown mediaType");
} }
if (
contentResult &&
(
(contentResult.type === "manga" && contentResult.pages?.length) ||
(contentResult.type === "ln" && contentResult.content)
)
) {
await setCache(contentCacheKey, contentResult, CACHE_TTL_MS); await setCache(contentCacheKey, contentResult, CACHE_TTL_MS);
}
return contentResult; return contentResult;
} catch (err) { } catch (err) {

View File

@@ -111,6 +111,7 @@ export interface ExtensionSettings {
} }
export interface StreamData { export interface StreamData {
videoSources: any;
url?: string; url?: string;
sources?: any[]; sources?: any[];
subtitles?: any[]; subtitles?: any[];