fix adult search
This commit is contained in:
1 parent
bee6625a9e
commit
bb010e7062
3 files changed
+82
-55
No files matched your search
+33
-23
@@ -714,12 +714,16 @@ function responseItems(response) {
|
||||
function getExternalIds(anime, key) {
|
||||
return asArray(anime && anime.external_ids ? anime.external_ids[key] : null).map((id) => asNumber(id)).filter((id) => id != null);
|
||||
}
|
||||
async function searchCatalogAnime(title) {
|
||||
const response = await apiGet("anime", {
|
||||
async function searchCatalogAnime(title, includeAdult = false) {
|
||||
const query = {
|
||||
q: title,
|
||||
limit: SEARCH_LIMIT,
|
||||
sort: "-popularity"
|
||||
});
|
||||
};
|
||||
if (includeAdult) {
|
||||
query.is_adult = true;
|
||||
}
|
||||
const response = await apiGet("anime", query);
|
||||
return responseItems(response);
|
||||
}
|
||||
function matchesAnyAniListId(anime, aniListIds) {
|
||||
@@ -744,28 +748,34 @@ function buildSearchTitles(targetMeta, mapping) {
|
||||
return title;
|
||||
}).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;
|
||||
try {
|
||||
results = await searchCatalogAnime(title);
|
||||
} catch (error) {
|
||||
console.warn(`[Miruro] Catalog search "${title}" failed: ${error.message}`);
|
||||
if (error.status === 403) {
|
||||
break;
|
||||
for (const includeAdult of [false, true]) {
|
||||
const titles = includeAdult ? searchTitles.slice(0, 3) : searchTitles;
|
||||
for (const title of titles) {
|
||||
let results;
|
||||
try {
|
||||
results = await searchCatalogAnime(title, includeAdult);
|
||||
} catch (error) {
|
||||
console.warn(`[Miruro] Catalog search "${title}" failed: ${error.message}`);
|
||||
if (error.status === 403) {
|
||||
return null;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
const match = pickCatalogMatch(results, candidateIds, targetId);
|
||||
if (match) {
|
||||
return match;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
||||
Reference in new issue
Block a user