Skip to content

Commit 4b90a06

Browse files
authored
feat(bilibili): 添加视频下载 (#1157)
1 parent 109b766 commit 4b90a06

File tree

4 files changed

+85
-10
lines changed

4 files changed

+85
-10
lines changed

go.mod

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

55
require (
66
github.com/Baidu-AIP/golang-sdk v1.1.1
7-
github.com/FloatTech/AnimeAPI v1.7.1-0.20250217140215-4856397458c9
7+
github.com/FloatTech/AnimeAPI v1.7.1-0.20250423082452-e16339a3962c
88
github.com/FloatTech/floatbox v0.0.0-20241106130736-5aea0a935024
99
github.com/FloatTech/gg v1.1.3
1010
github.com/FloatTech/imgfactory v0.2.2-0.20230413152719-e101cc3606ef

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
github.com/Baidu-AIP/golang-sdk v1.1.1 h1:RQsAmgDSAkiq22I6n7XJ2t3afgzFeqjY46FGhvrx4cw=
22
github.com/Baidu-AIP/golang-sdk v1.1.1/go.mod h1:bXnGw7xPeKt8aF7UCELKrV6UZ/46spItONK1RQBQj1Y=
33
github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
4-
github.com/FloatTech/AnimeAPI v1.7.1-0.20250217140215-4856397458c9 h1:tI9GgG8fdMK2WazFiEbMXAXjwMCckIfDaXbig9B6DdA=
5-
github.com/FloatTech/AnimeAPI v1.7.1-0.20250217140215-4856397458c9/go.mod h1:XXG1eBJf+eeWacQx5azsQKL5Gg7jDYTFyyZGIa/56js=
4+
github.com/FloatTech/AnimeAPI v1.7.1-0.20250423082452-e16339a3962c h1:bEe8VP2aHLR2NHk1BsBQFtP0XE3cxquvr0tW0CkKcDk=
5+
github.com/FloatTech/AnimeAPI v1.7.1-0.20250423082452-e16339a3962c/go.mod h1:XXG1eBJf+eeWacQx5azsQKL5Gg7jDYTFyyZGIa/56js=
66
github.com/FloatTech/floatbox v0.0.0-20241106130736-5aea0a935024 h1:mrvWpiwfRklt9AyiQjKgDGJjf4YL6FZ3yC+ydbkuF2o=
77
github.com/FloatTech/floatbox v0.0.0-20241106130736-5aea0a935024/go.mod h1:+P3hs+Cvl10/Aj3SNE96TuBvKAXCe+XD1pKphTZyiwk=
88
github.com/FloatTech/gg v1.1.3 h1:+GlL02lTKsxJQr4WCuNwVxC1/eBZrCvypCIBtxuOFb4=

plugin/bilibili/bilibili_parse.go

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,31 @@
22
package bilibili
33

44
import (
5+
"bytes"
56
"encoding/json"
67
"fmt"
78
"net/http"
9+
"os"
10+
"os/exec"
811
"regexp"
912
"strings"
1013
"time"
1114

1215
bz "github.com/FloatTech/AnimeAPI/bilibili"
16+
"github.com/FloatTech/floatbox/file"
1317
"github.com/FloatTech/floatbox/web"
1418
ctrl "github.com/FloatTech/zbpctrl"
1519
"github.com/FloatTech/zbputils/control"
1620
"github.com/FloatTech/zbputils/ctxext"
21+
"github.com/pkg/errors"
1722
zero "github.com/wdvxdr1123/ZeroBot"
1823
"github.com/wdvxdr1123/ZeroBot/message"
1924
)
2025

2126
const (
22-
enableHex = 0x10
23-
unableHex = 0x7fffffff_fffffffd
27+
enableHex = 0x10
28+
unableHex = 0x7fffffff_fffffffd
29+
bilibiliparseReferer = "https://www.bilibili.com"
2430
)
2531

2632
var (
@@ -33,6 +39,7 @@ var (
3339
searchDynamicRe = regexp.MustCompile(searchDynamic)
3440
searchArticleRe = regexp.MustCompile(searchArticle)
3541
searchLiveRoomRe = regexp.MustCompile(searchLiveRoom)
42+
cachePath string
3643
)
3744

3845
// 插件主体
@@ -42,6 +49,9 @@ func init() {
4249
Brief: "b站链接解析",
4350
Help: "例:- t.bilibili.com/642277677329285174\n- bilibili.com/read/cv17134450\n- bilibili.com/video/BV13B4y1x7pS\n- live.bilibili.com/22603245 ",
4451
})
52+
cachePath = en.DataFolder() + "cache/"
53+
_ = os.RemoveAll(cachePath)
54+
_ = os.MkdirAll(cachePath, 0755)
4555
en.OnRegex(`((b23|acg).tv|bili2233.cn)\\?/[0-9a-zA-Z]+`).SetBlock(true).Limit(limit.LimitByGroup).
4656
Handle(func(ctx *zero.Ctx) {
4757
u := ctx.State["regex_matched"].([]string)[0]
@@ -126,6 +136,12 @@ func handleVideo(ctx *zero.Ctx) {
126136
}
127137
}
128138
ctx.SendChain(msg...)
139+
downLoadMsg, err := getVideoDownload(cfg, card, cachePath)
140+
if err != nil {
141+
ctx.SendChain(message.Text("ERROR: ", err))
142+
return
143+
}
144+
ctx.SendChain(downLoadMsg...)
129145
}
130146

131147
func handleDynamic(ctx *zero.Ctx) {
@@ -189,3 +205,48 @@ func getVideoSummary(cookiecfg *bz.CookieConfig, card bz.Card) (msg []message.Se
189205
}
190206
return
191207
}
208+
209+
func getVideoDownload(cookiecfg *bz.CookieConfig, card bz.Card, cachePath string) (msg []message.Segment, err error) {
210+
var (
211+
data []byte
212+
videoDownload bz.VideoDownload
213+
stderr bytes.Buffer
214+
)
215+
today := time.Now().Format("20060102")
216+
videoFile := fmt.Sprintf("%s%s%s.mp4", cachePath, card.BvID, today)
217+
if file.IsExist(videoFile) {
218+
msg = append(msg, message.Video("file:///"+file.BOTPATH+"/"+videoFile))
219+
return
220+
}
221+
data, err = web.RequestDataWithHeaders(web.NewDefaultClient(), bz.SignURL(fmt.Sprintf(bz.VideoDownloadURL, card.BvID, card.CID)), "GET", func(req *http.Request) error {
222+
if cookiecfg != nil {
223+
cookie := ""
224+
cookie, err = cookiecfg.Load()
225+
if err != nil {
226+
return err
227+
}
228+
req.Header.Add("cookie", cookie)
229+
}
230+
req.Header.Set("User-Agent", ua)
231+
return nil
232+
}, nil)
233+
if err != nil {
234+
return
235+
}
236+
err = json.Unmarshal(data, &videoDownload)
237+
if err != nil {
238+
return
239+
}
240+
headers := fmt.Sprintf("User-Agent: %s\nReferer: %s", ua, bilibiliparseReferer)
241+
// 限制最多下载8分钟视频
242+
cmd := exec.Command("ffmpeg", "-ss", "0", "-t", "480", "-headers", headers, "-i", videoDownload.Data.Durl[0].URL, "-c", "copy", videoFile)
243+
cmd.Stderr = &stderr
244+
err = cmd.Run()
245+
if err != nil {
246+
err = errors.Errorf("未配置ffmpeg,%v", stderr)
247+
return
248+
}
249+
msg = append(msg, message.Video("file:///"+file.BOTPATH+"/"+videoFile))
250+
return
251+
252+
}

plugin/bilibili/card2msg.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package bilibili
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"time"
67

78
bz "github.com/FloatTech/AnimeAPI/bilibili"
89
"github.com/FloatTech/floatbox/binary"
10+
"github.com/FloatTech/floatbox/web"
911
"github.com/wdvxdr1123/ZeroBot/message"
1012
)
1113

@@ -303,7 +305,10 @@ func liveCard2msg(card bz.RoomCard) (msg []message.Segment) {
303305

304306
// videoCard2msg 视频卡片转消息
305307
func videoCard2msg(card bz.Card) (msg []message.Segment, err error) {
306-
var mCard bz.MemberCard
308+
var (
309+
mCard bz.MemberCard
310+
onlineTotal bz.OnlineTotal
311+
)
307312
msg = make([]message.Segment, 0, 16)
308313
mCard, err = bz.GetMemberCard(card.Owner.Mid)
309314
msg = append(msg, message.Text("标题: ", card.Title, "\n"))
@@ -313,16 +318,25 @@ func videoCard2msg(card bz.Card) (msg []message.Segment, err error) {
313318
}
314319
} else {
315320
if err != nil {
316-
err = nil
317321
msg = append(msg, message.Text("UP主: ", card.Owner.Name, "\n"))
318322
} else {
319323
msg = append(msg, message.Text("UP主: ", card.Owner.Name, " 粉丝: ", bz.HumanNum(mCard.Fans), "\n"))
320324
}
321325
}
322-
msg = append(msg, message.Text("播放: ", bz.HumanNum(card.Stat.View), " 弹幕: ", bz.HumanNum(card.Stat.Danmaku)))
323326
msg = append(msg, message.Image(card.Pic))
324-
msg = append(msg, message.Text("\n点赞: ", bz.HumanNum(card.Stat.Like), " 投币: ", bz.HumanNum(card.Stat.Coin), "\n",
325-
"收藏: ", bz.HumanNum(card.Stat.Favorite), " 分享: ", bz.HumanNum(card.Stat.Share), "\n",
327+
data, err := web.GetData(fmt.Sprintf(bz.OnlineTotalURL, card.BvID, card.CID))
328+
if err != nil {
329+
return
330+
}
331+
err = json.Unmarshal(data, &onlineTotal)
332+
if err != nil {
333+
return
334+
}
335+
msg = append(msg, message.Text("👀播放: ", bz.HumanNum(card.Stat.View), " 💬弹幕: ", bz.HumanNum(card.Stat.Danmaku),
336+
"\n👍点赞: ", bz.HumanNum(card.Stat.Like), " 💰投币: ", bz.HumanNum(card.Stat.Coin),
337+
"\n📁收藏: ", bz.HumanNum(card.Stat.Favorite), " 🔗分享: ", bz.HumanNum(card.Stat.Share),
338+
"\n📝简介: ", card.Desc,
339+
"\n🏄‍♂️ 总共 ", onlineTotal.Data.Total, " 人在观看,", onlineTotal.Data.Count, " 人在网页端观看\n",
326340
bz.VURL, card.BvID, "\n\n"))
327341
return
328342
}

0 commit comments

Comments
 (0)