This repository has been archived by the owner on Apr 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ld246.go
167 lines (151 loc) · 3.8 KB
/
ld246.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package application
import (
"encoding/json"
"fmt"
"github.com/go-rod/rod"
"github.com/hb0730/auto-sign/utils"
"github.com/hb0730/go-request"
"github.com/mritd/logger"
"net/http"
"time"
)
//https://ld246.com
//ldHeaders 设置请求头,防止限流拦截
var ldHeaders = map[string]string{
"User-Agent": "auto-sign/1.0.4",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
}
//Ld246 通过token签到
type Ld246 struct {
Username string
Password string
}
//Start 开始
func (ld Ld246) Start() error {
if ld.Username == "" {
logger.Info("[ld246] username is null")
return &utils.AutoSignError{
Module: "ld246",
Method: "start",
Message: "username is null",
}
}
if ld.Password == "" {
logger.Warn("[ld246] password is null")
return &utils.AutoSignError{
Module: "ld246",
Method: "start",
Message: "password is null",
}
}
return ld.doStart()
}
func (ld Ld246) doStart() error {
result, err := ld.Login()
if err != nil {
return err
}
var cookies = map[string]string{
"symphony": result.Token,
}
ld.Sign(cookies)
return ld.Logout(cookies)
}
//Login 通过username/password 换取token
func (ld Ld246) Login() (LoginResult, error) {
logger.Info("[ld246] login .....")
var result LoginResult
params := make(map[string]string, 0)
params["userName"] = ld.Username
params["userPassword"] = utils.GetMd5(ld.Password)
requestBody, _ := json.Marshal(params)
rq, err := request.CreateRequest(
"POST",
"https://ld246.com/api/v2/login",
string(requestBody))
if err != nil {
return result, err
}
err = rq.Do()
if err != nil {
return result, err
}
by, err := rq.GetBody()
if err != nil {
return result, err
}
logger.Info("[ld246] login success")
_ = json.Unmarshal(by, &result)
return result, nil
}
// Sign 签到
func (ld Ld246) Sign(cookies map[string]string) {
b := utils.CreateBrowser(false)
defer b.MustClose()
page := b.MustSetCookies(utils.ConvertRodCookies(cookies, ".ld246.com")...).
MustPage("")
defer page.MustClose()
//设置header
page.MustSetExtraHeaders(rodHeader()...)
page = page.MustNavigate("https://ld246.com/activity/checkin")
page.Timeout(30 * time.Second).MustWait(`document.title==='每日签到 - 活动 - 链滴'`).MustWaitLoad()
//time.Sleep(10 * time.Second)
page.MustWaitLoad()
page.Timeout(30*time.Second).
Race().
ElementR(`div.module__body > a.btn`, `领取今日签到奖励`).
MustHandle(func(e *rod.Element) {
_ = e.MustClick().WaitLoad()
page.MustNavigate("https://ld246.com/activity/checkin").MustWaitLoad()
html := page.MustElement("a.btn").MustWaitLoad().MustText()
logger.Infof("[ld246] %s", fmt.Sprintf("签到成功,%s \n", html))
}).Element(`a.btn`).MustHandle(func(e *rod.Element) {
html := e.MustText()
str := fmt.Sprintf("今日已签到, %s \n", html)
logger.Infof("[ld246] %s", str)
}).MustDo()
}
// Logout 登出
func (ld Ld246) Logout(cookies map[string]string) error {
if len(cookies) == 0 {
logger.Warn("[ld246] token is null")
return nil
}
rq, err := request.CreateRequest(
"POST",
"https://ld246.com/api/v2/logout",
"",
)
if err != nil {
return err
}
rq.Header(setHeader())
rq.AddCookiesFromMap(cookies)
err = rq.Do()
if err != nil {
return err
}
by, err := rq.GetBody()
logger.Infof("[ld246] logout success %v \n", string(by))
return nil
}
type LoginResult struct {
Code int `json:"code"`
Msg string `json:"msg"`
Token string `json:"token"`
UserName string `json:"username"`
}
func rodHeader() []string {
header := make([]string, 0)
for k, v := range ldHeaders {
header = append(append(header, k), v)
}
return header
}
func setHeader() http.Header {
header := http.Header{}
for k, v := range ldHeaders {
header.Set(k, v)
}
return header
}