Skip to content

Commit 9cb54a3

Browse files
committed
⚡️ 🎨 优化bilibili
1 parent 519ae62 commit 9cb54a3

18 files changed

Lines changed: 456 additions & 886 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.19
44

55
require (
66
github.com/Coloured-glaze/gg v1.3.4
7-
github.com/FloatTech/AnimeAPI v1.5.1-0.20221006135705-daf77c4dedea
7+
github.com/FloatTech/AnimeAPI v1.5.2-0.20221007041953-504c6d0e683b
88
github.com/FloatTech/floatbox v0.0.0-20221004092550-1ebf9b4e6198
99
github.com/FloatTech/sqlite v0.4.0
1010
github.com/FloatTech/ttl v0.0.0-20220715042055-15612be72f5b

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym
22
github.com/Coloured-glaze/gg v1.3.4 h1:l31zIF/HaVwkzjrj+A56RGQoSKyKuR1IWtIrqXGFStI=
33
github.com/Coloured-glaze/gg v1.3.4/go.mod h1:Ih5NLNNDHOy3RJbB0EPqGTreIzq/H02TGThIagh8HJg=
44
github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
5-
github.com/FloatTech/AnimeAPI v1.5.1-0.20221006135705-daf77c4dedea h1:1kZDanLMBknJa2Z+O9t17TNmOENnIYGaPZaGeSw9V5w=
6-
github.com/FloatTech/AnimeAPI v1.5.1-0.20221006135705-daf77c4dedea/go.mod h1:HFQWi7BT966CxSGx6Et/weLaCApsY8FFSbbMPHqEGl0=
5+
github.com/FloatTech/AnimeAPI v1.5.2-0.20221007041953-504c6d0e683b h1:HMNvCoycUJc/+2RdmwuLHyW1KbsuGrF0RQRivVZ2Qzc=
6+
github.com/FloatTech/AnimeAPI v1.5.2-0.20221007041953-504c6d0e683b/go.mod h1:HFQWi7BT966CxSGx6Et/weLaCApsY8FFSbbMPHqEGl0=
77
github.com/FloatTech/floatbox v0.0.0-20221004092550-1ebf9b4e6198 h1:AkwB7LKMK74yS5rIERhOQbJosDgOefJBSkhEiMbyr+A=
88
github.com/FloatTech/floatbox v0.0.0-20221004092550-1ebf9b4e6198/go.mod h1:4UDl6E/I2HqAqRnKdsxxfO28fkqGo3CzFOP2BhyI6ag=
99
github.com/FloatTech/sqlite v0.4.0 h1:fvQ1vc7fw99jYXccs5KItMluy7QL1t6NxbkH7aN1F4g=

plugin/bilibili/api.go

Lines changed: 0 additions & 144 deletions
This file was deleted.

plugin/bilibili/bilibili.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"time"
1818

1919
"github.com/Coloured-glaze/gg"
20+
bz "github.com/FloatTech/AnimeAPI/bilibili"
2021
fcext "github.com/FloatTech/floatbox/ctxext"
2122
"github.com/FloatTech/floatbox/file"
2223
"github.com/FloatTech/floatbox/img/writer"
@@ -40,8 +41,7 @@ var (
4041
4: "进入直播间",
4142
5: "标题变动",
4243
}
43-
cfgFile = "data/Bilibili/config.json"
44-
cfg config
44+
cfg = bz.NewCookieConfig("data/Bilibili/config.json")
4545
)
4646

