taevas.xyz/netlify/functions/anilist.ts

82 lines
2.1 KiB
TypeScript
Raw Normal View History

2023-05-09 14:14:28 +02:00
import { Handler } from '@netlify/functions'
import fetch from "node-fetch"
2023-05-29 14:47:53 +02:00
import { AnilistInfo } from '../../src/components/infos/Anilist'
2023-05-09 14:14:28 +02:00
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, sort: UPDATED_TIME_DESC) {
media {
title {
romaji
}
episodes
coverImage {
medium
}
2023-05-14 14:26:27 +02:00
siteUrl
2023-05-09 14:14:28 +02:00
}
progress
score (format: POINT_10)
startedAt {
day
month
year
}
updatedAt
completedAt {
day
month
year
}
}
}
`,
variables: {
userName: "Taevas"
}
})
})
if (anilist.status !== 200) {
return {
statusCode: 404,
body: ""
}
}
let p_json = await anilist.json() as {[key: string]: any}
let json = p_json.data.MediaList
2023-05-10 19:55:11 +02:00
let anime: AnilistInfo = {
2023-05-09 14:14:28 +02:00
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(),
2023-05-14 14:26:27 +02:00
cover: json.media.coverImage.medium,
url: json.media.siteUrl
2023-05-09 14:14:28 +02:00
}
2023-05-14 14:26:27 +02:00
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"))
2023-05-09 14:14:28 +02:00
return {
statusCode: 200,
body: JSON.stringify(anime)
}
}
export { handler }