feat: improve title searching algorithm for aniwatch
This commit is contained in:
@@ -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!)}`,
|
||||||
|
),
|
||||||
|
])
|
||||||
|
.then((responses) => {
|
||||||
|
return responses.reduce(
|
||||||
|
async (current, res) => {
|
||||||
|
if (res.status === "rejected") {
|
||||||
|
return current;
|
||||||
|
}
|
||||||
|
|
||||||
|
const json = (await res.value.json()) as {
|
||||||
success: boolean;
|
success: boolean;
|
||||||
data: AniwatchSearchResponse;
|
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;
|
||||||
|
|||||||
@@ -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),
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user