feat: improve title searching algorithm for aniwatch
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user