22package bilibili
33
44import (
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
2126const (
22- enableHex = 0x10
23- unableHex = 0x7fffffff_fffffffd
27+ enableHex = 0x10
28+ unableHex = 0x7fffffff_fffffffd
29+ bilibiliparseReferer = "https://www.bilibili.com"
2430)
2531
2632var (
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
131147func 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\n Referer: %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+ }
0 commit comments