fix: use string matching to map to correct title in Aniwatch
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import { findBestMatchingTitle } from "~/libs/findBestMatchingTitle";
|
||||
|
||||
import { Episode, type EpisodesResponse } from "./episode";
|
||||
|
||||
export async function getEpisodesFromAniwatch(
|
||||
@@ -8,9 +10,12 @@ export async function getEpisodesFromAniwatch(
|
||||
.then(({ fetchTitleFromAnilist }) =>
|
||||
fetchTitleFromAnilist(aniListId, undefined),
|
||||
)
|
||||
.then((title) => title?.title?.english ?? title?.title?.userPreferred);
|
||||
.then((title) => ({
|
||||
english: title?.title?.english,
|
||||
userPreferred: title?.title?.userPreferred,
|
||||
}));
|
||||
|
||||
if (!animeTitle) {
|
||||
if (!animeTitle.english && !animeTitle.userPreferred) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -55,12 +60,23 @@ export async function getEpisodesFromAniwatch(
|
||||
return null;
|
||||
}
|
||||
|
||||
function getAniwatchId(animeTitle: string): Promise<string | undefined> {
|
||||
function getAniwatchId(
|
||||
animeTitle: Partial<{ english: string; userPreferred: string }>,
|
||||
): Promise<string | undefined> {
|
||||
return fetch(
|
||||
`https://aniwatch.up.railway.app/anime/search?q=${encodeURIComponent(animeTitle)}`,
|
||||
`https://aniwatch.up.railway.app/anime/search?q=${encodeURIComponent(animeTitle.english ?? animeTitle.userPreferred!)}`,
|
||||
)
|
||||
.then((res) => res.json<AniwatchSearchResponse>())
|
||||
.then(({ animes }) => animes[0]?.id);
|
||||
.then(({ animes }) => {
|
||||
const bestMatchingTitle = findBestMatchingTitle(
|
||||
animeTitle,
|
||||
animes.map((anime) => ({
|
||||
english: anime.name,
|
||||
userPreferred: anime.jname,
|
||||
})),
|
||||
);
|
||||
return animes.find((anime) => anime.name === bestMatchingTitle)?.id;
|
||||
});
|
||||
}
|
||||
|
||||
export interface AniwatchEpisodesResponse {
|
||||
|
||||
Reference in New Issue
Block a user