204 lines
5.4 KiB
Go
204 lines
5.4 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"git.fuyu.moe/Fuyu/router"
|
|
"io/ioutil"
|
|
"mime"
|
|
"net/http"
|
|
"path"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func main() {
|
|
//fmt.Println("Hello world")
|
|
r := router.New()
|
|
r.GET(`/static/*asset`, getStatic)
|
|
r.GET(`/`, home)
|
|
r.GET(`/compare/:user`, compare)
|
|
err := r.Start(`:8070`)
|
|
check(err)
|
|
}
|
|
|
|
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 {
|
|
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 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 {
|
|
start := time.Now()
|
|
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(result)
|
|
fmt.Println("Getting IDs")
|
|
IDs := meikanAPIGetIDs()
|
|
confMap := map[int]int{}
|
|
for _, v := range IDs {
|
|
confMap[v.MyanimelistID] = v.MeikanID
|
|
}
|
|
fmt.Printf("35248 is %d \n", confMap[35248])
|
|
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].MyanimelistID == IDs[d].MyanimelistID {
|
|
jikanList.Anime[i].MeikanID = IDs[d].MeikanID
|
|
}
|
|
}
|
|
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:
|
|
jikanList.Completed++
|
|
temp.State = "Watching"
|
|
case 2:
|
|
jikanList.Completed++
|
|
temp.State = "Finished"
|
|
case 3:
|
|
jikanList.OnHold++
|
|
temp.State = "On Hold"
|
|
case 4:
|
|
jikanList.Dropped++
|
|
temp.State = "Dropped"
|
|
case 6:
|
|
jikanList.PlanToWatch++
|
|
temp.State = "Plan to watch"
|
|
}
|
|
switch jikanList.Anime[i].AiringStatus {
|
|
case 2:
|
|
temp.Anime.State = "Finished"
|
|
}
|
|
anime = append(anime, temp)
|
|
}
|
|
//fmt.Println(anime)
|
|
//resJSON, err := json.MarshalIndent(result, "", "\t")
|
|
//check(err)
|
|
//jsonfile := []byte(resJSON)
|
|
elapsed := time.Since(start)
|
|
fmt.Printf("Generating json took %s\n", elapsed)
|
|
return c.JSON(200, anime)
|
|
}
|
|
|
|
func jikanAPIGetUserAnimelist(user string) jikanAPIUserList {
|
|
var result jikanAPIUserList
|
|
start := time.Now()
|
|
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)
|
|
elapsed := time.Since(start)
|
|
fmt.Printf("Searching page 1 took %s\n", elapsed)
|
|
continue
|
|
} else {
|
|
_ = json.NewDecoder(bytes.NewReader(body)).Decode(&tempResult)
|
|
result.Anime = append(result.Anime, tempResult.Anime...)
|
|
elapsed := time.Since(start)
|
|
fmt.Printf("Searching page %d took %s\n", i, elapsed)
|
|
}
|
|
if len(tempResult.Anime) == 0 {
|
|
break
|
|
}
|
|
elapsed := time.Since(start)
|
|
fmt.Printf("Searching the userlist took %s\n", elapsed)
|
|
}
|
|
return result
|
|
}
|
|
|
|
func meikanAPIGetIDs() []meikanAPIIDs {
|
|
start := time.Now()
|
|
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)
|
|
elapsed := time.Since(start)
|
|
fmt.Printf("Getting all the IDs took %s\n", elapsed)
|
|
return result
|
|
}
|
|
|
|
func getStatic(c *router.Context) error {
|
|
path := path.Clean(strings.TrimPrefix(c.Request.URL.Path, `/static/`))
|
|
|
|
data, err := ioutil.ReadFile(`./static/` + path)
|
|
if err != nil {
|
|
return c.String(404, `Resource not found`)
|
|
}
|
|
|
|
c.Response.Header().Set(`Content-Type`, mime.TypeByExtension(filepath.Ext(path)))
|
|
c.Response.Write(data)
|
|
|
|
return nil
|
|
}
|