fixed performance
This commit is contained in:
parent
a53fff7391
commit
e077d0484b
78
main.go
78
main.go
@ -6,16 +6,23 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"git.fuyu.moe/Fuyu/router"
|
"git.fuyu.moe/Fuyu/router"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"mime"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"path"
|
||||||
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
//fmt.Println("Hello world")
|
//fmt.Println("Hello world")
|
||||||
r := router.New()
|
r := router.New()
|
||||||
|
r.GET(`/static/*asset`, getStatic)
|
||||||
r.GET(`/`, home)
|
r.GET(`/`, home)
|
||||||
r.GET(`/compare/:user`, compare)
|
r.GET(`/compare/:user`, compare)
|
||||||
r.Start(`:8070`)
|
err := r.Start(`:8070`)
|
||||||
|
check(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
type jikanAPIUserList struct {
|
type jikanAPIUserList struct {
|
||||||
@ -40,25 +47,25 @@ type jikanAPIAnime struct {
|
|||||||
Score int `json:"score"`
|
Score int `json:"score"`
|
||||||
WatchedEpisodes int `json:"watched_episodes"`
|
WatchedEpisodes int `json:"watched_episodes"`
|
||||||
TotalEpisodes int `json:"total_episodes"`
|
TotalEpisodes int `json:"total_episodes"`
|
||||||
AiringStatus int `json:"airing_status"`
|
AiringStatus int `json:"airing_status"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type meikanListItem struct {
|
type meikanListItem struct {
|
||||||
Anime meikanAnime `json:"anime"`
|
Anime meikanAnime `json:"anime"`
|
||||||
State string `json:"state"`
|
State string `json:"state"`
|
||||||
Episode int `json:"episode"`
|
Episode int `json:"episode"`
|
||||||
Rating int `json:"rating"`
|
Rating int `json:"rating"`
|
||||||
Hidden bool `json:"hidden"`
|
Hidden bool `json:"hidden"`
|
||||||
Recommended bool `json:"recommend"`
|
Recommended bool `json:"recommend"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type meikanAnime struct {
|
type meikanAnime struct {
|
||||||
ID int `json:"id"`
|
ID int `json:"id"`
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
Episodes int `json:"episodes"`
|
Episodes int `json:"episodes"`
|
||||||
State string `json:"state"`
|
State string `json:"state"`
|
||||||
Rating string `json:"rating"`
|
Rating string `json:"rating"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type meikanAPIIDs struct {
|
type meikanAPIIDs struct {
|
||||||
@ -78,13 +85,20 @@ func home(c *router.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func compare(c *router.Context) error {
|
func compare(c *router.Context) error {
|
||||||
|
start := time.Now()
|
||||||
user := c.Param(`user`)
|
user := c.Param(`user`)
|
||||||
//https://api.jikan.moe/v3/user/nekomata1037/animelist/all
|
//https://api.jikan.moe/v3/user/nekomata1037/animelist/all
|
||||||
//https://api.meikan.moe/v1/ids/anime
|
//https://api.meikan.moe/v1/ids/anime
|
||||||
jikanList := jikanAPIGetUserAnimelist(user)
|
jikanList := jikanAPIGetUserAnimelist(user)
|
||||||
var anime []meikanListItem
|
var anime []meikanListItem
|
||||||
//fmt.Println(result)
|
//fmt.Println(result)
|
||||||
|
fmt.Println("Getting IDs")
|
||||||
IDs := meikanAPIGetIDs()
|
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++ {
|
for i := 0; i < len(jikanList.Anime); i++ {
|
||||||
//fmt.Println(result.Anime[i].Title)
|
//fmt.Println(result.Anime[i].Title)
|
||||||
for d := 0; d < len(IDs); d++ {
|
for d := 0; d < len(IDs); d++ {
|
||||||
@ -93,11 +107,11 @@ func compare(c *router.Context) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
temp := meikanListItem{Anime: meikanAnime{ID: jikanList.Anime[i].MeikanID,
|
temp := meikanListItem{Anime: meikanAnime{ID: jikanList.Anime[i].MeikanID,
|
||||||
Title: jikanList.Anime[i].Title,
|
Title: jikanList.Anime[i].Title,
|
||||||
Type: jikanList.Anime[i].Type,
|
Type: jikanList.Anime[i].Type,
|
||||||
Episodes: jikanList.Anime[i].TotalEpisodes},
|
Episodes: jikanList.Anime[i].TotalEpisodes},
|
||||||
Episode: jikanList.Anime[i].WatchedEpisodes,
|
Episode: jikanList.Anime[i].WatchedEpisodes,
|
||||||
Rating: jikanList.Anime[i].Score}
|
Rating: jikanList.Anime[i].Score}
|
||||||
switch jikanList.Anime[i].WatchingStatus {
|
switch jikanList.Anime[i].WatchingStatus {
|
||||||
case 1:
|
case 1:
|
||||||
jikanList.Completed++
|
jikanList.Completed++
|
||||||
@ -125,11 +139,14 @@ func compare(c *router.Context) error {
|
|||||||
//resJSON, err := json.MarshalIndent(result, "", "\t")
|
//resJSON, err := json.MarshalIndent(result, "", "\t")
|
||||||
//check(err)
|
//check(err)
|
||||||
//jsonfile := []byte(resJSON)
|
//jsonfile := []byte(resJSON)
|
||||||
return c.JSON(200, jikanList)
|
elapsed := time.Since(start)
|
||||||
|
fmt.Printf("Generating json took %s\n", elapsed)
|
||||||
|
return c.JSON(200, anime)
|
||||||
}
|
}
|
||||||
|
|
||||||
func jikanAPIGetUserAnimelist(user string) jikanAPIUserList {
|
func jikanAPIGetUserAnimelist(user string) jikanAPIUserList {
|
||||||
var result jikanAPIUserList
|
var result jikanAPIUserList
|
||||||
|
start := time.Now()
|
||||||
for i := 1; ; i++ {
|
for i := 1; ; i++ {
|
||||||
resp, err := http.Get("https://api.jikan.moe/v3/user/" + user + "/animelist/all/" + strconv.Itoa(i))
|
resp, err := http.Get("https://api.jikan.moe/v3/user/" + user + "/animelist/all/" + strconv.Itoa(i))
|
||||||
check(err)
|
check(err)
|
||||||
@ -139,21 +156,26 @@ func jikanAPIGetUserAnimelist(user string) jikanAPIUserList {
|
|||||||
check(err)
|
check(err)
|
||||||
if i == 1 {
|
if i == 1 {
|
||||||
_ = json.NewDecoder(bytes.NewReader(body)).Decode(&result)
|
_ = json.NewDecoder(bytes.NewReader(body)).Decode(&result)
|
||||||
fmt.Println("Searching page 1")
|
elapsed := time.Since(start)
|
||||||
|
fmt.Printf("Searching page 1 took %s\n", elapsed)
|
||||||
continue
|
continue
|
||||||
} else {
|
} else {
|
||||||
_ = json.NewDecoder(bytes.NewReader(body)).Decode(&tempResult)
|
_ = json.NewDecoder(bytes.NewReader(body)).Decode(&tempResult)
|
||||||
result.Anime = append(result.Anime, tempResult.Anime...)
|
result.Anime = append(result.Anime, tempResult.Anime...)
|
||||||
fmt.Println("Searching page " + strconv.Itoa(i))
|
elapsed := time.Since(start)
|
||||||
|
fmt.Printf("Searching page %d took %s\n", i, elapsed)
|
||||||
}
|
}
|
||||||
if len(tempResult.Anime) == 0 {
|
if len(tempResult.Anime) == 0 {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
elapsed := time.Since(start)
|
||||||
|
fmt.Printf("Searching the userlist took %s\n", elapsed)
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func meikanAPIGetIDs() []meikanAPIIDs {
|
func meikanAPIGetIDs() []meikanAPIIDs {
|
||||||
|
start := time.Now()
|
||||||
resp, err := http.Get("https://api.meikan.moe/v1/ids/anime")
|
resp, err := http.Get("https://api.meikan.moe/v1/ids/anime")
|
||||||
check(err)
|
check(err)
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
@ -161,5 +183,21 @@ func meikanAPIGetIDs() []meikanAPIIDs {
|
|||||||
body, err := ioutil.ReadAll(resp.Body)
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
check(err)
|
check(err)
|
||||||
_ = json.NewDecoder(bytes.NewReader(body)).Decode(&result)
|
_ = json.NewDecoder(bytes.NewReader(body)).Decode(&result)
|
||||||
|
elapsed := time.Since(start)
|
||||||
|
fmt.Printf("Getting all the IDs took %s\n", elapsed)
|
||||||
return result
|
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
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user