actions.go 2.7 KB

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