| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- package common
- import (
- "fmt"
- "github.com/go-resty/resty/v2"
- "net/http"
- "regexp"
- "strings"
- )
- // NewHttpClient 新建一个 resty 的对象
- func NewHttpClient(_reqParam ...ReqParam) *resty.Client {
- //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"
- 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"
- var reqParam ReqParam
- var HttpProxy, UserAgent, Referer string
- if len(_reqParam) > 0 {
- reqParam = _reqParam[0]
- }
- if len(reqParam.HttpProxy) > 0 {
- HttpProxy = reqParam.HttpProxy
- }
- if len(reqParam.UserAgent) > 0 {
- UserAgent = reqParam.UserAgent
- } else {
- UserAgent = defUserAgent
- }
- if len(reqParam.Referer) > 0 {
- Referer = reqParam.Referer
- }
- httpClient := resty.New()
- httpClient.SetTimeout(HTMLTimeOut)
- if HttpProxy != "" {
- httpClient.SetProxy(HttpProxy)
- }
- httpClient.SetHeaders(map[string]string{
- "Content-Type": "application/json",
- "User-Agent": UserAgent,
- })
- if len(Referer) > 0 {
- httpClient.SetHeader("Referer", Referer)
- }
- return httpClient
- }
- // DownFile 从指定的 Url 下载文件
- func DownFile(urlStr string, _reqParam ...ReqParam) ([]byte, string, error) {
- var reqParam ReqParam
- if len(_reqParam) > 0 {
- reqParam = _reqParam[0]
- }
- httpClient := NewHttpClient(reqParam)
- resp, err := httpClient.R().Get(urlStr)
- if err != nil {
- return nil, "", err
- }
- filename := GetFileName(resp.RawResponse)
- return resp.Body(), filename, nil
- }
- // GetFileName 获取下载文件的文件名
- func GetFileName(resp *http.Response) string {
- contentDisposition := resp.Header.Get("Content-Disposition")
- if len(contentDisposition) == 0 {
- return ""
- }
- re := regexp.MustCompile(`filename=["]*([^"]+)["]*`)
- matched := re.FindStringSubmatch(contentDisposition)
- if matched == nil || len(matched) == 0 || len(matched[0]) == 0 {
- //fmt.Println("######")
- return ""
- }
- return matched[1]
- }
- // AddBaseUrl 判断驶入的 url 是否需要拼接 baseUrl
- func AddBaseUrl(baseUrl, url string) string {
- if strings.Contains(url, "://") {
- return url
- }
- return fmt.Sprintf("%s%s", baseUrl, url)
- }
- // ReqParam 可选择传入的参数
- type ReqParam struct {
- HttpProxy string // HttpClient 相关
- UserAgent string // HttpClient 相关
- Referer string // HttpClient 相关
- MediaType string // HttpClient 相关
- Charset string // HttpClient 相关
- Topic int // 搜索结果的时候,返回 Topic N 以内的
- }
|