actions.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package routes
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "codeberg.org/vnpower/pixivfe/v2/session"
  9. "github.com/gofiber/fiber/v2"
  10. "github.com/tidwall/gjson"
  11. )
  12. func pixivPostRequest(c *fiber.Ctx, url, payload, token, csrf string, isJSON bool) error {
  13. requestBody := []byte(payload)
  14. req, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBody))
  15. if err != nil {
  16. return err
  17. }
  18. req = req.WithContext(c.Context())
  19. req.Header.Add("User-Agent", "Mozilla/5.0")
  20. req.Header.Add("Accept", "application/json")
  21. req.Header.Add("Cookie", "PHPSESSID="+token)
  22. req.Header.Add("x-csrf-token", csrf)
  23. if isJSON {
  24. req.Header.Add("Content-Type", "application/json; charset=utf-8")
  25. } else {
  26. req.Header.Add("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
  27. }
  28. // req.AddCookie(&http.Cookie{
  29. // Name: "PHPSESSID",
  30. // Value: token,
  31. // })
  32. resp, err := http.DefaultClient.Do(req)
  33. if err != nil {
  34. return errors.New("Failed to do this action.")
  35. }
  36. body, err := io.ReadAll(resp.Body)
  37. if err != nil {
  38. return errors.New("Cannot parse the response from Pixiv. Please report this issue.")
  39. }
  40. body_s := string(body)
  41. if !gjson.Valid(body_s) {
  42. return fmt.Errorf("Invalid JSON: %v", body_s)
  43. }
  44. errr := gjson.Get(body_s, "error")
  45. if !errr.Exists() {
  46. return errors.New("Incompatible request body.")
  47. }
  48. if errr.Bool() {
  49. return errors.New("Pixiv: Invalid request.")
  50. }
  51. return nil
  52. }
  53. func AddBookmarkRoute(c *fiber.Ctx) error {
  54. token := session.GetPixivToken(c)
  55. csrf := session.GetCookie(c, session.Cookie_CSRF)
  56. if token == "" || csrf == "" {
  57. return PromptUserToLoginPage(c)
  58. }
  59. id := c.Params("id")
  60. if id == "" {
  61. return errors.New("No ID provided.")
  62. }
  63. URL := "https://www.pixiv.net/ajax/illusts/bookmarks/add"
  64. payload := fmt.Sprintf(`{
  65. "illust_id": "%s",
  66. "restrict": 0,
  67. "comment": "",
  68. "tags": []
  69. }`, id)
  70. if err := pixivPostRequest(c, URL, payload, token, csrf, true); err != nil {
  71. return err
  72. }
  73. return c.SendString("Success")
  74. }
  75. func DeleteBookmarkRoute(c *fiber.Ctx) error {
  76. token := session.GetPixivToken(c)
  77. csrf := session.GetCookie(c, session.Cookie_CSRF)
  78. if token == "" || csrf == "" {
  79. return PromptUserToLoginPage(c)
  80. }
  81. id := c.Params("id")
  82. if id == "" {
  83. return errors.New("No ID provided.")
  84. }
  85. // You can't unlike
  86. URL := "https://www.pixiv.net/ajax/illusts/bookmarks/delete"
  87. payload := fmt.Sprintf(`bookmark_id=%s`, id)
  88. if err := pixivPostRequest(c, URL, payload, token, csrf, false); err != nil {
  89. return err
  90. }
  91. return c.SendString("Success")
  92. }
  93. func LikeRoute(c *fiber.Ctx) error {
  94. token := session.GetPixivToken(c)
  95. csrf := session.GetCookie(c, session.Cookie_CSRF)
  96. if token == "" || csrf == "" {
  97. return PromptUserToLoginPage(c)
  98. }
  99. id := c.Params("id")
  100. if id == "" {
  101. return errors.New("No ID provided.")
  102. }
  103. URL := "https://www.pixiv.net/ajax/illusts/like"
  104. payload := fmt.Sprintf(`{"illust_id": "%s"}`, id)
  105. if err := pixivPostRequest(c, URL, payload, token, csrf, true); err != nil {
  106. return err
  107. }
  108. return c.SendString("Success")
  109. }