mirror of
https://codeberg.org/VnPower/PixivFE
synced 2024-12-06 19:16:23 +01:00
ba3ec43559
thanks semgrep! Squashed commit of the following: commit 3a5e08190583804d3b330ecc6cb3422bc04c291b Author: iacore <noreply+gpg-stub@1a-insec.net> Date: Fri Feb 9 12:58:07 2024 +0000 final commit f1cdc73e9f8d331715de8e8aec5d1573218df0c3 Author: iacore <noreply+gpg-stub@1a-insec.net> Date: Fri Feb 9 12:50:23 2024 +0000 manual tweak commit 3a3ba2b8d8d7ce9af4d897288596d9318c715a08 Author: iacore <noreply+gpg-stub@1a-insec.net> Date: Fri Feb 9 12:49:15 2024 +0000 stage 1
99 lines
2.4 KiB
Go
99 lines
2.4 KiB
Go
package core
|
|
|
|
import (
|
|
"fmt"
|
|
"html/template"
|
|
"net/http"
|
|
"time"
|
|
|
|
session "codeberg.org/vnpower/pixivfe/v2/core/config"
|
|
url "codeberg.org/vnpower/pixivfe/v2/core/http"
|
|
"github.com/gofiber/fiber/v2"
|
|
"golang.org/x/net/html"
|
|
)
|
|
|
|
func get_weekday(n time.Weekday) int {
|
|
switch n {
|
|
case time.Sunday:
|
|
return 1
|
|
case time.Monday:
|
|
return 2
|
|
case time.Tuesday:
|
|
return 3
|
|
case time.Wednesday:
|
|
return 4
|
|
case time.Thursday:
|
|
return 5
|
|
case time.Friday:
|
|
return 6
|
|
case time.Saturday:
|
|
return 7
|
|
}
|
|
return 0
|
|
}
|
|
|
|
func GetRankingCalendar(c *fiber.Ctx, mode string, year, month int) (template.HTML, error) {
|
|
token := session.GetToken(c)
|
|
URL := url.GetRankingCalendarURL(mode, year, month)
|
|
|
|
req, _ := http.NewRequest("GET", URL, nil)
|
|
req.Header.Add("User-Agent", "Mozilla/5.0")
|
|
req.Header.Add("Cookie", "PHPSESSID="+token)
|
|
// req.AddCookie(&http.Cookie{
|
|
// Name: "PHPSESSID",
|
|
// Value: token,
|
|
// })
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
// Use the html package to parse the response body from the request
|
|
doc, err := html.Parse(resp.Body)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
// Find and print all links on the web page
|
|
var links []string
|
|
var link func(*html.Node)
|
|
link = func(n *html.Node) {
|
|
if n.Type == html.ElementNode && n.Data == "img" {
|
|
for _, a := range n.Attr {
|
|
if a.Key == "data-src" {
|
|
// adds a new link entry when the attribute matches
|
|
links = append(links, session.ProxyImageUrl(a.Val))
|
|
}
|
|
}
|
|
}
|
|
|
|
// traverses the HTML of the webpage from the first child node
|
|
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
|
link(c)
|
|
}
|
|
}
|
|
link(doc)
|
|
|
|
// now := time.Now()
|
|
// yearNow := now.Year()
|
|
// monthNow := now.Month()
|
|
lastMonth := time.Date(year, time.Month(month), 0, 0, 0, 0, 0, time.UTC)
|
|
thisMonth := time.Date(year, time.Month(month+1), 0, 0, 0, 0, 0, time.UTC)
|
|
|
|
renderString := ""
|
|
for i := 0; i < get_weekday(lastMonth.Weekday()); i++ {
|
|
renderString += "<div class=\"calendar-node calendar-node-empty\"></div>"
|
|
}
|
|
for i := 0; i < thisMonth.Day(); i++ {
|
|
date := fmt.Sprintf("%d%02d%02d", year, month, i+1)
|
|
if len(links) > i {
|
|
renderString += fmt.Sprintf(`<a href="/ranking?mode=%s&date=%s"><div class="calendar-node" style="background-image: url(%s)"><span>%d</span></div></a>`, mode, date, links[i], i+1)
|
|
} else {
|
|
renderString += fmt.Sprintf(`<div class="calendar-node"><span>%d</span></div>`, i+1)
|
|
}
|
|
}
|
|
return template.HTML(renderString), nil
|
|
}
|