feat: improve title searching algorithm for aniwatch

This commit is contained in:
2024-10-28 05:08:15 -04:00
parent c2411975d5
commit 9fafa5b45d
2 changed files with 41 additions and 13 deletions

View File

@@ -122,16 +122,44 @@ async function fetchEpisodes(
function getAniwatchId(
animeTitle: Partial<{ english: string; userPreferred: string }>,
): Promise<string | undefined> {
return fetch(
`https://aniwatch.up.railway.app/api/v2/hianime/search?q=${encodeURIComponent(animeTitle.english ?? animeTitle.userPreferred!)}`,
)
.then(
(res) =>
res.json() as Promise<{
success: boolean;
data: AniwatchSearchResponse;
}>,
)
return Promise.allSettled([
fetch(
`https://aniwatch.up.railway.app/api/v2/hianime/search?q=${encodeURIComponent(animeTitle.english!)}`,
),
fetch(
`https://aniwatch.up.railway.app/api/v2/hianime/search?q=${encodeURIComponent(animeTitle.userPreferred!)}`,
),
])
.then((responses) => {
return responses.reduce(
async (current, res) => {
if (res.status === "rejected") {
return current;
}
const json = (await res.value.json()) as {
success: boolean;
data: AniwatchSearchResponse;
};
const currentValue = await current;
console.log(currentValue);
return {
success: currentValue.success || json.success,
data: {
...currentValue.data,
animes: [
...currentValue.data.animes,
...(json.data?.animes ?? []),
],
},
};
},
Promise.resolve({
success: false,
data: { animes: [] },
}),
);
})
.then(({ success, data: { animes } }) => {
if (!success) {
return;