2
2
package bilibili
3
3
4
4
import (
5
+ "bytes"
5
6
"encoding/json"
6
7
"fmt"
7
8
"net/http"
9
+ "os"
10
+ "os/exec"
8
11
"regexp"
9
12
"strings"
10
13
"time"
11
14
12
15
bz "github.com/FloatTech/AnimeAPI/bilibili"
16
+ "github.com/FloatTech/floatbox/file"
13
17
"github.com/FloatTech/floatbox/web"
14
18
ctrl "github.com/FloatTech/zbpctrl"
15
19
"github.com/FloatTech/zbputils/control"
16
20
"github.com/FloatTech/zbputils/ctxext"
21
+ "github.com/pkg/errors"
17
22
zero "github.com/wdvxdr1123/ZeroBot"
18
23
"github.com/wdvxdr1123/ZeroBot/message"
19
24
)
20
25
21
26
const (
22
- enableHex = 0x10
23
- unableHex = 0x7fffffff_fffffffd
27
+ enableHex = 0x10
28
+ unableHex = 0x7fffffff_fffffffd
29
+ bilibiliparseReferer = "https://www.bilibili.com"
24
30
)
25
31
26
32
var (
33
39
searchDynamicRe = regexp .MustCompile (searchDynamic )
34
40
searchArticleRe = regexp .MustCompile (searchArticle )
35
41
searchLiveRoomRe = regexp .MustCompile (searchLiveRoom )
42
+ cachePath string
36
43
)
37
44
38
45
// 插件主体
@@ -42,6 +49,9 @@ func init() {
42
49
Brief : "b站链接解析" ,
43
50
Help : "例:- t.bilibili.com/642277677329285174\n - bilibili.com/read/cv17134450\n - bilibili.com/video/BV13B4y1x7pS\n - live.bilibili.com/22603245 " ,
44
51
})
52
+ cachePath = en .DataFolder () + "cache/"
53
+ _ = os .RemoveAll (cachePath )
54
+ _ = os .MkdirAll (cachePath , 0755 )
45
55
en .OnRegex (`((b23|acg).tv|bili2233.cn)\\?/[0-9a-zA-Z]+` ).SetBlock (true ).Limit (limit .LimitByGroup ).
46
56
Handle (func (ctx * zero.Ctx ) {
47
57
u := ctx .State ["regex_matched" ].([]string )[0 ]
@@ -126,6 +136,12 @@ func handleVideo(ctx *zero.Ctx) {
126
136
}
127
137
}
128
138
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 ... )
129
145
}
130
146
131
147
func handleDynamic (ctx * zero.Ctx ) {
@@ -189,3 +205,48 @@ func getVideoSummary(cookiecfg *bz.CookieConfig, card bz.Card) (msg []message.Se
189
205
}
190
206
return
191
207
}
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