Skip to content

Commit 8ae591d

Browse files
committed
feat(bilibili): move methods back from zbp
1 parent 180d338 commit 8ae591d

7 files changed

Lines changed: 537 additions & 62 deletions

File tree

bilibili/api.go

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/FloatTech/floatbox/binary"
1212
"github.com/FloatTech/floatbox/web"
1313
"github.com/tidwall/gjson"
14+
"github.com/wdvxdr1123/ZeroBot/message"
1415
)
1516

1617
// ErrAPINeedCookie ...
@@ -20,15 +21,15 @@ var ErrAPINeedCookie = errors.New("api need cookie")
2021
var Ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36"
2122

2223
// SearchUser 查找b站用户
23-
func SearchUser(cookiecfg *CookieConfig, keyword string) (r []SearchResult, err error) {
24+
func (cfg *CookieConfig) SearchUser(keyword string) (r []SearchResult, err error) {
2425
client := &http.Client{}
2526
req, err := http.NewRequest("GET", fmt.Sprintf(SearchUserURL, keyword), nil)
2627
if err != nil {
2728
return
2829
}
29-
if cookiecfg != nil {
30+
if cfg != nil {
3031
cookie := ""
31-
cookie, err = cookiecfg.Load()
32+
cookie, err = cfg.Load()
3233
if err != nil {
3334
return
3435
}
@@ -76,12 +77,12 @@ func LoadDynamicDetail(str string) (card DynamicCard, err error) {
7677
}
7778

7879
// GetDynamicDetail 用动态id查动态信息
79-
func GetDynamicDetail(cookiecfg *CookieConfig, dynamicIDStr string) (card DynamicCard, err error) {
80+
func (cfg *CookieConfig) GetDynamicDetail(dynamicIDStr string) (card DynamicCard, err error) {
8081
var data []byte
8182
data, err = web.RequestDataWithHeaders(web.NewDefaultClient(), fmt.Sprintf(DynamicDetailURL, dynamicIDStr), "GET", func(req *http.Request) error {
82-
if cookiecfg != nil {
83+
if cfg != nil {
8384
cookie := ""
84-
cookie, err = cookiecfg.Load()
85+
cookie, err = cfg.Load()
8586
if err != nil {
8687
return err
8788
}
@@ -107,15 +108,15 @@ func GetMemberCard(uid any) (result MemberCard, err error) {
107108
}
108109

109110
// GetMedalWall 用b站uid获得牌子
110-
func GetMedalWall(cookiecfg *CookieConfig, uid string) (result []Medal, err error) {
111+
func (cfg *CookieConfig) GetMedalWall(uid string) (result []Medal, err error) {
111112
client := &http.Client{}
112113
req, err := http.NewRequest("GET", fmt.Sprintf(MedalWallURL, uid), nil)
113114
if err != nil {
114115
return
115116
}
116-
if cookiecfg != nil {
117+
if cfg != nil {
117118
cookie := ""
118-
cookie, err = cookiecfg.Load()
119+
cookie, err = cfg.Load()
119120
if err != nil {
120121
return
121122
}
@@ -199,12 +200,12 @@ func GetVideoInfo(id string) (card Card, err error) {
199200
if err != nil {
200201
return
201202
}
202-
err = json.Unmarshal(binary.StringToBytes(gjson.ParseBytes(data).Get("data").Raw), &card)
203+
err = json.Unmarshal(binary.StringToBytes(gjson.ParseBytes(data).Raw), &card)
203204
return
204205
}
205206

206207
// GetVideoSummary 用av或bv查看AI视频总结
207-
func GetVideoSummary(cookiecfg *CookieConfig, id string) (videoSummary VideoSummary, err error) {
208+
func (cfg *CookieConfig) GetVideoSummary(id string) (videoSummary VideoSummary, err error) {
208209
var (
209210
data []byte
210211
card Card
@@ -223,9 +224,9 @@ func GetVideoSummary(cookiecfg *CookieConfig, id string) (videoSummary VideoSumm
223224
return
224225
}
225226
data, err = web.RequestDataWithHeaders(web.NewDefaultClient(), SignURL(fmt.Sprintf(VideoSummaryURL, card.BvID, card.CID, card.Owner.Mid)), "GET", func(req *http.Request) error {
226-
if cookiecfg != nil {
227+
if cfg != nil {
227228
cookie := ""
228-
cookie, err = cookiecfg.Load()
229+
cookie, err = cfg.Load()
229230
if err != nil {
230231
return err
231232
}
@@ -240,3 +241,12 @@ func GetVideoSummary(cookiecfg *CookieConfig, id string) (videoSummary VideoSumm
240241
err = json.Unmarshal(data, &videoSummary)
241242
return
242243
}
244+
245+
// GetDetailMessage 用动态id查动态信息
246+
func (cfg *CookieConfig) GetDetailMessage(dynamicIDStr string) (msg []message.Segment, err error) {
247+
dyc, err := cfg.GetDynamicDetail(dynamicIDStr)
248+
if err != nil {
249+
return
250+
}
251+
return dyc.ToMessage()
252+
}

bilibili/api_test.go

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,56 @@ func TestGetAllGuard(t *testing.T) {
1414

1515
func TestGetDynamicDetail(t *testing.T) {
1616
cfg := NewCookieConfig("config.json")
17-
detail, err := GetDynamicDetail(cfg, "851252197280710664")
17+
detail, err := cfg.GetDynamicDetail("851252197280710664")
1818
if err != nil {
1919
t.Fatal(err)
2020
}
2121
t.Logf("%+v\n", detail)
2222
}
23+
24+
func TestArticleInfo(t *testing.T) {
25+
card, err := GetArticleInfo("17279244")
26+
if err != nil {
27+
t.Fatal(err)
28+
}
29+
t.Log(card.ToArticleMessage("17279244"))
30+
}
31+
32+
func TestMemberCard(t *testing.T) {
33+
card, err := GetMemberCard(2)
34+
if err != nil {
35+
t.Fatal(err)
36+
}
37+
t.Logf("%+v\n", card)
38+
}
39+
40+
func TestVideoInfo(t *testing.T) {
41+
card, err := GetVideoInfo("10007")
42+
if err != nil {
43+
t.Fatal(err)
44+
}
45+
t.Log(card.ToVideoMessage())
46+
card, err = GetVideoInfo("BV1xx411c7mD")
47+
if err != nil {
48+
t.Fatal(err)
49+
}
50+
t.Log(card.ToVideoMessage())
51+
card, err = GetVideoInfo("bv1xx411c7mD")
52+
if err != nil {
53+
t.Fatal(err)
54+
}
55+
t.Log(card.ToVideoMessage())
56+
card, err = GetVideoInfo("BV1mF411j7iU")
57+
if err != nil {
58+
t.Fatal(err)
59+
}
60+
t.Log(card.ToVideoMessage())
61+
}
62+
63+
func TestLiveRoomInfo(t *testing.T) {
64+
card, err := GetLiveRoomInfo("83171", "b_ut=7;buvid3=0;i-wanna-go-back=-1;innersign=0;")
65+
if err != nil {
66+
t.Fatal(err)
67+
}
68+
t.Log(card.ToMessage())
69+
}

bilibili/ctx.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package bilibili
2+
3+
import (
4+
"regexp"
5+
"strconv"
6+
"time"
7+
8+
zero "github.com/wdvxdr1123/ZeroBot"
9+
"github.com/wdvxdr1123/ZeroBot/message"
10+
)
11+
12+
var re = regexp.MustCompile(`^\d+$`)
13+
14+
func RequireUser(cfg *CookieConfig) func(ctx *zero.Ctx) bool {
15+
return func(ctx *zero.Ctx) bool {
16+
keyword := ctx.State["regex_matched"].([]string)[1]
17+
if !re.MatchString(keyword) {
18+
searchRes, err := cfg.SearchUser(keyword)
19+
if err != nil {
20+
ctx.SendChain(message.Text("ERROR: ", err))
21+
return false
22+
}
23+
ctx.State["uid"] = strconv.FormatInt(searchRes[0].Mid, 10)
24+
return true
25+
}
26+
next := zero.NewFutureEvent("message", 999, false, ctx.CheckSession())
27+
recv, cancel := next.Repeat()
28+
defer cancel()
29+
ctx.SendChain(message.Text("输入为纯数字, 请选择查询uid还是用户名, 输入对应序号:\n0. 查询uid\n1. 查询用户名"))
30+
for {
31+
select {
32+
case <-time.After(time.Second * 10):
33+
ctx.SendChain(message.Text("时间太久啦!", zero.BotConfig.NickName[0], "帮你选择查询uid"))
34+
ctx.State["uid"] = keyword
35+
return true
36+
case c := <-recv:
37+
msg := c.Event.Message.ExtractPlainText()
38+
num, err := strconv.Atoi(msg)
39+
if err != nil {
40+
ctx.SendChain(message.Text("请输入数字!"))
41+
continue
42+
}
43+
if num < 0 || num > 1 {
44+
ctx.SendChain(message.Text("序号非法!"))
45+
continue
46+
}
47+
if num == 0 {
48+
ctx.State["uid"] = keyword
49+
return true
50+
} else if num == 1 {
51+
searchRes, err := cfg.SearchUser(keyword)
52+
if err != nil {
53+
ctx.SendChain(message.Text("ERROR: ", err))
54+
return false
55+
}
56+
ctx.State["uid"] = strconv.FormatInt(searchRes[0].Mid, 10)
57+
return true
58+
}
59+
}
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)