From 072b70a67116ef2dfde68ddbff083c522b65c089 Mon Sep 17 00:00:00 2001 From: VnPower Date: Sat, 13 May 2023 19:25:32 +0700 Subject: [PATCH] Feature: daily ranking --- handler/illust.go | 31 +++++++++++++++++++++++++------ main.go | 17 +++++++++++++---- template/index.html | 14 ++++++++------ 3 files changed, 46 insertions(+), 16 deletions(-) diff --git a/handler/illust.go b/handler/illust.go index 993d750..6730453 100644 --- a/handler/illust.go +++ b/handler/illust.go @@ -35,12 +35,31 @@ func GetRecommendedIllust(c *gin.Context) []entity.Illust { return illusts } -func getJson(url string, target interface{}) error { - r, err := http.Get(url) - if err != nil { - return err +func GetRankingIllust(c *gin.Context, mode string) []entity.Illust { + if mode == "" { + mode = "day" } - defer r.Body.Close() + URL := "https://hibi.cocomi.cf/api/pixiv/rank?mode=" + mode + var illusts []entity.Illust + illusts = append(illusts, entity.Illust{}) // Placeholder to shift the index - return json.NewDecoder(r.Body).Decode(target) + resp, err := http.Get(URL) + + 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.") + } + + return illusts } diff --git a/main.go b/main.go index 3dbd597..3560307 100644 --- a/main.go +++ b/main.go @@ -1,23 +1,32 @@ package main import ( + "html/template" "net/http" "pixivfe/handler" "github.com/gin-gonic/gin" ) -func test(c *gin.Context) { - data := handler.GetRecommendedIllust(c) - c.HTML(http.StatusOK, "index.html", data) +func inc(n int) int { + return n + 1 +} + +func index_page(c *gin.Context) { + recommended := handler.GetRecommendedIllust(c) + ranking := handler.GetRankingIllust(c, "") + c.HTML(http.StatusOK, "index.html", gin.H{"Recommended": recommended, "Rankings": ranking}) } func main() { server := gin.Default() + server.SetFuncMap(template.FuncMap{ + "inc": inc, + }) server.Static("css/", "./template/css") server.LoadHTMLGlob("template/*.html") // Listen and Servr in 0.0.0.0:8080 - server.GET("/", test) + server.GET("/", index_page) server.Run(":8080") } diff --git a/template/index.html b/template/index.html index 10dc64d..b170367 100644 --- a/template/index.html +++ b/template/index.html @@ -25,7 +25,7 @@

Recommended works