Skip to content

Commit

Permalink
✨ 修复b站小程序解析 (#895)
Browse files Browse the repository at this point in the history
  • Loading branch information
guohuiyuan authored May 4, 2024
1 parent 2a820cf commit 0e3c025
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 41 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.20

require (
github.com/Baidu-AIP/golang-sdk v1.1.1
github.com/FloatTech/AnimeAPI v1.7.1-0.20240502092440-2cf39e6d08c1
github.com/FloatTech/AnimeAPI v1.7.1-0.20240504155718-e1a4844e94a5
github.com/FloatTech/floatbox v0.0.0-20240504085530-b2994112ec50
github.com/FloatTech/gg v1.1.3-0.20230226151425-6ea91286ba08
github.com/FloatTech/imgfactory v0.2.2-0.20230413152719-e101cc3606ef
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ github.com/Baidu-AIP/golang-sdk v1.1.1 h1:RQsAmgDSAkiq22I6n7XJ2t3afgzFeqjY46FGhv
github.com/Baidu-AIP/golang-sdk v1.1.1/go.mod h1:bXnGw7xPeKt8aF7UCELKrV6UZ/46spItONK1RQBQj1Y=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/FloatTech/AnimeAPI v1.7.1-0.20240502092440-2cf39e6d08c1 h1:jCHd5jhWyycMoCKh4Sjx4oyoRv99uB7ZuRrjeA3ZErk=
github.com/FloatTech/AnimeAPI v1.7.1-0.20240502092440-2cf39e6d08c1/go.mod h1:Ru6q5pZUnfMg1iu0M1Hp73q9N3LNIbDr16kjkzyG6Xk=
github.com/FloatTech/AnimeAPI v1.7.1-0.20240504155718-e1a4844e94a5 h1:0Xgngi3sWMnnvdl5PZapu4sWP5fp7ftmYMy5eIGKMdM=
github.com/FloatTech/AnimeAPI v1.7.1-0.20240504155718-e1a4844e94a5/go.mod h1:Ru6q5pZUnfMg1iu0M1Hp73q9N3LNIbDr16kjkzyG6Xk=
github.com/FloatTech/floatbox v0.0.0-20240504085530-b2994112ec50 h1:1D9BS5nI8UFQ8dg3QJicntfGbtL5WdZsKm/OMyBd/lM=
github.com/FloatTech/floatbox v0.0.0-20240504085530-b2994112ec50/go.mod h1:OzGLhvmtz1TKIdGaJDd8pQumvD36UqK+dWsiCISmzQQ=
github.com/FloatTech/gg v1.1.3-0.20230226151425-6ea91286ba08 h1:dPLeoiTVSBlgls+66EB/UJ2e38BaASmBN5nANaycSBU=
Expand Down
50 changes: 46 additions & 4 deletions plugin/bilibili/bilibili_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@
package bilibili

import (
"encoding/json"
"fmt"
"net/http"
"regexp"
"strings"
"time"

bz "github.com/FloatTech/AnimeAPI/bilibili"
"github.com/FloatTech/floatbox/web"
ctrl "github.com/FloatTech/zbpctrl"
"github.com/FloatTech/zbputils/control"
"github.com/FloatTech/zbputils/ctxext"
Expand All @@ -32,10 +37,11 @@ func init() {
Brief: "b站链接解析",
Help: "例:- t.bilibili.com/642277677329285174\n- bilibili.com/read/cv17134450\n- bilibili.com/video/BV13B4y1x7pS\n- live.bilibili.com/22603245 ",
})
en.OnRegex(`((b23|acg).tv|bili2233.cn)/[0-9a-zA-Z]+`).SetBlock(true).Limit(limit.LimitByGroup).
en.OnRegex(`((b23|acg).tv|bili2233.cn)\\?/[0-9a-zA-Z]+`).SetBlock(true).Limit(limit.LimitByGroup).
Handle(func(ctx *zero.Ctx) {
url := ctx.State["regex_matched"].([]string)[0]
realurl, err := bz.GetRealURL("https://" + url)
u := ctx.State["regex_matched"].([]string)[0]
u = strings.ReplaceAll(u, "\\", "")
realurl, err := bz.GetRealURL("https://" + u)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
Expand Down Expand Up @@ -76,9 +82,10 @@ func handleVideo(ctx *zero.Ctx) {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
summaryMsg, err := getVideoSummary(card)
summaryMsg, err := getVideoSummary(cfg, card)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
ctx.SendChain(msg...)
return
}
msg = append(msg, summaryMsg...)
Expand Down Expand Up @@ -111,3 +118,38 @@ func handleLive(ctx *zero.Ctx) {
}
ctx.SendChain(liveCard2msg(card)...)
}

// getVideoSummary AI视频总结
func getVideoSummary(cookiecfg *bz.CookieConfig, card bz.Card) (msg []message.MessageSegment, err error) {
var (
data []byte
videoSummary bz.VideoSummary
)
data, err = web.RequestDataWithHeaders(web.NewDefaultClient(), bz.SignURL(fmt.Sprintf(bz.VideoSummaryURL, card.BvID, card.CID, card.Owner.Mid)), "GET", func(req *http.Request) error {
if cookiecfg != nil {
cookie := ""
cookie, err = cookiecfg.Load()
if err != nil {
return err
}
req.Header.Add("cookie", cookie)
}
req.Header.Set("User-Agent", ua)
return nil
}, nil)
if err != nil {
return
}
err = json.Unmarshal(data, &videoSummary)
msg = make([]message.MessageSegment, 0, 16)
msg = append(msg, message.Text("已为你生成视频总结\n\n"))
msg = append(msg, message.Text(videoSummary.Data.ModelResult.Summary, "\n\n"))
for _, v := range videoSummary.Data.ModelResult.Outline {
msg = append(msg, message.Text("● ", v.Title, "\n"))
for _, p := range v.PartOutline {
msg = append(msg, message.Text(fmt.Sprintf("%d:%d %s\n", p.Timestamp/60, p.Timestamp%60, p.Content)))
}
msg = append(msg, message.Text("\n"))
}
return
}
26 changes: 0 additions & 26 deletions plugin/bilibili/card2msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package bilibili

import (
"encoding/json"
"fmt"
"time"

bz "github.com/FloatTech/AnimeAPI/bilibili"
"github.com/FloatTech/floatbox/binary"
"github.com/FloatTech/floatbox/web"
"github.com/wdvxdr1123/ZeroBot/message"
)

Expand Down Expand Up @@ -326,27 +324,3 @@ func videoCard2msg(card bz.Card) (msg []message.MessageSegment, err error) {
bz.VURL, card.BvID, "\n\n"))
return
}

// getVideoSummary AI视频总结
func getVideoSummary(card bz.Card) (msg []message.MessageSegment, err error) {
var (
data []byte
videoSummary bz.VideoSummary
)
data, err = web.GetData(bz.SignURL(fmt.Sprintf(bz.VideoSummaryURL, card.BvID, card.CID)))
if err != nil {
return
}
err = json.Unmarshal(data, &videoSummary)
msg = make([]message.MessageSegment, 0, 16)
msg = append(msg, message.Text("已为你生成视频总结\n\n"))
msg = append(msg, message.Text(videoSummary.Data.ModelResult.Summary, "\n\n"))
for _, v := range videoSummary.Data.ModelResult.Outline {
msg = append(msg, message.Text("● ", v.Title, "\n"))
for _, p := range v.PartOutline {
msg = append(msg, message.Text(fmt.Sprintf("%d:%d %s\n", p.Timestamp/60, p.Timestamp%60, p.Content)))
}
msg = append(msg, message.Text("\n"))
}
return
}
8 changes: 0 additions & 8 deletions plugin/bilibili/card2msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,6 @@ func TestVideoInfo(t *testing.T) {
t.Log(videoCard2msg(card))
}

func TestGetVideoSummary(t *testing.T) {
card, err := bz.GetVideoInfo("BV1mF411j7iU")
if err != nil {
t.Fatal(err)
}
t.Log(getVideoSummary(card))
}

func TestLiveRoomInfo(t *testing.T) {
card, err := bz.GetLiveRoomInfo("83171")
if err != nil {
Expand Down

0 comments on commit 0e3c025

Please sign in to comment.