From de4deb389f56349f30edb556c48f513202919019 Mon Sep 17 00:00:00 2001 From: VnPower Date: Sun, 14 May 2023 10:56:50 +0700 Subject: [PATCH] Code cleanup --- handler/illust.go | 83 ++++++++++++++++++++++------------------------- main.go | 4 +-- 2 files changed, 40 insertions(+), 47 deletions(-) diff --git a/handler/illust.go b/handler/illust.go index ff2d168..4bddbaf 100644 --- a/handler/illust.go +++ b/handler/illust.go @@ -14,76 +14,69 @@ import ( func GetRecommendedIllust(c *gin.Context) []entity.Illust { URL := "https://hibi.cocomi.cf/api/pixiv/illust_recommended" var illusts []entity.Illust - resp, err := http.Get(URL) - if err != nil { - panic(":(") - } + s := Request(URL) + g := GetInnerJSON(s, "illusts") - body, err := ioutil.ReadAll(resp.Body) + err := json.Unmarshal([]byte(g), &illusts) if err != nil { - panic(":(") - } - s := string(body) - g := gjson.Get(s, "illusts").String() - - err = json.Unmarshal([]byte(g), &illusts) - if err != nil { - panic(":(") + panic("Failed to parse JSON") } return illusts } func GetRankingIllust(c *gin.Context, mode string) []entity.Illust { - if mode == "" { - mode = "day" - } URL := "https://hibi.cocomi.cf/api/pixiv/rank?mode=" + mode var illusts []entity.Illust - illusts = append(illusts, entity.Illust{}) // Placeholder to shift the index - resp, err := http.Get(URL) + s := Request(URL) + g := GetInnerJSON(s, "illusts") + err := json.Unmarshal([]byte(g), &illusts) if err != nil { - panic("Failed to request") - } - - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - panic("Failed to parse body") - } - s := string(body) - g := gjson.Get(s, "illusts").String() - - err = json.Unmarshal([]byte(g), &illusts) - if err != nil { - panic("Failed to parse Json.") + panic("Failed to parse JSON") } return illusts } func GetSpotlightArticle(c *gin.Context) []entity.Spotlight { - URL := "https://hibi.cocomi.cf/api/pixiv/spotlights?lang=en" + // URL := "https://hibi.cocomi.cf/api/pixiv/spotlights?lang=en" + URL := "https://now.pixiv.pics/api/pixivision?lang=en" var articles []entity.Spotlight - resp, err := http.Get(URL) - if err != nil { - panic(":(") - } + s := Request(URL) + g := GetInnerJSON(s, "articles") - body, err := ioutil.ReadAll(resp.Body) + err := json.Unmarshal([]byte(g), &articles) if err != nil { - panic(":(") - } - s := string(body) - g := gjson.Get(s, "spotlight_articles").String() - - err = json.Unmarshal([]byte(g), &articles) - if err != nil { - panic(":(") + panic("Failed to parse JSON") } return articles } + +func GetInnerJSON(resp string, key string) string { + // As I see, the API always start its response with { "key": [...] } + return gjson.Get(resp, key).String() +} + +func Request(URL string) string { + client := &http.Client{} + + req, _ := http.NewRequest("GET", URL, nil) + req.Header.Set("accept-language", "en") + resp, err := client.Do(req) + + if err != nil { + panic("Failed to make a request to " + URL) + } + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + panic("Failed to parse response") + } + + return string(body) +} diff --git a/main.go b/main.go index 4bf9841..2ce1759 100644 --- a/main.go +++ b/main.go @@ -14,7 +14,7 @@ func inc(n int) int { func index_page(c *gin.Context) { recommended := handler.GetRecommendedIllust(c) - ranking := handler.GetRankingIllust(c, "") + ranking := handler.GetRankingIllust(c, "day") spotlight := handler.GetSpotlightArticle(c) c.HTML(http.StatusOK, "index.html", gin.H{"Recommended": recommended, "Rankings": ranking, "Spotlights": spotlight}) } @@ -28,5 +28,5 @@ func main() { server.LoadHTMLGlob("template/*.html") server.GET("/", index_page) - server.Run(":8000") + server.Run(":8080") }