ranking.go 1.6 KB

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