fix adult search

This commit is contained in:
janisslsm committed 2026-10-27 13:18:54 +02:00
1 parent bee6625a9e
commit bb010e7062
3 files changed
+82 -55

No files matched your search

+47 -30
View File
@@ -285,13 +285,20 @@ function getExternalIds(anime, key) {
.filter((id) => id != null);
}
async function searchCatalogAnime(title) {
// Miruro rejects search limits above 15 with a generic 400.
const response = await apiGet('anime', {
async function searchCatalogAnime(title, includeAdult = false) {
// Miruro rejects search limits above 15 with a generic 400. Its catalog
// also hides adult entries unless is_adult=true is requested explicitly.
const query = {
q: title,
limit: SEARCH_LIMIT,
sort: '-popularity'
});
};
if (includeAdult) {
query.is_adult = true;
}
const response = await apiGet('anime', query);
return responseItems(response);
}
@@ -327,38 +334,48 @@ function buildSearchTitles(targetMeta, mapping) {
.slice(0, MAX_SEARCH_QUERIES);
}
function pickCatalogMatch(results, candidateIds, targetId) {
if (results.length === 0) {
return null;
}
const exact = results.find((anime) => getExternalIds(anime, 'anilist').includes(Number(targetId)));
if (exact) {
return exact;
}
return results.find((anime) => matchesAnyAniListId(anime, candidateIds)) || null;
}
async function findCatalogAnime(candidateIds, targetId, searchTitles) {
for (const title of searchTitles) {
let results;
// The catalog excludes adult entries from a default search, so retry the
// same titles with is_adult=true when the normal pass found nothing.
for (const includeAdult of [false, true]) {
const titles = includeAdult ? searchTitles.slice(0, 3) : searchTitles;
try {
results = await searchCatalogAnime(title);
} catch (error) {
console.warn(`[Miruro] Catalog search "${title}" failed: ${error.message}`);
for (const title of titles) {
let results;
// A 403 means the catalog rejected the client itself (Cloudflare or
// an IP block); trying more title variants cannot help.
if (error.status === 403) {
break;
try {
results = await searchCatalogAnime(title, includeAdult);
} catch (error) {
console.warn(`[Miruro] Catalog search "${title}" failed: ${error.message}`);
// A 403 means the catalog rejected the client itself (Cloudflare
// or an IP block); trying more title variants cannot help.
if (error.status === 403) {
return null;
}
continue;
}
continue;
}
const match = pickCatalogMatch(results, candidateIds, targetId);
if (results.length === 0) {
continue;
}
const exact = results.find((anime) => getExternalIds(anime, 'anilist').includes(Number(targetId)));
if (exact) {
return exact;
}
const candidate = results.find((anime) => matchesAnyAniListId(anime, candidateIds));
if (candidate) {
return candidate;
if (match) {
return match;
}
}
}