top.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package handler
  2. import (
  3. "fmt"
  4. "codeberg.org/vnpower/pixivfe/models"
  5. "github.com/goccy/go-json"
  6. )
  7. func (p *PixivClient) GetLandingPage(mode string) (models.LandingArtworks, error) {
  8. var context models.LandingArtworks
  9. URL := fmt.Sprintf(LandingPageURL, mode)
  10. response, err := p.PixivRequest(URL)
  11. if err != nil {
  12. return context, err
  13. }
  14. type IDS struct {
  15. Ids []any `json:"ids"`
  16. }
  17. var pages struct {
  18. Pixivision []models.Pixivision `json:"pixivision"`
  19. Follow []any `json:"follow"`
  20. Commission []any `json:"completeRequestIds"`
  21. Newest []any `json:"newPost"`
  22. Recommended IDS `json:"recommend"`
  23. EditorRecommended []any `json:"editorRecommend"`
  24. UserRecommended []any `json:"recommendUser"`
  25. RecommendedByTags []struct {
  26. models.LandingRecommendByTags
  27. Ids []any `json:"ids"`
  28. } `json:"recommendByTag"`
  29. }
  30. var body struct {
  31. Thumbnails json.RawMessage `json:"thumbnails"`
  32. Page json.RawMessage `json:"page"`
  33. }
  34. var artworks struct {
  35. Artworks []models.IllustShort `json:"illust"`
  36. }
  37. err = json.Unmarshal([]byte(response), &body)
  38. if err != nil {
  39. return context, err
  40. }
  41. err = json.Unmarshal([]byte(body.Thumbnails), &artworks)
  42. if err != nil {
  43. return context, err
  44. }
  45. err = json.Unmarshal([]byte(body.Page), &pages)
  46. if err != nil {
  47. return context, err
  48. }
  49. context.Pixivision = pages.Pixivision
  50. // Keep track
  51. count := len(pages.Commission)
  52. context.Commissions = artworks.Artworks[:count]
  53. context.Following = artworks.Artworks[count : count+len(pages.Follow)]
  54. count += len(pages.Follow)
  55. context.Recommended = artworks.Artworks[count : count+len(pages.Recommended.Ids)]
  56. count += len(pages.Recommended.Ids)
  57. context.Newest = artworks.Artworks[count : count+len(pages.Newest)]
  58. count += len(pages.Newest)
  59. // For rankings, we just take 100 anyway
  60. context.Rankings = artworks.Artworks[count : count+100]
  61. count += 100
  62. // IDK what this is
  63. count += len(pages.EditorRecommended)
  64. context.Users = artworks.Artworks[count : count+len(pages.UserRecommended)*3]
  65. count += len(pages.UserRecommended) * 3
  66. for i := 0; i < len(pages.RecommendedByTags); i++ {
  67. temp := pages.RecommendedByTags[i]
  68. temp.Artworks = artworks.Artworks[count : count+min(len(temp.Ids), 18)]
  69. context.RecommendByTags = append(context.RecommendByTags, temp.LandingRecommendByTags)
  70. count += len(temp.Ids)
  71. }
  72. return context, nil
  73. }