mal-importer/main.go
2018-12-03 12:29:17 +01:00

138 lines
3.4 KiB
Go

package main
import (
"encoding/json"
"fmt"
"git.fuyu.moe/Fuyu/router"
"net/http"
"strconv"
)
// nolint: errcheck
type jikanAPIUserList struct {
Anime []jikanAPIAnime `json:"anime"`
}
type jikanAPIAnime struct {
MeikanID int `json:"meikan_id"`
MyanimelistID int `json:"mal_id"`
Title string `json:"title"`
ImageURL string `json:"image_url"`
Type string `json:"type"`
WatchingStatus int `json:"watching_status"`
Score int `json:"score"`
WatchedEpisodes int `json:"watched_episodes"`
TotalEpisodes int `json:"total_episodes"`
AiringStatus int `json:"airing_status"`
}
type meikanListItem struct {
Anime meikanAnime `json:"anime"`
State string `json:"state"`
Episode int `json:"episode"`
Rating int `json:"rating"`
Hidden bool `json:"hidden"`
Recommended bool `json:"recommend"`
}
type meikanAnime struct {
ID int `json:"id"`
Title string `json:"title"`
Type string `json:"type"`
Episodes int `json:"episodes"`
State string `json:"state"`
Rating string `json:"rating"`
}
type meikanAPIIDs struct {
MeikanID int `json:"meikan"`
MyanimelistID int `json:"myanimelist"`
AnidbID int `json:"anidb"`
}
func main() {
r := router.New()
r.Renderer = NewRenderer()
r.GET(`/static/*asset`, getStatic)
r.GET(`/`, home)
r.GET(`/compare/:user`, compare)
err := r.Start(`:8070`)
check(err)
}
func check(e error) {
if e != nil {
panic(e)
}
}
func home(c *router.Context) error {
return c.Render(200, `index`, ``)
}
func compare(c *router.Context) error {
user := c.Param(`user`)
//https://api.jikan.moe/v3/user/nekomata1037/animelist/all
//https://api.meikan.moe/v1/ids/anime
jikanList := jikanAPIGetUserAnimelist(user)
var anime []meikanListItem
fmt.Println("Getting IDs")
IDs := meikanAPIGetIDs()
for i := 0; i < len(jikanList.Anime); i++ {
jikanList.Anime[i].MeikanID = IDs[jikanList.Anime[i].MyanimelistID]
temp := meikanListItem{Anime: meikanAnime{ID: jikanList.Anime[i].MeikanID,
Title: jikanList.Anime[i].Title,
Type: jikanList.Anime[i].Type,
Episodes: jikanList.Anime[i].TotalEpisodes},
Episode: jikanList.Anime[i].WatchedEpisodes,
Rating: jikanList.Anime[i].Score}
switch jikanList.Anime[i].WatchingStatus {
case 1:
temp.State = "Watching"
case 2:
temp.State = "Finished"
case 3:
temp.State = "On Hold"
case 4:
temp.State = "Dropped"
case 6:
temp.State = "Plan to watch"
}
switch jikanList.Anime[i].AiringStatus {
case 2:
temp.Anime.State = "Finished"
}
anime = append(anime, temp)
}
return c.JSON(200, anime)
}
func jikanAPIGetUserAnimelist(user string) jikanAPIUserList {
var userList jikanAPIUserList
for i := 1; ; i++ {
resp, err := http.Get("https://api.jikan.moe/v3/user/" + user + "/animelist/all/" + strconv.Itoa(i))
check(err)
var tempResult jikanAPIUserList
_ = json.NewDecoder(resp.Body).Decode(&tempResult)
userList.Anime = append(userList.Anime, tempResult.Anime...)
if len(tempResult.Anime) == 0 {
break
}
}
return userList
}
func meikanAPIGetIDs() map[int]int {
resp, err := http.Get("https://api.meikan.moe/v1/ids/anime")
check(err)
var result []meikanAPIIDs
_ = json.NewDecoder(resp.Body).Decode(&result)
IDsMap := map[int]int{}
for _, id := range result {
IDsMap[id.MyanimelistID] = id.MeikanID
}
return IDsMap
}