util.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package common
  2. import (
  3. "fmt"
  4. "github.com/go-resty/resty/v2"
  5. "net/http"
  6. "regexp"
  7. "strings"
  8. )
  9. // NewHttpClient 新建一个 resty 的对象
  10. func NewHttpClient(_reqParam ...ReqParam) *resty.Client {
  11. //const defUserAgent = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50"
  12. const defUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36 Edg/91.0.864.41"
  13. var reqParam ReqParam
  14. var HttpProxy, UserAgent, Referer string
  15. if len(_reqParam) > 0 {
  16. reqParam = _reqParam[0]
  17. }
  18. if len(reqParam.HttpProxy) > 0 {
  19. HttpProxy = reqParam.HttpProxy
  20. }
  21. if len(reqParam.UserAgent) > 0 {
  22. UserAgent = reqParam.UserAgent
  23. } else {
  24. UserAgent = defUserAgent
  25. }
  26. if len(reqParam.Referer) > 0 {
  27. Referer = reqParam.Referer
  28. }
  29. httpClient := resty.New()
  30. httpClient.SetTimeout(HTMLTimeOut)
  31. if HttpProxy != "" {
  32. httpClient.SetProxy(HttpProxy)
  33. }
  34. httpClient.SetHeaders(map[string]string{
  35. "Content-Type": "application/json",
  36. "User-Agent": UserAgent,
  37. })
  38. if len(Referer) > 0 {
  39. httpClient.SetHeader("Referer", Referer)
  40. }
  41. return httpClient
  42. }
  43. // DownFile 从指定的 Url 下载文件
  44. func DownFile(urlStr string, _reqParam ...ReqParam) ([]byte, string, error) {
  45. var reqParam ReqParam
  46. if len(_reqParam) > 0 {
  47. reqParam = _reqParam[0]
  48. }
  49. httpClient := NewHttpClient(reqParam)
  50. resp, err := httpClient.R().Get(urlStr)
  51. if err != nil {
  52. return nil, "", err
  53. }
  54. filename := GetFileName(resp.RawResponse)
  55. return resp.Body(), filename, nil
  56. }
  57. // GetFileName 获取下载文件的文件名
  58. func GetFileName(resp *http.Response) string {
  59. contentDisposition := resp.Header.Get("Content-Disposition")
  60. if len(contentDisposition) == 0 {
  61. return ""
  62. }
  63. re := regexp.MustCompile(`filename=["]*([^"]+)["]*`)
  64. matched := re.FindStringSubmatch(contentDisposition)
  65. if matched == nil || len(matched) == 0 || len(matched[0]) == 0 {
  66. //fmt.Println("######")
  67. return ""
  68. }
  69. return matched[1]
  70. }
  71. // AddBaseUrl 判断驶入的 url 是否需要拼接 baseUrl
  72. func AddBaseUrl(baseUrl, url string) string {
  73. if strings.Contains(url, "://") {
  74. return url
  75. }
  76. return fmt.Sprintf("%s%s", baseUrl, url)
  77. }
  78. // ReqParam 可选择传入的参数
  79. type ReqParam struct {
  80. HttpProxy string // HttpClient 相关
  81. UserAgent string // HttpClient 相关
  82. Referer string // HttpClient 相关
  83. MediaType string // HttpClient 相关
  84. Charset string // HttpClient 相关
  85. Topic int // 搜索结果的时候,返回 Topic N 以内的
  86. }