manual matching survives ttl now

This commit is contained in:
2026-01-02 17:54:55 +01:00
parent 59fdadd288
commit 5c4f4cf3b6
4 changed files with 176 additions and 38 deletions

View File

@@ -361,16 +361,37 @@ export async function searchEpisodesInExtension(ext: Extension | null, name: str
if (cached) {
const isExpired = Date.now() - cached.created_at > CACHE_TTL_MS;
try {
const parsed = JSON.parse(cached.result) as {
mediaId?: string;
episodes: Episode[];
};
if (!isExpired) {
console.log(`[${name}] Episodes cache hit for: ${query}`);
try {
return JSON.parse(cached.result) as Episode[];
return parsed.episodes;
}
// Si el caché expiró pero tenemos mediaId, refrescamos directamente
if (parsed.mediaId && ext.type === "anime-board" && ext.search && typeof ext.findEpisodes === "function") {
console.log(`[${name}] Episodes cache expired but mediaId found, refreshing...`);
const chapterList = await ext.findEpisodes(parsed.mediaId);
if (!Array.isArray(chapterList)) return [];
const result: Episode[] = chapterList.map(ep => ({
id: ep.id,
number: ep.number,
url: ep.url,
title: ep.title
}));
await setCache(cacheKey, { mediaId: parsed.mediaId, episodes: result }, CACHE_TTL_MS);
return result;
}
} catch (e) {
console.error(`[${name}] Error parsing cached episodes:`, e);
}
} else {
console.log(`[${name}] Episodes cache expired for: ${query}`);
}
}
if (ext.type === "anime-board" && ext.search && typeof ext.findEpisodes === "function") {
@@ -422,6 +443,7 @@ export async function searchEpisodesInExtension(ext: Extension | null, name: str
const chapterList = await ext.findEpisodes(mediaId);
if (!Array.isArray(chapterList)) return [];
const result: Episode[] = chapterList.map(ep => ({
id: ep.id,
number: ep.number,
@@ -429,7 +451,8 @@ export async function searchEpisodesInExtension(ext: Extension | null, name: str
title: ep.title
}));
await setCache(cacheKey, result, CACHE_TTL_MS);
// Cachear tanto el mediaId como los episodios
await setCache(cacheKey, { mediaId, episodes: result }, CACHE_TTL_MS);
return result;
} catch (e) {
@@ -465,15 +488,36 @@ export async function getStreamData(extension: Extension, episode: string, id: s
throw new Error("Extension doesn't support required methods");
}
let episodes;
let animeTitle: string | undefined;
if (source === "anilist" && !extensionAnimeId) {
const anime: any = await getAnimeById(id);
episodes = await searchEpisodesInExtension(extension, extension.constructor.name, anime.title.romaji);
} else {
episodes = await extension.findEpisodes(extensionAnimeId ?? id);
const targetId = extensionAnimeId ?? id;
episodes = await extension.findEpisodes(targetId);
if (extensionAnimeId) {
const anime: any = await getAnimeById(id);
animeTitle = anime.title.romaji;
const episodesCacheKey = `anime:episodes:${providerName}:${animeTitle}`;
const episodesResult: Episode[] = episodes.map((ep: any) => ({
id: ep.id,
number: ep.number,
url: ep.url,
title: ep.title
}));
await setCache(episodesCacheKey, {
mediaId: extensionAnimeId,
episodes: episodesResult
}, CACHE_TTL_MS);
}
}
const targetEp = episodes.find(e => e.number === parseInt(episode));
const targetEp = episodes.find((e: any) => e.number === parseInt(episode));
if (!targetEp) {
throw new Error("Episode not found");

View File

@@ -363,23 +363,45 @@ async function fetchBookMetadata(id: string): Promise<Book | null> {
}
async function searchChaptersInExtension(ext: Extension, name: string, lookupId: string, cacheId: string, search: boolean, origin: string, disableCache = false): Promise<ChapterWithProvider[]> {
const cacheKey = `chapters:${name}:${origin}:${search ? "search" : "id"}:${cacheId}`;
const cacheKey = `chapters:${name}:${origin}:id:${cacheId}`;
if (!disableCache) {
const cached = await getCache(cacheKey);
if (cached) {
const isExpired = Date.now() - cached.created_at > CACHE_TTL_MS;
try {
const parsed = JSON.parse(cached.result) as {
mediaId?: string;
chapters: ChapterWithProvider[];
};
if (!isExpired) {
console.log(`[${name}] Chapters cache hit for: ${lookupId}`);
try {
return JSON.parse(cached.result) as ChapterWithProvider[];
return parsed.chapters;
}
if (parsed.mediaId) {
const chaps = await ext.findChapters!(parsed.mediaId);
const result = chaps.map(ch => ({
id: ch.id,
number: parseFloat(ch.number.toString()),
title: ch.title,
date: ch.releaseDate,
provider: name,
index: ch.index,
language: ch.language ?? null,
}));
await setCache(cacheKey, { mediaId: parsed.mediaId, chapters: result }, CACHE_TTL_MS);
return result;
}
} catch (e) {
console.error(`[${name}] Error parsing cached chapters:`, e);
}
}
}
}
try {
console.log(`[${name}] Searching chapters for: ${lookupId}`);
@@ -436,7 +458,10 @@ async function searchChaptersInExtension(ext: Extension, name: string, lookupId:
language: ch.language ?? null,
}));
await setCache(cacheKey, result, CACHE_TTL_MS);
await setCache(cacheKey, {
mediaId,
chapters: result
}, CACHE_TTL_MS);
return result;
} catch (e) {

View File

@@ -361,16 +361,37 @@ export async function searchEpisodesInExtension(ext: Extension | null, name: str
if (cached) {
const isExpired = Date.now() - cached.created_at > CACHE_TTL_MS;
try {
const parsed = JSON.parse(cached.result) as {
mediaId?: string;
episodes: Episode[];
};
if (!isExpired) {
console.log(`[${name}] Episodes cache hit for: ${query}`);
try {
return JSON.parse(cached.result) as Episode[];
return parsed.episodes;
}
// Si el caché expiró pero tenemos mediaId, refrescamos directamente
if (parsed.mediaId && ext.type === "anime-board" && ext.search && typeof ext.findEpisodes === "function") {
console.log(`[${name}] Episodes cache expired but mediaId found, refreshing...`);
const chapterList = await ext.findEpisodes(parsed.mediaId);
if (!Array.isArray(chapterList)) return [];
const result: Episode[] = chapterList.map(ep => ({
id: ep.id,
number: ep.number,
url: ep.url,
title: ep.title
}));
await setCache(cacheKey, { mediaId: parsed.mediaId, episodes: result }, CACHE_TTL_MS);
return result;
}
} catch (e) {
console.error(`[${name}] Error parsing cached episodes:`, e);
}
} else {
console.log(`[${name}] Episodes cache expired for: ${query}`);
}
}
if (ext.type === "anime-board" && ext.search && typeof ext.findEpisodes === "function") {
@@ -422,6 +443,7 @@ export async function searchEpisodesInExtension(ext: Extension | null, name: str
const chapterList = await ext.findEpisodes(mediaId);
if (!Array.isArray(chapterList)) return [];
const result: Episode[] = chapterList.map(ep => ({
id: ep.id,
number: ep.number,
@@ -429,7 +451,8 @@ export async function searchEpisodesInExtension(ext: Extension | null, name: str
title: ep.title
}));
await setCache(cacheKey, result, CACHE_TTL_MS);
// Cachear tanto el mediaId como los episodios
await setCache(cacheKey, { mediaId, episodes: result }, CACHE_TTL_MS);
return result;
} catch (e) {
@@ -465,15 +488,36 @@ export async function getStreamData(extension: Extension, episode: string, id: s
throw new Error("Extension doesn't support required methods");
}
let episodes;
let animeTitle: string | undefined;
if (source === "anilist" && !extensionAnimeId) {
const anime: any = await getAnimeById(id);
episodes = await searchEpisodesInExtension(extension, extension.constructor.name, anime.title.romaji);
} else {
episodes = await extension.findEpisodes(extensionAnimeId ?? id);
const targetId = extensionAnimeId ?? id;
episodes = await extension.findEpisodes(targetId);
if (extensionAnimeId) {
const anime: any = await getAnimeById(id);
animeTitle = anime.title.romaji;
const episodesCacheKey = `anime:episodes:${providerName}:${animeTitle}`;
const episodesResult: Episode[] = episodes.map((ep: any) => ({
id: ep.id,
number: ep.number,
url: ep.url,
title: ep.title
}));
await setCache(episodesCacheKey, {
mediaId: extensionAnimeId,
episodes: episodesResult
}, CACHE_TTL_MS);
}
}
const targetEp = episodes.find(e => e.number === parseInt(episode));
const targetEp = episodes.find((e: any) => e.number === parseInt(episode));
if (!targetEp) {
throw new Error("Episode not found");

View File

@@ -363,23 +363,45 @@ async function fetchBookMetadata(id: string): Promise<Book | null> {
}
async function searchChaptersInExtension(ext: Extension, name: string, lookupId: string, cacheId: string, search: boolean, origin: string, disableCache = false): Promise<ChapterWithProvider[]> {
const cacheKey = `chapters:${name}:${origin}:${search ? "search" : "id"}:${cacheId}`;
const cacheKey = `chapters:${name}:${origin}:id:${cacheId}`;
if (!disableCache) {
const cached = await getCache(cacheKey);
if (cached) {
const isExpired = Date.now() - cached.created_at > CACHE_TTL_MS;
try {
const parsed = JSON.parse(cached.result) as {
mediaId?: string;
chapters: ChapterWithProvider[];
};
if (!isExpired) {
console.log(`[${name}] Chapters cache hit for: ${lookupId}`);
try {
return JSON.parse(cached.result) as ChapterWithProvider[];
return parsed.chapters;
}
if (parsed.mediaId) {
const chaps = await ext.findChapters!(parsed.mediaId);
const result = chaps.map(ch => ({
id: ch.id,
number: parseFloat(ch.number.toString()),
title: ch.title,
date: ch.releaseDate,
provider: name,
index: ch.index,
language: ch.language ?? null,
}));
await setCache(cacheKey, { mediaId: parsed.mediaId, chapters: result }, CACHE_TTL_MS);
return result;
}
} catch (e) {
console.error(`[${name}] Error parsing cached chapters:`, e);
}
}
}
}
try {
console.log(`[${name}] Searching chapters for: ${lookupId}`);
@@ -436,7 +458,10 @@ async function searchChaptersInExtension(ext: Extension, name: string, lookupId:
language: ch.language ?? null,
}));
await setCache(cacheKey, result, CACHE_TTL_MS);
await setCache(cacheKey, {
mediaId,
chapters: result
}, CACHE_TTL_MS);
return result;
} catch (e) {