index.go 2.9 KB

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