Code cleanup

This commit is contained in:
VnPower 2023-05-14 10:56:50 +07:00
parent f0e4bc1a06
commit de4deb389f
Signed by: vnpower
GPG key ID: 881DE3DEB966106C
2 changed files with 40 additions and 47 deletions

View file

@ -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)
}

View file

@ -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")
}