discovery.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package core
  2. import (
  3. "fmt"
  4. "codeberg.org/vnpower/pixivfe/v2/session"
  5. "github.com/goccy/go-json"
  6. "github.com/gofiber/fiber/v2"
  7. "github.com/tidwall/gjson"
  8. )
  9. func GetDiscoveryArtwork(c *fiber.Ctx, mode string) ([]ArtworkBrief, error) {
  10. token := session.GetPixivToken(c)
  11. URL := GetDiscoveryURL(mode, 100)
  12. var artworks []ArtworkBrief
  13. resp, err := UnwrapWebAPIRequest(c.Context(), URL, token)
  14. if err != nil {
  15. return nil, err
  16. }
  17. resp = session.ProxyImageUrl(c, resp)
  18. if !gjson.Valid(resp) {
  19. return nil, fmt.Errorf("Invalid JSON: %v", resp)
  20. }
  21. data := gjson.Get(resp, "thumbnails.illust").String()
  22. err = json.Unmarshal([]byte(data), &artworks)
  23. if err != nil {
  24. return nil, err
  25. }
  26. return artworks, nil
  27. }
  28. func GetDiscoveryNovels(c *fiber.Ctx, mode string) ([]NovelBrief, error) {
  29. token := session.GetPixivToken(c)
  30. URL := GetDiscoveryNovelURL(mode, 100)
  31. var novels []NovelBrief
  32. resp, err := UnwrapWebAPIRequest(c.Context(), URL, token)
  33. if err != nil {
  34. return nil, err
  35. }
  36. resp = session.ProxyImageUrl(c, resp)
  37. if !gjson.Valid(resp) {
  38. return nil, fmt.Errorf("Invalid JSON: %v", resp)
  39. }
  40. data := gjson.Get(resp, "thumbnails.novel").String()
  41. err = json.Unmarshal([]byte(data), &novels)
  42. if err != nil {
  43. return nil, err
  44. }
  45. return novels, nil
  46. }