This commit is contained in:
David Alasow 2018-11-27 13:46:50 +01:00
commit cb06b74d17

127
main.go Normal file
View File

@ -0,0 +1,127 @@
package main
import (
"bytes"
"encoding/json"
"fmt"
"git.fuyu.moe/Fuyu/router"
"io/ioutil"
"net/http"
"strconv"
)
func main() {
//fmt.Println("Hello world")
r := router.New()
r.GET(`/`, home)
r.GET(`/compare/:user`, compare)
r.Start(`:8080`)
}
type jikanAPIUserList struct {
RequestHash string `json:"request_hash"`
RequestCached bool `json:"request_cached"`
RequestCacheExpiry int `json:"request_cache_expiry"`
Completed int `json:"completed"`
Watching int `json:"watching"`
Dropped int `json:"dropped"`
PlanToWatch int `json:"plan_to_watch"`
OnHold int `json:"on_hold"`
Anime []jikanAPIAnime `json:"anime"`
}
type jikanAPIAnime struct {
MalID 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"`
MeikanID int `json:"meikan_id"`
}
type meikanAPIIDs struct {
MeikanID int `json:"meikan"`
MyanimelistID int `json:"myanimelist"`
AnidbID int `json:"anidb"`
}
func check(e error) {
if e != nil {
panic(e)
}
}
func home(c *router.Context) error {
return c.String(200, "Test")
}
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)
//fmt.Println(result)
IDs := meikanAPIGetIDs()
for i := 0; i < len(jikanList.Anime); i++ {
//fmt.Println(result.Anime[i].Title)
for d := 0; d < len(IDs); d++ {
if jikanList.Anime[i].MalID == IDs[d].MyanimelistID {
jikanList.Anime[i].MeikanID = IDs[d].MeikanID
}
}
if jikanList.Anime[i].WatchingStatus == 1 {
jikanList.Watching++
} else if jikanList.Anime[i].WatchingStatus == 2 {
jikanList.Completed++
} else if jikanList.Anime[i].WatchingStatus == 3 {
jikanList.OnHold++
} else if jikanList.Anime[i].WatchingStatus == 4 {
jikanList.Dropped++
} else if jikanList.Anime[i].WatchingStatus == 6 {
jikanList.PlanToWatch++
}
}
//resJSON, err := json.MarshalIndent(result, "", "\t")
//check(err)
//jsonfile := []byte(resJSON)
return c.JSON(200, jikanList)
}
func jikanAPIGetUserAnimelist(user string) jikanAPIUserList {
var result jikanAPIUserList
for i := 1; ; i++ {
resp, err := http.Get("https://api.jikan.moe/v3/user/" + user + "/animelist/all/" + strconv.Itoa(i))
check(err)
defer resp.Body.Close()
var tempResult jikanAPIUserList
body, err := ioutil.ReadAll(resp.Body)
check(err)
if i == 1 {
_ = json.NewDecoder(bytes.NewReader(body)).Decode(&result)
fmt.Println("Searching page 1")
continue
} else {
_ = json.NewDecoder(bytes.NewReader(body)).Decode(&tempResult)
result.Anime = append(result.Anime, tempResult.Anime...)
fmt.Println("Searching page " + strconv.Itoa(i))
}
if len(tempResult.Anime) == 0 {
break
}
}
return result
}
func meikanAPIGetIDs() []meikanAPIIDs {
resp, err := http.Get("https://api.meikan.moe/v1/ids/anime")
check(err)
defer resp.Body.Close()
var result []meikanAPIIDs
body, err := ioutil.ReadAll(resp.Body)
check(err)
_ = json.NewDecoder(bytes.NewReader(body)).Decode(&result)
return result
}