You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

151 lines
3.7 KiB

package main
import (
"encoding/json"
"fmt"
"git.fuyu.moe/Fuyu/flog"
"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 meikanList struct {
User string
Anime []meikanListItem
}
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.ErrorHandler = errorHandler
r.GET(`/static/*asset`, getStatic)
r.GET(`/`, home)
r.GET(`/compare`, 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.QueryParam(`name`)
//https://api.jikan.moe/v3/user/nekomata1037/animelist/all
//https://api.meikan.moe/v1/ids/anime
jikanList := jikanAPIGetUserAnimelist(user)
var list meikanList
list.User = user
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"
}
list.Anime = append(list.Anime, temp)
}
return c.Render(200, `list`, list)
}
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
}
func errorHandler(c *router.Context, v interface{}) {
flog.Critical(c.Request.Method, ` `, c.Request.URL.Path, `: `, v)
_ = c.StatusText(http.StatusBadRequest)
}