ranking.go 1.5 KB

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