mal-importer/main.go

152 lines
3.7 KiB
Go
Raw Permalink Normal View History

2018-11-27 13:46:50 +01:00
package main
import (
"encoding/json"
"fmt"
2018-12-03 15:26:52 +01:00
"git.fuyu.moe/Fuyu/flog"
2018-11-27 13:46:50 +01:00
"git.fuyu.moe/Fuyu/router"
"net/http"
"strconv"
)
2018-12-03 12:29:17 +01:00
// nolint: errcheck
2018-11-27 13:46:50 +01:00
type jikanAPIUserList struct {
2018-12-03 12:29:17 +01:00
Anime []jikanAPIAnime `json:"anime"`
2018-11-27 13:46:50 +01:00
}
type jikanAPIAnime struct {
2018-11-28 17:00:45 +01:00
MeikanID int `json:"meikan_id"`
MyanimelistID int `json:"mal_id"`
2018-11-27 13:46:50 +01:00
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"`
2018-11-29 15:31:10 +01:00
AiringStatus int `json:"airing_status"`
2018-11-28 17:00:45 +01:00
}
type meikanListItem struct {
2018-11-29 15:31:10 +01:00
Anime meikanAnime `json:"anime"`
State string `json:"state"`
Episode int `json:"episode"`
Rating int `json:"rating"`
Hidden bool `json:"hidden"`
Recommended bool `json:"recommend"`
2018-11-28 17:00:45 +01:00
}
2018-12-03 15:26:52 +01:00
type meikanList struct {
User string
Anime []meikanListItem
}
2018-11-28 17:00:45 +01:00
type meikanAnime struct {
2018-11-29 15:31:10 +01:00
ID int `json:"id"`
Title string `json:"title"`
Type string `json:"type"`
Episodes int `json:"episodes"`
State string `json:"state"`
Rating string `json:"rating"`
2018-11-27 13:46:50 +01:00
}
type meikanAPIIDs struct {
MeikanID int `json:"meikan"`
MyanimelistID int `json:"myanimelist"`
AnidbID int `json:"anidb"`
}
2018-12-03 12:29:17 +01:00
func main() {
r := router.New()
r.Renderer = NewRenderer()
2018-12-03 20:18:28 +01:00
r.ErrorHandler = errorHandler
2018-12-03 12:29:17 +01:00
r.GET(`/static/*asset`, getStatic)
r.GET(`/`, home)
2018-12-03 15:26:52 +01:00
r.GET(`/compare`, compare)
2018-12-03 12:29:17 +01:00
err := r.Start(`:8070`)
check(err)
}
2018-11-27 13:46:50 +01:00
func check(e error) {
if e != nil {
panic(e)
}
}
func home(c *router.Context) error {
2018-12-03 12:29:17 +01:00
return c.Render(200, `index`, ``)
2018-11-27 13:46:50 +01:00
}
func compare(c *router.Context) error {
2018-12-03 15:26:52 +01:00
user := c.QueryParam(`name`)
2018-11-27 13:46:50 +01:00
//https://api.jikan.moe/v3/user/nekomata1037/animelist/all
//https://api.meikan.moe/v1/ids/anime
jikanList := jikanAPIGetUserAnimelist(user)
2018-12-03 15:26:52 +01:00
var list meikanList
list.User = user
2018-11-29 15:31:10 +01:00
fmt.Println("Getting IDs")
2018-11-27 13:46:50 +01:00
IDs := meikanAPIGetIDs()
for i := 0; i < len(jikanList.Anime); i++ {
2018-12-03 12:29:17 +01:00
jikanList.Anime[i].MeikanID = IDs[jikanList.Anime[i].MyanimelistID]
2018-11-28 17:00:45 +01:00
temp := meikanListItem{Anime: meikanAnime{ID: jikanList.Anime[i].MeikanID,
2018-11-29 15:31:10 +01:00
Title: jikanList.Anime[i].Title,
Type: jikanList.Anime[i].Type,
2018-11-28 17:00:45 +01:00
Episodes: jikanList.Anime[i].TotalEpisodes},
Episode: jikanList.Anime[i].WatchedEpisodes,
2018-11-29 15:31:10 +01:00
Rating: jikanList.Anime[i].Score}
2018-11-28 17:00:45 +01:00
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"
2018-11-27 13:46:50 +01:00
}
2018-12-03 20:18:28 +01:00
list.Anime = append(list.Anime, temp)
2018-11-27 13:46:50 +01:00
}
2018-12-03 12:29:17 +01:00
2018-12-03 15:26:52 +01:00
return c.Render(200, `list`, list)
2018-11-27 13:46:50 +01:00
}
func jikanAPIGetUserAnimelist(user string) jikanAPIUserList {
2018-12-03 12:29:17 +01:00
var userList jikanAPIUserList
2018-11-27 13:46:50 +01:00
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
2018-12-03 12:29:17 +01:00
_ = json.NewDecoder(resp.Body).Decode(&tempResult)
userList.Anime = append(userList.Anime, tempResult.Anime...)
2018-11-27 13:46:50 +01:00
if len(tempResult.Anime) == 0 {
break
}
}
2018-12-03 12:29:17 +01:00
return userList
2018-11-27 13:46:50 +01:00
}
2018-12-03 12:29:17 +01:00
func meikanAPIGetIDs() map[int]int {
2018-11-27 13:46:50 +01:00
resp, err := http.Get("https://api.meikan.moe/v1/ids/anime")
check(err)
var result []meikanAPIIDs
2018-12-03 12:29:17 +01:00
_ = json.NewDecoder(resp.Body).Decode(&result)
IDsMap := map[int]int{}
for _, id := range result {
IDsMap[id.MyanimelistID] = id.MeikanID
2018-11-29 15:31:10 +01:00
}
2018-12-03 12:29:17 +01:00
return IDsMap
2018-11-29 15:31:10 +01:00
}
2018-12-03 15:26:52 +01:00
func errorHandler(c *router.Context, v interface{}) {
flog.Critical(c.Request.Method, ` `, c.Request.URL.Path, `: `, v)
_ = c.StatusText(http.StatusBadRequest)
}