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( function getAniwatchId(
animeTitle: Partial<{ english: string; userPreferred: string }>, animeTitle: Partial<{ english: string; userPreferred: string }>,
): Promise<string | undefined> { ): Promise<string | undefined> {
return fetch( return Promise.allSettled([
`https://aniwatch.up.railway.app/api/v2/hianime/search?q=${encodeURIComponent(animeTitle.english ?? animeTitle.userPreferred!)}`, fetch(
) `https://aniwatch.up.railway.app/api/v2/hianime/search?q=${encodeURIComponent(animeTitle.english!)}`,
.then( ),
(res) => fetch(
res.json() as Promise<{ `https://aniwatch.up.railway.app/api/v2/hianime/search?q=${encodeURIComponent(animeTitle.userPreferred!)}`,
success: boolean; ),
data: AniwatchSearchResponse; ])
}>, .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 } }) => { .then(({ success, data: { animes } }) => {
if (!success) { if (!success) {
return; return;

View File

@@ -22,14 +22,14 @@ type Title = {
export const findBestMatchingTitle = ( export const findBestMatchingTitle = (
title: Partial<Title>, title: Partial<Title>,
titles: Partial<Title>[], titlesToSearch: Partial<Title>[],
): string | null => { ): string | null => {
const { english, userPreferred } = title; const { english, userPreferred } = title;
const userPreferredBestMatch = userPreferred const userPreferredBestMatch = userPreferred
? findBestMatch( ? findBestMatch(
userPreferred, userPreferred,
titles titlesToSearch
.map((title) => title.userPreferred) .map((title) => title.userPreferred)
.filter((title) => title !== undefined), .filter((title) => title !== undefined),
) )
@@ -37,7 +37,7 @@ export const findBestMatchingTitle = (
const englishBestMatch = english const englishBestMatch = english
? findBestMatch( ? findBestMatch(
english, english,
titles titlesToSearch
.map((title) => title.english) .map((title) => title.english)
.filter((title) => title !== undefined), .filter((title) => title !== undefined),
) )