4747
// 查成分的
@@ -73,7 +73,7 @@ func init() {
7373
engine.OnRegex(`^>user info\s?(.{1,25})$`, getPara).SetBlock(true).
7474
Handle(func(ctx *zero.Ctx) {
7575
id := ctx.State["uid"].(string)
76-
card, err := getMemberCard(id)
76+
card, err := bz.GetMemberCard(id)
7777
if err != nil {
7878
ctx.SendChain(message.Text("ERROR: ", err))
7979
return
@@ -92,7 +92,7 @@ func init() {
9292
Handle(func(ctx *zero.Ctx) {
9393
id := ctx.State["uid"].(string)
9494
// 获取详情
95-
fo, err := getVtbDetail(id)
95+
fo, err := bz.GetVtbDetail(id)
9696
if err != nil {
9797
ctx.SendChain(message.Text("ERROR: ", err))
9898
return
@@ -120,7 +120,7 @@ func init() {
120120
ctx.SendChain(message.Image("file:///" + file.BOTPATH + "/" + drawedFile))
121121
return
122122
}
123-
u, err := getMemberCard(id)
123+
u, err := bz.GetMemberCard(id)
124124
if err != nil {
125125
ctx.SendChain(message.Text("ERROR: ", err))
126126
return
@@ -131,13 +131,13 @@ func init() {
131131
return
132132
}
133133
vupLen := len(vups)
134-
medals, err := getMedalwall(id)
135-
sort.Sort(medalSlice(medals))
134+
medals, err := bz.GetMedalWall(cfg, id)
135+
sort.Sort(bz.MedalSorter(medals))
136136
if err != nil {
137137
ctx.SendChain(message.Text("ERROR: ", err))
138138
}
139139
frontVups := make([]vup, 0)
140-
medalMap := make(map[int64]medal)
140+
medalMap := make(map[int64]bz.Medal)
141141
for _, v := range medals {
142142
up := vup{
143143
Mid: v.Mid,
@@ -173,7 +173,7 @@ func init() {
173173
back = img.Size(back, backX, backY).Im
174174
}
175175
if len(vups) > 50 {
176-
ctx.SendChain(message.Text(u.Name + "关注的up主太多了只展示前50个up"))
176+
ctx.SendChain(message.Text(u.Name + "关注的up主太多了, 只展示前50个up"))
177177
vups = vups[:50]
178178
}
179179
canvas := gg.NewContext(1500, int(500*(1.1+float64(len(vups))/3)))
@@ -275,19 +275,19 @@ func init() {
275275
if pagenum == "" {
276276
pagenum = "0"
277277
}
278-
u, err := getMemberCard(id)
278+
u, err := bz.GetMemberCard(id)
279279
if err != nil {
280280
ctx.SendChain(message.Text("ERROR: ", err))
281281
return
282282
}
283-
var danmaku danmakusuki
283+
var danmaku bz.Danmakusuki
284284
tr := &http.Transport{
285285
DisableKeepAlives: true,
286286
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
287287
}
288288

289289
client := &http.Client{Transport: tr}
290-
data, err := web.RequestDataWith(client, fmt.Sprintf(danmakuAPI, id, pagenum), "GET", "", web.RandUA())
290+
data, err := web.RequestDataWith(client, fmt.Sprintf(bz.DanmakuAPI, id, pagenum), "GET", "", web.RandUA())
291291
if err != nil {
292292
ctx.SendChain(message.Text("ERROR: ", err))
293293
return
@@ -363,7 +363,7 @@ func init() {
363363
canvas.DrawString(u.Mid, 900+n, 122.5)
364364
canvas.DrawString(fmt.Sprintf("粉丝:%d 关注:%d", u.Fans, u.Attention), startWidth, 222.5)
365365
canvas.DrawString(fmt.Sprintf("页码:[%d/%d]", danmaku.Data.PageNum, (danmaku.Data.Total-1)/5), startWidth, 322.5)
366-
canvas.DrawString("网页链接: "+fmt.Sprintf(danmakuURL, u.Mid), startWidth, 422.5)
366+
canvas.DrawString("网页链接: "+fmt.Sprintf(bz.DanmakuURL, u.Mid), startWidth, 422.5)
367367
var channelStart float64
368368
channelStart = float64(550)
369369
for i := 0; i < len(danmaku.Data.Data); i++ {
@@ -527,7 +527,7 @@ func init() {
527527
engine.OnRegex(`^设置b站cookie?\s+(.*)$`, zero.SuperUserPermission).SetBlock(true).
528528
Handle(func(ctx *zero.Ctx) {
529529
cookie := ctx.State["regex_matched"].([]string)[1]
530-
err := setBilibiliCookie(cookie)
530+
err := cfg.Set(cookie)
531531
if err != nil {
532532
ctx.SendChain(message.Text("ERROR: ", err))
533533
return
@@ -571,7 +571,7 @@ func int2rbg(t int64) (int64, int64, int64) {
571571
func getPara(ctx *zero.Ctx) bool {
572572
keyword := ctx.State["regex_matched"].([]string)[1]
573573
if !re.MatchString(keyword) {
574-
searchRes, err := searchUser(keyword)
574+
searchRes, err := bz.SearchUser(cfg, keyword)
575575
if err != nil {
576576
ctx.SendChain(message.Text("ERROR: ", err))
577577
return false
@@ -582,7 +582,7 @@ func getPara(ctx *zero.Ctx) bool {
582582
next := zero.NewFutureEvent("message", 999, false, ctx.CheckSession())
583583
recv, cancel := next.Repeat()
584584
defer cancel()
585-
ctx.SendChain(message.Text("输入为纯数字请选择查询uid还是用户名输入对应序号:\n0. 查询uid\n1. 查询用户名"))
585+
ctx.SendChain(message.Text("输入为纯数字, 请选择查询uid还是用户名, 输入对应序号:\n0. 查询uid\n1. 查询用户名"))
586586
for {
587587
select {
588588
case <-time.After(time.Second * 10):
@@ -604,7 +604,7 @@ func getPara(ctx *zero.Ctx) bool {
604604
ctx.State["uid"] = keyword
605605
return true
606606
} else if num == 1 {
607-
searchRes, err := searchUser(keyword)
607+
searchRes, err := bz.SearchUser(cfg, keyword)
608608
if err != nil {
609609
ctx.SendChain(message.Text("ERROR: ", err))
610610
return false

plugin/bilibili/bilibili_parse.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"regexp"
66
"time"
77

8+
bz "github.com/FloatTech/AnimeAPI/bilibili"
89
ctrl "github.com/FloatTech/zbpctrl"
910
"github.com/FloatTech/zbputils/control"
1011
"github.com/FloatTech/zbputils/ctxext"
@@ -34,7 +35,7 @@ func init() {
3435
en.OnRegex(`((b23|acg).tv|bili2233.cn)/[0-9a-zA-Z]+`).SetBlock(true).Limit(limit.LimitByGroup).
3536
Handle(func(ctx *zero.Ctx) {
3637
url := ctx.State["regex_matched"].([]string)[0]
37-
realurl, err := getrealurl("https://" + url)
38+
realurl, err := bz.GetRealUrl("https://" + url)
3839
if err != nil {
3940
ctx.SendChain(message.Text("ERROR: ", err))
4041
return
@@ -65,7 +66,7 @@ func handleVideo(ctx *zero.Ctx) {
6566
if id == "" {
6667
id = ctx.State["regex_matched"].([]string)[2]
6768
}
68-
card, err := getVideoInfo(id)
69+
card, err := bz.GetVideoInfo(id)
6970
if err != nil {
7071
ctx.SendChain(message.Text("ERROR: ", err))
7172
return
@@ -88,7 +89,7 @@ func handleDynamic(ctx *zero.Ctx) {
8889
}
8990

9091
func handleArticle(ctx *zero.Ctx) {
91-
card, err := getArticleInfo(ctx.State["regex_matched"].([]string)[1])
92+
card, err := bz.GetArticleInfo(ctx.State["regex_matched"].([]string)[1])
9293
if err != nil {
9394
ctx.SendChain(message.Text("ERROR: ", err))
9495
return
@@ -97,7 +98,7 @@ func handleArticle(ctx *zero.Ctx) {
9798
}
9899

99100
func handleLive(ctx *zero.Ctx) {
100-
card, err := getLiveRoomInfo(ctx.State["regex_matched"].([]string)[1])
101+
card, err := bz.GetLiveRoomInfo(ctx.State["regex_matched"].([]string)[1])
101102
if err != nil {
102103
ctx.SendChain(message.Text("ERROR: ", err))
103104
return

plugin/bilibili/bilibilimodel.go

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
package bilibili
22

33
import (
4-
"encoding/json"
5-
"errors"
64
"os"
75

86
"github.com/FloatTech/floatbox/binary"
9-
"github.com/FloatTech/floatbox/file"
107
"github.com/FloatTech/floatbox/web"
118
_ "github.com/fumiama/sqlite3" // use sql
129
"github.com/jinzhu/gorm"
@@ -92,32 +89,3 @@ func updateVup() error {
9289
}
9390
return nil
9491
}
95-
96-
func setBilibiliCookie(cookie string) (err error) {
97-
cfg = config{
98-
BilibiliCookie: cookie,
99-
}
100-
return saveConfig(cfg)
101-
}
102-
103-
func reflushBilibiliCookie() (err error) {
104-
if file.IsNotExist(cfgFile) {
105-
err = errors.New("未初始化配置")
106-
return
107-
}
108-
reader, err := os.Open(cfgFile)
109-
if err != nil {
110-
return
111-
}
112-
defer reader.Close()
113-
return json.NewDecoder(reader).Decode(&cfg)
114-
}
115-
116-
func saveConfig(cfg config) (err error) {
117-
reader, err := os.Create(cfgFile)
118-
if err != nil {
119-
return err
120-
}
121-
defer reader.Close()
122-
return json.NewEncoder(reader).Encode(&cfg)
123-
}

0 commit comments

Comments
 (0)