forked from ThreeCatsLoveFish/MedalHelper
/
user.go
232 lines (213 loc) · 5.41 KB
/
user.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package service
import (
"context"
"errors"
"fmt"
"sync"
"time"
"github.com/BoYanZh/medalhelper/dto"
"github.com/BoYanZh/medalhelper/manager"
"github.com/BoYanZh/medalhelper/service/push"
"github.com/BoYanZh/medalhelper/util"
"github.com/TwiN/go-color"
"github.com/google/uuid"
"github.com/sethvargo/go-retry"
"github.com/tidwall/gjson"
)
type User struct {
// 用户ID
Uid int
// 用户名称
Name string
// 是否登录
isLogin bool
// UUID
uuid []string
// 登录凭证
accessKey string
// 白名单的房间ID
allowedUIDs []int
// 被禁止的房间ID
bannedUIDs []int
// 推送服务
pushName string
// 用户佩戴的勋章
wearMedal dto.MedalInfo
// 用户等级小于20的勋章
medalsLow []dto.MedalInfo
// 今日亲密度没满的勋章
remainMedals []dto.MedalInfo
// 日志信息
message string
}
func NewUser(accessKey, pushName string, allowUIDs, banUIDs []int) User {
return User{
accessKey: accessKey,
allowedUIDs: allowUIDs,
bannedUIDs: banUIDs,
pushName: pushName,
wearMedal: dto.DefaultMedal,
uuid: []string{uuid.NewString(), uuid.NewString()},
message: "",
}
}
func (user User) info(format string, v ...interface{}) {
format = color.Green + "[INFO] " + color.Reset + format
format = color.Reset + color.Blue + user.Name + color.Reset + " " + format
util.PrintColor(format, v...)
}
func (user *User) loginVerify() bool {
resp, err := manager.LoginVerify(user.accessKey)
if err != nil || resp.Data.Mid == 0 {
user.isLogin = false
return false
}
user.Uid = resp.Data.Mid
user.Name = resp.Data.Name
user.isLogin = true
user.info("登录成功")
return true
}
func (user *User) signIn() {
signInfo, err := manager.SignIn(user.accessKey)
if err != nil {
return
}
resp := gjson.Parse(signInfo)
if resp.Get("code").Int() == 0 {
signed := resp.Get("data.hadSignDays").String()
all := resp.Get("data.allDays").String()
user.info("签到成功, 本月签到次数: %s/%s", signed, all)
} else {
user.info("%s", resp.Get("message").String())
}
userInfo, err := manager.GetUserInfo(user.accessKey)
if err != nil {
return
}
level := userInfo.Data.Exp.UserLevel
unext := userInfo.Data.Exp.Unext
user.info("当前用户UL等级: %d, 还差 %d 经验升级", level, unext)
}
func (user *User) setMedals() {
// Clean medals storage
user.medalsLow = make([]dto.MedalInfo, 0, 10)
user.remainMedals = make([]dto.MedalInfo, 0, 10)
// Fetch and update medals
medals, wearMedal := manager.GetMedal(user.accessKey)
if wearMedal {
user.wearMedal = medals[0]
}
// Whitelist
if len(user.allowedUIDs) > 0 {
for _, medal := range medals {
if util.IntContain(user.allowedUIDs, medal.Medal.TargetID) != -1 {
user.medalsLow = append(user.medalsLow, medal)
if medal.Medal.TodayFeed < 1300 {
user.remainMedals = append(user.remainMedals, medal)
}
}
}
return
}
// Default blacklist
for _, medal := range medals {
if util.IntContain(user.bannedUIDs, medal.Medal.TargetID) != -1 {
continue
}
if medal.RoomInfo.RoomID == 0 {
continue
}
if medal.Medal.Level <= 20 {
user.medalsLow = append(user.medalsLow, medal)
if medal.Medal.TodayFeed < 1500 {
user.remainMedals = append(user.remainMedals, medal)
}
}
}
}
func (user *User) checkMedals() bool {
user.setMedals()
fullMedalList := make([]string, 0, len(user.medalsLow))
failMedalList := make([]string, 0)
for _, medal := range user.medalsLow {
if medal.Medal.TodayFeed == 1500 {
fullMedalList = append(fullMedalList, medal.AnchorInfo.NickName)
} else {
failMedalList = append(failMedalList, medal.AnchorInfo.NickName)
}
}
user.message = fmt.Sprintf(
"20级以下牌子共 %d 个\n【1500】%d个\n【1500以下】 %v等 %d个\n",
len(user.medalsLow), len(fullMedalList),
failMedalList, len(failMedalList),
)
user.info(user.message)
return len(fullMedalList) == len(user.medalsLow)
}
// Send daily report notification
func (user *User) report() {
if len(user.pushName) != 0 {
pushEnd := push.NewPush(user.pushName)
_ = pushEnd.Submit(push.Data{
Title: "# 今日亲密度获取情况如下",
Content: fmt.Sprintf("用户%s,%s", user.Name, user.message),
})
}
}
// Send expire notification
func (user *User) expire() {
if len(user.pushName) != 0 {
pushEnd := push.NewPush(user.pushName)
_ = pushEnd.Submit(push.Data{
Title: "# AccessKey 过期",
Content: fmt.Sprintf("用户未登录, accessKey: %s", user.accessKey),
})
}
}
func (user *User) Init() bool {
if user.loginVerify() {
user.signIn()
user.setMedals()
return true
} else {
util.Error("用户登录失败, accessKey: %s", user.accessKey)
user.expire()
return false
}
}
func (user *User) RunOnce() bool {
switch util.GlobalConfig.CD.Async {
case 0: // Sync
task := NewTask(*user, []IAction{
&Like{},
&Danmaku{},
&WatchLive{},
})
task.Start()
case 1: // Async
task := NewTask(*user, []IAction{
&ALike{},
&Danmaku{},
&WatchLive{},
})
task.Start()
}
return user.checkMedals()
}
func (user *User) Start(wg *sync.WaitGroup) {
if user.isLogin {
backOff := retry.NewConstant(5 * time.Second)
backOff = retry.WithMaxRetries(10, backOff)
_ = retry.Do(context.Background(), backOff, func(ctx context.Context) error {
if ok := user.RunOnce(); !ok {
return retry.RetryableError(errors.New("task not complete"))
}
return nil
})
user.report()
} else {
util.Error("用户未登录, accessKey: %s", user.accessKey)
}
wg.Done()
}