index.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package core
  2. import (
  3. "fmt"
  4. session "codeberg.org/vnpower/pixivfe/v2/core/session"
  5. http "codeberg.org/vnpower/pixivfe/v2/core/http"
  6. "github.com/goccy/go-json"
  7. "github.com/gofiber/fiber/v2"
  8. "github.com/tidwall/gjson"
  9. )
  10. type Pixivision struct {
  11. ID string `json:"id"`
  12. Title string `json:"title"`
  13. Thumbnail string `json:"thumbnailUrl"`
  14. URL string `json:"url"`
  15. }
  16. type RecommendedTags struct {
  17. Name string `json:"tag"`
  18. Artworks []ArtworkBrief
  19. }
  20. type LandingArtworks struct {
  21. Commissions []ArtworkBrief
  22. Following []ArtworkBrief
  23. Recommended []ArtworkBrief
  24. Newest []ArtworkBrief
  25. Rankings []ArtworkBrief
  26. Users []ArtworkBrief
  27. Pixivision []Pixivision
  28. RecommendByTags []RecommendedTags
  29. }
  30. func GetLanding(c *fiber.Ctx, mode string) (*LandingArtworks, error) {
  31. var pages struct {
  32. Pixivision []Pixivision `json:"pixivision"`
  33. Follow []int `json:"follow"`
  34. Recommended struct {
  35. IDs []string `json:"ids"`
  36. } `json:"recommend"`
  37. // EditorRecommended []any `json:"editorRecommend"`
  38. // UserRecommended []any `json:"recommendUser"`
  39. // Commission []any `json:"completeRequestIds"`
  40. RecommendedByTags []struct {
  41. Name string `json:"tag"`
  42. IDs []string `json:"ids"`
  43. } `json:"recommendByTag"`
  44. }
  45. URL := http.GetLandingURL(mode)
  46. var landing LandingArtworks
  47. resp, err := http.UnwrapWebAPIRequest(c.Context(), URL, "")
  48. if err != nil {
  49. return &landing, err
  50. }
  51. resp = session.ProxyImageUrl(c, resp)
  52. if !gjson.Valid(resp) {
  53. return nil, fmt.Errorf("Invalid JSON: %v", resp)
  54. }
  55. artworks := map[string]ArtworkBrief{}
  56. // Get thumbnails and save it into a map, since they were kept
  57. // separately and need to the index quickly.
  58. //
  59. // Since there are no duplicates in this object, we are unable
  60. // to rely to ranges (ex. one artwork in two separate sections)
  61. stuff := gjson.Get(resp, "thumbnails.illust")
  62. stuff.ForEach(func(key, value gjson.Result) bool {
  63. var artwork ArtworkBrief
  64. err = json.Unmarshal([]byte(value.String()), &artwork)
  65. if err != nil {
  66. return false
  67. }
  68. if artwork.ID != "" {
  69. artworks[artwork.ID] = artwork
  70. }
  71. return true // keep iterating
  72. })
  73. pagesStr := gjson.Get(resp, "page").String()
  74. err = json.Unmarshal([]byte(pagesStr), &pages)
  75. if err != nil {
  76. return &landing, err
  77. }
  78. // Parse everything
  79. landing.Pixivision = pages.Pixivision
  80. landing.Following = make([]ArtworkBrief, len(pages.Follow))
  81. for _, i := range pages.Follow {
  82. landing.Following = append(landing.Following, artworks[fmt.Sprint(i)])
  83. }
  84. for _, i := range pages.RecommendedByTags {
  85. temp := make([]ArtworkBrief, 0)
  86. for _, j := range i.IDs {
  87. temp = append(temp, artworks[j])
  88. }
  89. landing.RecommendByTags = append(landing.RecommendByTags, RecommendedTags{Name: i.Name, Artworks: temp})
  90. }
  91. landing.Recommended = make([]ArtworkBrief, 0)
  92. for _, i := range pages.Recommended.IDs {
  93. landing.Recommended = append(landing.Recommended, artworks[i])
  94. }
  95. return &landing, nil
  96. }