client.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package handler
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "codeberg.org/vnpower/pixivfe/models"
  9. )
  10. type PixivClient struct {
  11. Client *http.Client
  12. Cookie map[string]string
  13. Header map[string]string
  14. Lang string
  15. }
  16. func (p *PixivClient) SetHeader(header map[string]string) {
  17. p.Header = header
  18. }
  19. func (p *PixivClient) AddHeader(key, value string) {
  20. p.Header[key] = value
  21. }
  22. func (p *PixivClient) SetUserAgent(value string) {
  23. p.AddHeader("User-Agent", value)
  24. }
  25. func (p *PixivClient) SetCookie(cookie map[string]string) {
  26. p.Cookie = cookie
  27. }
  28. func (p *PixivClient) AddCookie(key, value string) {
  29. p.Cookie[key] = value
  30. }
  31. func (p *PixivClient) SetSessionID(value string) {
  32. p.Cookie["PHPSESSID"] = value
  33. }
  34. func (p *PixivClient) SetLang(lang string) {
  35. p.Lang = lang
  36. }
  37. func (p *PixivClient) Request(URL string, token ...string) (*http.Response, error) {
  38. req, _ := http.NewRequest("GET", URL, nil)
  39. // Add headers
  40. for k, v := range p.Header {
  41. req.Header.Add(k, v)
  42. }
  43. for k, v := range p.Cookie {
  44. req.AddCookie(&http.Cookie{Name: k, Value: v})
  45. }
  46. if token != nil {
  47. req.AddCookie(&http.Cookie{Name: "PHPSESSID", Value: token[0]})
  48. }
  49. // Make a request
  50. resp, err := p.Client.Do(req)
  51. if err != nil {
  52. return resp, err
  53. }
  54. if resp.StatusCode != 200 {
  55. return resp, errors.New(fmt.Sprintf("Pixiv returned code: %d for request %s", resp.StatusCode, URL))
  56. }
  57. return resp, nil
  58. }
  59. func (p *PixivClient) TextRequest(URL string, tokens ...string) (string, error) {
  60. var token string
  61. if len(token) > 0 {
  62. token = tokens[0]
  63. }
  64. /// Make a request to a URL and return the response's string body
  65. resp, err := p.Request(URL, token)
  66. if err != nil {
  67. return "", err
  68. }
  69. // Extract the bytes from server's response
  70. body, err := io.ReadAll(resp.Body)
  71. if err != nil {
  72. return string(body), err
  73. }
  74. return string(body), nil
  75. }
  76. func (p *PixivClient) PixivRequest(URL string, tokens ...string) (json.RawMessage, error) {
  77. /// Make a request to a Pixiv API URL with a standard response, handle errors and return the raw JSON response
  78. var response models.PixivResponse
  79. var token string
  80. if len(token) > 0 {
  81. token = tokens[0]
  82. }
  83. body, err := p.TextRequest(URL, token)
  84. // body = strings.ReplaceAll(body, "i.pximg.net", configs.ProxyServer)
  85. if err != nil {
  86. return nil, err
  87. }
  88. err = json.Unmarshal([]byte(body), &response)
  89. if err != nil {
  90. return nil, err
  91. }
  92. if response.Error {
  93. // Pixiv returned an error
  94. return nil, errors.New("Pixiv responded: " + response.Message)
  95. }
  96. return response.Body, nil
  97. }