Files
aniplay-api/src/controllers/title/amvstrm.ts

63 lines
1.9 KiB
TypeScript

import { Title } from "~/types/title";
export async function fetchTitleFromAmvstrm(
aniListId: number,
): Promise<Title | undefined> {
return Promise.all([
fetch(`https://api-amvstrm.nyt92.eu.org/api/v2/info/${aniListId}`).then(
(res) => res.json<any>(),
),
fetchMissingInformationFromAnify(aniListId).catch((err) => {
console.error("Failed to get missing information from Anify", err);
return null;
}),
]).then(async ([amvstrmInfo, anifyInfo]) => {
if (amvstrmInfo.code >= 400) {
console.error(
`Error trying to load title from amvstrm; aniListId: ${aniListId}, code: ${amvstrmInfo.code}, message: ${amvstrmInfo.message}`,
);
return undefined;
}
return {
id: amvstrmInfo.id,
idMal: amvstrmInfo.idMal,
title: {
userPreferred: amvstrmInfo.title.userPreferred,
english: amvstrmInfo.title.english,
},
description: amvstrmInfo.description,
episodes: amvstrmInfo.episodes,
genres: amvstrmInfo.genres,
status: amvstrmInfo.status,
averageScore: amvstrmInfo.score.averageScore,
bannerImage: amvstrmInfo.bannerImage ?? anifyInfo?.bannerImage,
coverImage: {
extraLarge: amvstrmInfo.coverImage.extraLarge,
large: amvstrmInfo.coverImage.large,
medium: amvstrmInfo.coverImage.medium,
},
countryOfOrigin:
amvstrmInfo.countryOfOrigin ?? anifyInfo?.countryOfOrigin,
nextAiringEpisode: amvstrmInfo.nextair,
mediaListEntry: null,
};
});
}
type AnifyInformation = {
bannerImage: string | null;
countryOfOrigin: string;
};
function fetchMissingInformationFromAnify(
aniListId: number,
): Promise<AnifyInformation> {
return fetch(`https://api.anify.tv/info?id=${aniListId}`)
.then((res) => res.json() as Promise<AnifyInformation>)
.then(({ bannerImage, countryOfOrigin }) => ({
bannerImage,
countryOfOrigin,
}));
}