Skip to content

Commit

Permalink
添加AI视频总结 (#787)
Browse files Browse the repository at this point in the history
* ✨ 添加AI视频总结

* 🐛 添加空格

* 🎨 修改文本格式
  • Loading branch information
guohuiyuan committed Oct 14, 2023
1 parent 844b727 commit 8c24194
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 156 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.6.1-0.20230827161910-7e6a3d93a0ba
github.com/FloatTech/AnimeAPI v1.6.1-0.20231012150753-6cf9415a5645
github.com/FloatTech/floatbox v0.0.0-20230827160415-f0865337a824
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.6.1-0.20230827161910-7e6a3d93a0ba h1:0tqYkIc6RK8P3/cGEMSoBBwhiEHKPJC+1F7xlVkikgY=
github.com/FloatTech/AnimeAPI v1.6.1-0.20230827161910-7e6a3d93a0ba/go.mod h1:6vYu7bW5gPQsBnXB+I6yk+eJQaaAwusoQ/I/wQMwOAI=
github.com/FloatTech/AnimeAPI v1.6.1-0.20231012150753-6cf9415a5645 h1:B1uLcGF+BtXGcMmDkQaFd3Tl8Wpb8xFVNQnxPX+HH34=
github.com/FloatTech/AnimeAPI v1.6.1-0.20231012150753-6cf9415a5645/go.mod h1:6vYu7bW5gPQsBnXB+I6yk+eJQaaAwusoQ/I/wQMwOAI=
github.com/FloatTech/floatbox v0.0.0-20230827160415-f0865337a824 h1:w72fzQg1Y9+VLSRl7iKzaZ6fG3myyMJfpOSajcjaMDM=
github.com/FloatTech/floatbox v0.0.0-20230827160415-f0865337a824/go.mod h1:FwQm6wk+b4wuW54KCKn3zccMX47Q5apnHD/Yakzv0fI=
github.com/FloatTech/gg v1.1.3-0.20230226151425-6ea91286ba08 h1:dPLeoiTVSBlgls+66EB/UJ2e38BaASmBN5nANaycSBU=
Expand Down
8 changes: 7 additions & 1 deletion plugin/bilibili/bilibili_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,17 @@ func handleVideo(ctx *zero.Ctx) {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
summaryMsg, err := getVideoSummary(card)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
msg = append(msg, summaryMsg...)
ctx.SendChain(msg...)
}

func handleDynamic(ctx *zero.Ctx) {
msg, err := dynamicDetail(ctx.State["regex_matched"].([]string)[2])
msg, err := dynamicDetail(cfg, ctx.State["regex_matched"].([]string)[2])
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
Expand Down
124 changes: 4 additions & 120 deletions plugin/bilibili/bilibilipush.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,10 @@ package bilibili

import (
"bytes"
"crypto/md5"
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
"net/url"
"path/filepath"
"sort"
"strconv"
"strings"
"time"

bz "github.com/FloatTech/AnimeAPI/bilibili"
Expand All @@ -21,38 +15,25 @@ import (
ctrl "github.com/FloatTech/zbpctrl"
"github.com/FloatTech/zbputils/control"
"github.com/FloatTech/zbputils/img/text"
"github.com/RomiChan/syncx"
"github.com/pkg/errors"
"github.com/tidwall/gjson"
zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message"
"github.com/wdvxdr1123/ZeroBot/utils/helper"
)

const (
ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36"
referer = "https://space.bilibili.com/%v"
infoURL = "https://api.bilibili.com/x/space/wbi/acc/info?mid=%v&token=&platform=web&web_location=1550101"
navURL = "https://api.bilibili.com/x/web-interface/nav"
)

// bdb bilibili推送数据库
var bdb *bilibilipushdb

var (
lastTime = map[int64]int64{}
liveStatus = map[int64]int{}
upMap = map[int64]string{}
mixinKeyEncTab = [...]int{
46, 47, 18, 2, 53, 8, 23, 32, 15, 50, 10, 31, 58, 3, 45, 35, 27, 43, 5, 49,
33, 9, 42, 19, 29, 28, 14, 39, 12, 38, 41, 13, 37, 48, 7, 16, 24, 55, 40,
61, 26, 17, 0, 1, 60, 51, 30, 4, 22, 25, 54, 21, 56, 59, 6, 63, 57, 62, 11,
36, 20, 34, 44, 52,
}
cache syncx.Map[string, string]
lastUpdateTime time.Time

replacements = [...]string{"!", "'", "(", ")", "*"}
lastTime = map[int64]int64{}
liveStatus = map[int64]int{}
upMap = map[int64]string{}
)

func init() {
Expand Down Expand Up @@ -221,7 +202,7 @@ func changeAtAll(gid int64, b int) (err error) {
func getName(buid int64) (name string, err error) {
var ok bool
if name, ok = upMap[buid]; !ok {
data, err := web.RequestDataWithHeaders(web.NewDefaultClient(), signURL(fmt.Sprintf(infoURL, buid)), "GET", func(r *http.Request) error {
data, err := web.RequestDataWithHeaders(web.NewDefaultClient(), bz.SignURL(fmt.Sprintf(infoURL, buid)), "GET", func(r *http.Request) error {
r.Header.Set("User-Agent", ua)
return nil
}, nil)
Expand All @@ -240,103 +221,6 @@ func getName(buid int64) (name string, err error) {
return
}

func getMixinKey(orig string) string {
var str strings.Builder
t := 0
for _, v := range mixinKeyEncTab {
if v < len(orig) {
str.WriteByte(orig[v])
t++
}
if t > 31 {
break
}
}
return str.String()
}

func wbiSign(params map[string]string, imgKey string, subKey string) map[string]string {
mixinKey := getMixinKey(imgKey + subKey)
currTime := strconv.FormatInt(time.Now().Unix(), 10)
params["wts"] = currTime
// Sort keys
keys := make([]string, 0, len(params))
for k, v := range params {
keys = append(keys, k)
for _, old := range replacements {
v = strings.ReplaceAll(v, old, "")
}
params[k] = v
}
sort.Strings(keys)
h := md5.New()
for k, v := range keys {
h.Write([]byte(v))
h.Write([]byte{'='})
h.Write([]byte(params[v]))
if k < len(keys)-1 {
h.Write([]byte{'&'})
}
}
h.Write([]byte(mixinKey))
params["w_rid"] = hex.EncodeToString(h.Sum(make([]byte, 0, md5.Size)))
return params
}

func getWbiKeysCached() (string, string) {
if time.Since(lastUpdateTime).Minutes() > 10 {
imgKey, subKey := getWbiKeys()
cache.Store("imgKey", imgKey)
cache.Store("subKey", subKey)
lastUpdateTime = time.Now()
return imgKey, subKey
}
imgKeyI, _ := cache.Load("imgKey")
subKeyI, _ := cache.Load("subKey")
return imgKeyI, subKeyI
}

func getWbiKeys() (string, string) {
data, _ := web.RequestDataWithHeaders(web.NewDefaultClient(), navURL, "GET", func(r *http.Request) error {
if cfg != nil {
cookie, err := cfg.Load()
if err == nil {
r.Header.Set("cookie", cookie)
return nil
}
return err
}
return errors.New("未配置-cookie")
}, nil)
json := helper.BytesToString(data)
imgURL := gjson.Get(json, "data.wbi_img.img_url").String()
subURL := gjson.Get(json, "data.wbi_img.sub_url").String()
imgKey := imgURL[strings.LastIndex(imgURL, "/")+1:]
imgKey = strings.TrimSuffix(imgKey, filepath.Ext(imgKey))
subKey := subURL[strings.LastIndex(subURL, "/")+1:]
subKey = strings.TrimSuffix(subKey, filepath.Ext(subKey))
return imgKey, subKey
}

func signURL(urlStr string) string {
urlObj, _ := url.Parse(urlStr)
imgKey, subKey := getWbiKeysCached()
query := urlObj.Query()
params := map[string]string{}
for k, v := range query {
if len(v) > 0 {
params[k] = v[0]
}
}
newParams := wbiSign(params, imgKey, subKey)
for k, v := range newParams {
query.Set(k, v)
}
urlObj.RawQuery = query.Encode()
newURL := urlObj.String()
return newURL
}

// subscribe 订阅
func subscribe(buid, groupid int64) (err error) {
bpMap := map[string]any{
Expand Down
32 changes: 29 additions & 3 deletions plugin/bilibili/card2msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ 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 @@ -253,8 +255,8 @@ func card2msg(dynamicCard *bz.DynamicCard, card *bz.Card, cType int) (msg []mess
}

// dynamicDetail 用动态id查动态信息
func dynamicDetail(dynamicIDStr string) (msg []message.MessageSegment, err error) {
dyc, err := bz.GetDynamicDetail(dynamicIDStr)
func dynamicDetail(cookiecfg *bz.CookieConfig, dynamicIDStr string) (msg []message.MessageSegment, err error) {
dyc, err := bz.GetDynamicDetail(cookiecfg, dynamicIDStr)
if err != nil {
return
}
Expand Down Expand Up @@ -321,6 +323,30 @@ func videoCard2msg(card bz.Card) (msg []message.MessageSegment, err error) {
msg = append(msg, message.Image(card.Pic))
msg = append(msg, message.Text("\n点赞: ", bz.HumanNum(card.Stat.Like), " 投币: ", bz.HumanNum(card.Stat.Coin), "\n",
"收藏: ", bz.HumanNum(card.Stat.Favorite), " 分享: ", bz.HumanNum(card.Stat.Share), "\n",
bz.VURL, card.BvID))
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
}
37 changes: 8 additions & 29 deletions plugin/bilibili/card2msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,6 @@ func TestArticleInfo(t *testing.T) {

}

func TestDynamicDetail(t *testing.T) {
t.Log("cType = 1")
t.Log(dynamicDetail("642279068898689029"))

t.Log("cType = 2")
t.Log(dynamicDetail("642470680290394121"))

t.Log("cType = 2048")
t.Log(dynamicDetail("642277677329285174"))

t.Log("cType = 4")
t.Log(dynamicDetail("642154347357011968"))

t.Log("cType = 8")
t.Log(dynamicDetail("675892999274627104"))

t.Log("cType = 4308")
t.Log(dynamicDetail("668598718656675844"))

t.Log("cType = 64")
t.Log(dynamicDetail("675966082178088963"))

t.Log("cType = 256")
t.Log(dynamicDetail("599253048535707632"))

t.Log("cType = 4,投票类型")
t.Log(dynamicDetail("677231070435868704"))
}

func TestMemberCard(t *testing.T) {
card, err := bz.GetMemberCard(2)
if err != nil {
Expand Down Expand Up @@ -75,6 +46,14 @@ 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 8c24194

Please sign in to comment.