Update animelib/animelib.js

This commit is contained in:
aka paul
2026-02-04 18:50:33 +00:00
parent 34dc12f3c7
commit 879a962872
+26 -5
View File
@@ -1,21 +1,32 @@
async function searchResults(keyword) { async function searchResults(keyword) {
const results = []; const results = [];
try { try {
const response = await fetchv2("https://hapi.hentaicdn.org/api/anime?fields[]=rate_avg&fields[]=rate&fields[]=releaseDate&q=" + keyword); const response = await fetchv2(
"https://hapi.hentaicdn.org/api/anime?fields[]=rate_avg&fields[]=rate&fields[]=releaseDate&q=" + keyword
);
const json = await response.json(); const json = await response.json();
if (json && Array.isArray(json.data)) { if (json && Array.isArray(json.data)) {
for (const item of json.data) { for (const item of json.data) {
const title = item.eng_name || item.name || "Unknown";
const image = item.cover?.default || item.cover?.thumbnail || ""; const image = item.cover?.default || item.cover?.thumbnail || "";
results.push({ results.push({
title: item.eng_name || item.name || "Unknown", title,
image: "https://passthrough-worker.simplepostrequest.workers.dev/?simple=" + image + "&referer=https://animelib.org/", image: "https://passthrough-worker.simplepostrequest.workers.dev/?simple=" +
href: item.slug_url || item.slug || item.id image +
"&referer=https://animelib.org/",
href: item.slug_url || item.slug || item.id,
_score: scoreTitle(title, keyword)
}); });
} }
} }
return JSON.stringify(results); results.sort((a, b) => a._score - b._score);
return JSON.stringify(
results.map(({ _score, ...rest }) => rest)
);
} catch (err) { } catch (err) {
return JSON.stringify([{ return JSON.stringify([{
title: err.message, title: err.message,
@@ -25,6 +36,16 @@ async function searchResults(keyword) {
} }
} }
function scoreTitle(title, keyword) {
const t = title.toLowerCase();
const k = keyword.toLowerCase();
if (t === k) return 0;
if (t.startsWith(k)) return 1;
if (t.includes(k)) return 2;
return 3;
}
async function extractDetails(slug) { async function extractDetails(slug) {
try { try {
const response = await fetchv2( const response = await fetchv2(