ranking.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package core
  2. import (
  3. "errors"
  4. "strings"
  5. "codeberg.org/vnpower/pixivfe/v2/session"
  6. "github.com/goccy/go-json"
  7. "github.com/gofiber/fiber/v2"
  8. )
  9. type Ranking struct {
  10. Contents []struct {
  11. Title string `json:"title"`
  12. Image string `json:"url"`
  13. Pages int `json:"illust_page_count,string"`
  14. ArtistName string `json:"user_name"`
  15. ArtistAvatar string `json:"profile_img"`
  16. ID int `json:"illust_id"`
  17. ArtistID int `json:"user_id"`
  18. Rank int `json:"rank"`
  19. IllustType int `json:"illust_type,string"`
  20. } `json:"contents"`
  21. Mode string `json:"mode"`
  22. Content string `json:"content"`
  23. Page int `json:"page"`
  24. RankTotal int `json:"rank_total"`
  25. CurrentDate string `json:"date"`
  26. PrevDateRaw json.RawMessage `json:"prev_date"`
  27. NextDateRaw json.RawMessage `json:"next_date"`
  28. PrevDate string
  29. NextDate string
  30. }
  31. func GetRanking(c *fiber.Ctx, mode, content, date, page string) (Ranking, error) {
  32. URL := GetRankingURL(mode, content, date, page)
  33. var ranking Ranking
  34. resp := WebAPIRequest(c.Context(), URL, "")
  35. if !resp.Ok {
  36. return ranking, errors.New(resp.Message)
  37. }
  38. proxiedResp := session.ProxyImageUrl(c, resp.Body)
  39. err := json.Unmarshal([]byte(proxiedResp), &ranking)
  40. if err != nil {
  41. return ranking, err
  42. }
  43. ranking.PrevDate = strings.ReplaceAll(string(ranking.PrevDateRaw[:]), "\"", "")
  44. ranking.NextDate = strings.ReplaceAll(string(ranking.NextDateRaw[:]), "\"", "")
  45. return ranking, nil
  46. }