-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.go
87 lines (74 loc) · 1.93 KB
/
base.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package wxchannels
import (
"fmt"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
"time"
"github.com/go-kratos/kratos/v2/log"
"github.com/imroc/req/v3"
)
//腾讯视频号相关
const (
UA_MAC_FIREFOX = `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36`
)
type _wxBase struct { //基类
AdminCookies *cookiejar.Jar
Ua string
AppLog *log.Helper
}
func getStrMax(s string, maxLenth int) string {
if len(s) > 100 {
return s[0:100]
} else {
return s
}
}
func (b *_wxBase) GetUADefault() string {
if len(b.Ua) > 0 {
return b.Ua
}
return UA_MAC_FIREFOX
}
func (b *_wxBase) SetUa(ua string) {
b.Ua = ua
}
func (b *_wxBase) GetReqClient(_headers map[string]string) *req.Client {
client := req.C().
SetTimeout(10 * time.Second).
SetUserAgent(b.GetUADefault()).SetCookieJar(b.AdminCookies) //.EnableDumpAll()
if len(_headers) > 0 {
client = client.SetCommonHeaders(_headers)
}
return client
}
func (b *_wxBase) SetAdminCookie(cs []*http.Cookie) (err error) { //默认都是https
if b.AdminCookies == nil {
err = fmt.Errorf("CookieJar为空")
return
}
// 根据不同cookie的域名来设置
diffDomain := make(map[string][]*http.Cookie)
for _, cv := range cs {
if _, isSet := diffDomain[cv.Domain]; !isSet {
diffDomain[cv.Domain] = make([]*http.Cookie, 0)
}
diffDomain[cv.Domain] = append(diffDomain[cv.Domain], cv)
}
for dName, dCookie := range diffDomain {
domainUrl := ""
if len(dName) > 2 && strings.HasPrefix(dName, `.`) {
domainUrl = fmt.Sprintf("https://%s", strings.TrimPrefix(dName, `.`))
} else if len(dName) > 2 && !strings.HasPrefix(dName, `.`) {
domainUrl = fmt.Sprintf("https://%s", dName)
}
uDomain, udErr := url.Parse(domainUrl)
if udErr == nil {
b.AdminCookies.SetCookies(uDomain, dCookie)
} else {
log.Errorf("Errror set:%s cookie异常:%s", dName, udErr.Error())
}
}
return
}