actions.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. errr := gjson.Get(string(body), "error")
  33. if !errr.Exists() {
  34. return errors.New("Incompatible request body.")
  35. }
  36. if errr.Bool() {
  37. return errors.New("Pixiv: Invalid request.")
  38. }
  39. return nil
  40. }
  41. func AddBookmarkRoute(c *fiber.Ctx) error {
  42. token := session.CheckToken(c)
  43. csrf := session.GetCSRFToken(c)
  44. if token == "" || csrf == "" {
  45. return c.Redirect("/login")
  46. }
  47. id := c.Params("id")
  48. if id == "" {
  49. return errors.New("No ID provided.")
  50. }
  51. URL := "https://www.pixiv.net/ajax/illusts/bookmarks/add"
  52. payload := fmt.Sprintf(`{
  53. "illust_id": "%s",
  54. "restrict": 0,
  55. "comment": "",
  56. "tags": []
  57. }`, id)
  58. if err := pixivPostRequest(URL, payload, token, csrf); err != nil {
  59. return err
  60. }
  61. return c.SendString("Success")
  62. }
  63. func DeleteBookmarkRoute(c *fiber.Ctx) error {
  64. token := session.CheckToken(c)
  65. csrf := session.GetCSRFToken(c)
  66. if token == "" || csrf == "" {
  67. return c.Redirect("/login")
  68. }
  69. id := c.Params("id")
  70. if id == "" {
  71. return errors.New("No ID provided.")
  72. }
  73. // You can't unlike
  74. URL := "https://www.pixiv.net/ajax/illusts/bookmarks/delete"
  75. payload := fmt.Sprintf(`bookmark_id=%s`, id)
  76. if err := pixivPostRequest(URL, payload, token, csrf); err != nil {
  77. return err
  78. }
  79. return c.SendString("Success")
  80. }
  81. func LikeRoute(c *fiber.Ctx) error {
  82. token := session.CheckToken(c)
  83. csrf := session.GetCSRFToken(c)
  84. if token == "" || csrf == "" {
  85. return c.Redirect("/login")
  86. }
  87. id := c.Params("id")
  88. if id == "" {
  89. return errors.New("No ID provided.")
  90. }
  91. URL := "https://www.pixiv.net/ajax/illusts/like"
  92. payload := fmt.Sprintf(`{"illust_id": "%s"}`, id)
  93. if err := pixivPostRequest(URL, payload, token, csrf); err != nil {
  94. return err
  95. }
  96. return c.SendString("Success")
  97. }