taevas.xyz/netlify/functions/anilist.ts
Taevas 121fcb02cc Fix the inconsistent indentation
The code was actually a mess regarding the indentation!
So now it uses 2 spaces

Also, there was an issue with the Anilist information
Some MediaLists can have the properties of startedAt set to null
So we filter out such MediaLists
Guess it only applies to imported animes, so it shouldn't matter much
2023-10-30 00:52:36 +01:00

82 lines
2.2 KiB
TypeScript

import { Handler } from '@netlify/functions'
import fetch from "node-fetch"
import { AnilistInfo } from '../../src/components/infos/Anilist'
const handler: Handler = async (event, context) => {
let anilist = await fetch("https://graphql.anilist.co", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Accept": "application/json"
},
body: JSON.stringify({
query: `
query ($userName: String) {
MediaList (userName: $userName, type: ANIME, startedAt_greater: 1, sort: UPDATED_TIME_DESC) {
media {
title {
romaji
}
episodes
coverImage {
medium
}
siteUrl
}
progress
score (format: POINT_10)
startedAt {
day
month
year
}
updatedAt
completedAt {
day
month
year
}
}
}
`,
variables: {
userName: "Taevas"
}
})
})
if (anilist.status !== 200) {
// log the issue in netlify, return 404 to the user anyway
console.log(await anilist.json())
return {
statusCode: 404,
body: ""
}
}
let p_json = await anilist.json() as {[key: string]: any}
let json = p_json.data.MediaList
let anime: AnilistInfo = {
title: json.media.title.romaji,
episodes: {
watched: json.progress,
total: json.media.episodes,
},
score: json.score,
startDate: new Date(`${json.startedAt.year}-${json.startedAt.month}-${json.startedAt.day}`).toISOString(),
updateDate: new Date(json.updatedAt * 1000).toISOString(),
endDate: new Date(`${json.completedAt.year}-${json.completedAt.month}-${json.completedAt.day}`).toISOString(),
cover: json.media.coverImage.medium,
url: json.media.siteUrl
}
anime.startDate = anime.startDate.substring(0, anime.startDate.indexOf("T"))
anime.updateDate = anime.updateDate.substring(0, anime.updateDate.indexOf("T"))
anime.endDate = anime.endDate.substring(0, anime.endDate.indexOf("T"))
return {
statusCode: 200,
body: JSON.stringify(anime)
}
}
export { handler }