client.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package httpserver
  2. import (
  3. "fmt"
  4. "io"
  5. "net/http"
  6. "strings"
  7. )
  8. /*
  9. Retrieve the site audit config file from Semrush
  10. returns a byte array of the body, the content type of the resp, and an error
  11. */
  12. func (c *Controller) RetrieveStaticResource(path string) ([]byte, string, error) {
  13. url := fmt.Sprintf("https://static.semrush.com%s", path)
  14. req, err := http.NewRequest("GET", url, nil)
  15. if err != nil {
  16. return nil, "", err
  17. }
  18. req.AddCookie(c.Config.PhpSession)
  19. req.AddCookie(c.Config.SsoToken)
  20. req.Header.Set("User-Agent", c.Config.UserAgent)
  21. req.Header.Set("Referer", "https://www.semrush.com")
  22. req.Header.Set("Origin", "https://www.semrush.com")
  23. resp, err := c.Client.Do(req)
  24. if err != nil {
  25. return nil, "", err
  26. }
  27. defer resp.Body.Close()
  28. b, err := io.ReadAll(resp.Body)
  29. if err != nil {
  30. return nil, "", err
  31. }
  32. return b, resp.Header.Get("content-type"), nil
  33. }
  34. /*
  35. Perform a call against the siteaudit api
  36. :param path: the URI path with the query
  37. :param query: the query to add to the request
  38. :returns a byte array of the response, the content type and an error
  39. */
  40. func (c *Controller) SiteauditApiCall(method string, path string, query string, body io.Reader) ([]byte, string, error) {
  41. query = strings.ReplaceAll(query, "https://sem.bunnytools.shop", "https://www.semrush.com")
  42. url := fmt.Sprintf("https://www.semrush.com%s?%s", path, query)
  43. req, err := http.NewRequest(method, url, body)
  44. if err != nil {
  45. return nil, "", err
  46. }
  47. req.AddCookie(c.Config.PhpSession)
  48. req.AddCookie(c.Config.SsoToken)
  49. req.Header.Set("User-Agent", c.Config.UserAgent)
  50. req.Header.Set("Referer", "https://www.semrush.com")
  51. req.Header.Set("Origin", "https://www.semrush.com")
  52. resp, err := c.Client.Do(req)
  53. if err != nil {
  54. return nil, "", err
  55. }
  56. defer resp.Body.Close()
  57. b, err := io.ReadAll(resp.Body)
  58. if err != nil {
  59. return nil, "", err
  60. }
  61. return b, resp.Header.Get("content-type"), nil
  62. }
  63. /*
  64. Generic site call to the semrush site
  65. */
  66. func (c *Controller) SemrushGeneric() ([]byte, string, error) {
  67. // TODO: make this working lol
  68. }