Feature: daily ranking

This commit is contained in:
VnPower
2023-05-13 19:25:32 +07:00
parent eb095e3249
commit 072b70a671
3 changed files with 46 additions and 16 deletions
+25 -6
View File
@@ -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
}
+13 -4
View File
@@ -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")
}
+8 -6
View File
@@ -25,7 +25,7 @@
<div class="container">
<h1>Recommended works</h1>
<div class="thumbnail-container">
{{ range . }}
{{ range .Recommended }}
<div class="artwork-thumbnail-small artwork-thumbnail">
<a href="#">
<img
@@ -47,19 +47,20 @@
</div>
{{ end }}
</div>
<h1>Rankings</h1>
<h1>Today's rankings</h1>
<div class="thumbnail-container">
{{ range $i, $_ := .Rankings }}
<div class="artwork-thumbnail-large artwork-thumbnail">
<div class="artwork-rank-circle">1</div>
<div class="artwork-rank-circle">{{ $i | inc}}</div>
<a href="#">
<img
src="https://px2.rainchan.win/c/360x360_70/img-master/img/2023/05/07/17/30/45/107910958_p0_square1200.jpg"
alt="asd"
src="{{ .Images.square_medium }}"
alt="{{ .Title }}"
class="artwork-master-image"
/>
</a>
<h3 class="artwork-thumbnail-title no-margin">Title</h3>
<h3 class="artwork-thumbnail-title no-margin">{{ .Title }}</h3>
<a href="#" class="artwork-thumbnail-artist flex"
><img
src="https://px2.rainchan.win/c/360x360_70/img-master/img/2023/05/07/17/30/45/107910958_p0_square1200.jpg"
@@ -69,6 +70,7 @@
VnPower</a
>
</div>
{{ end }}
</div>
<h1>Pixivision</h1>