discovery.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. func GetDiscoveryArtwork(c *fiber.Ctx, mode string) ([]ArtworkBrief, error) {
  11. token := session.GetPixivToken(c)
  12. URL := http.GetDiscoveryURL(mode, 100)
  13. var artworks []ArtworkBrief
  14. resp, err := http.UnwrapWebAPIRequest(c.Context(), URL, token)
  15. if err != nil {
  16. return nil, err
  17. }
  18. resp = session.ProxyImageUrl(c, resp)
  19. if !gjson.Valid(resp) {
  20. return nil, fmt.Errorf("Invalid JSON: %v", resp)
  21. }
  22. data := gjson.Get(resp, "thumbnails.illust").String()
  23. err = json.Unmarshal([]byte(data), &artworks)
  24. if err != nil {
  25. return nil, err
  26. }
  27. return artworks, nil
  28. }
  29. func GetDiscoveryNovels(c *fiber.Ctx, mode string) ([]NovelBrief, error) {
  30. token := session.GetPixivToken(c)
  31. URL := http.GetDiscoveryNovelURL(mode, 100)
  32. var novels []NovelBrief
  33. resp, err := http.UnwrapWebAPIRequest(c.Context(), URL, token)
  34. if err != nil {
  35. return nil, err
  36. }
  37. resp = session.ProxyImageUrl(c, resp)
  38. if !gjson.Valid(resp) {
  39. return nil, fmt.Errorf("Invalid JSON: %v", resp)
  40. }
  41. data := gjson.Get(resp, "thumbnails.novel").String()
  42. err = json.Unmarshal([]byte(data), &novels)
  43. if err != nil {
  44. return nil, err
  45. }
  46. return novels, nil
  47. }