-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbilibiliRelation.go
114 lines (108 loc) · 3.08 KB
/
bilibiliRelation.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
package main
import (
"encoding/json"
"fmt"
"github.com/moxcomic/ihttp"
log "github.com/sirupsen/logrus"
"github.com/ysmood/gson"
"strconv"
"time"
)
type relationStat struct {
Code int `json:"code"`
Message string `json:"message"`
Ttl int `json:"ttl"`
Data struct {
Mid int `json:"mid"`
Following int `json:"following"`
Whisper int `json:"whisper"`
Black int `json:"black"`
Follower int `json:"follower"`
} `json:"data"`
}
func initListen() {
listLen := len(v.GetStringSlice("listen.list"))
for i := 0; i < listLen; i++ {
uid := v.GetInt(fmt.Sprint("listen.list.", i, ".uid"))
target := v.GetInt(fmt.Sprint("listen.list.", i, ".target"))
groupsRaw := v.GetStringSlice(fmt.Sprint("listen.list.", i, ".group"))
groups := []int{}
for i2, groupRaw := range groupsRaw {
g, err := strconv.Atoi(groupRaw)
if err != nil {
log.Error("[Relation] listen.list.", i, ".group.", i2, " 格式错误: ", groupRaw)
continue
}
groups = append(groups, g)
}
usersRaw := v.GetStringSlice(fmt.Sprint("listen.list.", i, ".user"))
users := []int{}
for i2, userRaw := range usersRaw {
g, err := strconv.Atoi(userRaw)
if err != nil {
log.Error("[Relation] listen.list.", i, ".group.", i2, " 格式错误: ", userRaw)
continue
}
users = append(users, g)
}
go listenFollowers(uid, target, groups, users)
}
}
func listenFollowers(uid any, target int, groups, users []int) {
log.Debug("[Relation] listen followers of ", uid, " til ", target, " for ", groups)
i := ihttp.New().WithUrl("https://api.bilibili.com/x/relation/stat?vmid=1954091502").
WithAddQuerys(map[string]any{"vmid": uid}).
WithHeaders(iheaders).WithCookie(biliIdentity.Cookie)
lastStat := &relationStat{}
for {
resp, err := i.Get().ToBytes()
if err != nil {
log.Error("[Relation] stat of ", uid, " 请求失败: ", err)
time.Sleep(time.Second * 10)
continue
}
stat := &relationStat{}
err = json.Unmarshal(resp, stat)
if err != nil {
log.Error(
"[Relation] stat of ", uid, " 反序列化出错: ", err,
"\n resp: ", BytesToString(resp),
"\n Unmarshal by gson: ", gson.New(resp).JSON("", ""),
)
time.Sleep(time.Second * 10)
continue
}
if stat.Code != 0 {
log.Error("[Relation] stat of ", uid, " 响应出错: ", *stat)
time.Sleep(time.Second * 10)
continue
}
last, now := lastStat.Data.Follower, stat.Data.Follower
if last != now && last != 0 {
change := func() string {
if change := now - last; change > 0 {
return fmt.Sprint(" (+", change, ")")
} else if change < 0 {
return fmt.Sprint(" (", change, ")")
}
return ""
}()
msg := fmt.Sprint("[Relation] ", uid, " 粉丝数: ", last, " -> ", now, change)
log.Info(msg)
}
if stat.Data.Follower >= target {
msg := fmt.Sprint(
"[Relation] uid ", uid,
" 在 ", time.Now().Format("2006/01/02-15/04/05"),
" 达到 ", target, " 粉丝数",
)
log.Info(msg)
bot.Log2SU.Info(msg)
bot.SendPrivateMsgs(users, msg)
bot.SendGroupMsgs(groups, msg)
return
}
lastStat = stat
time.Sleep(time.Second)
}
}