Fix bilibilipush 20240404 (#884)

*  添加cookie

*  添加签名

* 🐛 修改嵌套错误

* 🐛 优化infoURL

*  添加cookie
This commit is contained in:
himawari 2024-04-05 13:03:17 +08:00 committed by GitHub
parent cf0e87ab9a
commit d7cdfb1544
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -24,7 +24,7 @@ import (
const ( 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" 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" referer = "https://space.bilibili.com/%v"
infoURL = "https://api.bilibili.com/x/space/wbi/acc/info?mid=%v&token=&platform=web&web_location=1550101" infoURL = "https://api.bilibili.com/x/space/wbi/acc/info?mid=%v"
) )
// bdb bilibili推送数据库 // bdb bilibili推送数据库
@ -77,7 +77,7 @@ func init() {
en.OnRegex(`^添加[B|b]站订阅\s?(.{1,25})$`, zero.UserOrGrpAdmin, getPara).SetBlock(true).Handle(func(ctx *zero.Ctx) { en.OnRegex(`^添加[B|b]站订阅\s?(.{1,25})$`, zero.UserOrGrpAdmin, getPara).SetBlock(true).Handle(func(ctx *zero.Ctx) {
buid, _ := strconv.ParseInt(ctx.State["uid"].(string), 10, 64) buid, _ := strconv.ParseInt(ctx.State["uid"].(string), 10, 64)
name, err := getName(buid) name, err := getName(buid, cfg)
if err != nil || name == "" { if err != nil || name == "" {
ctx.SendChain(message.Text("ERROR: ", err)) ctx.SendChain(message.Text("ERROR: ", err))
return return
@ -95,7 +95,7 @@ func init() {
en.OnRegex(`^取消[B|b]站订阅\s?(.{1,25})$`, zero.UserOrGrpAdmin, getPara).SetBlock(true).Handle(func(ctx *zero.Ctx) { en.OnRegex(`^取消[B|b]站订阅\s?(.{1,25})$`, zero.UserOrGrpAdmin, getPara).SetBlock(true).Handle(func(ctx *zero.Ctx) {
buid, _ := strconv.ParseInt(ctx.State["uid"].(string), 10, 64) buid, _ := strconv.ParseInt(ctx.State["uid"].(string), 10, 64)
name, err := getName(buid) name, err := getName(buid, cfg)
if err != nil { if err != nil {
ctx.SendChain(message.Text("ERROR: ", err)) ctx.SendChain(message.Text("ERROR: ", err))
return return
@ -112,7 +112,7 @@ func init() {
}) })
en.OnRegex(`^取消[B|b]站动态订阅\s?(.{1,25})$`, zero.UserOrGrpAdmin, getPara).SetBlock(true).Handle(func(ctx *zero.Ctx) { en.OnRegex(`^取消[B|b]站动态订阅\s?(.{1,25})$`, zero.UserOrGrpAdmin, getPara).SetBlock(true).Handle(func(ctx *zero.Ctx) {
buid, _ := strconv.ParseInt(ctx.State["uid"].(string), 10, 64) buid, _ := strconv.ParseInt(ctx.State["uid"].(string), 10, 64)
name, err := getName(buid) name, err := getName(buid, cfg)
if err != nil { if err != nil {
ctx.SendChain(message.Text("ERROR: ", err)) ctx.SendChain(message.Text("ERROR: ", err))
return return
@ -133,7 +133,7 @@ func init() {
if gid == 0 { if gid == 0 {
gid = -ctx.Event.UserID gid = -ctx.Event.UserID
} }
name, err := getName(buid) name, err := getName(buid, cfg)
if err != nil { if err != nil {
ctx.SendChain(message.Text("ERROR: ", err)) ctx.SendChain(message.Text("ERROR: ", err))
return return
@ -200,10 +200,18 @@ func changeAtAll(gid int64, b int) (err error) {
} }
// 取得uid的名字 // 取得uid的名字
func getName(buid int64) (name string, err error) { func getName(buid int64, cookiecfg *bz.CookieConfig) (name string, err error) {
var ok bool var ok bool
if name, ok = upMap[buid]; !ok { if name, ok = upMap[buid]; !ok {
data, err := web.RequestDataWithHeaders(web.NewDefaultClient(), bz.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 {
if cookiecfg != nil {
cookie := ""
cookie, err = cookiecfg.Load()
if err != nil {
return err
}
r.Header.Add("Cookie", cookie)
}
r.Header.Set("User-Agent", ua) r.Header.Set("User-Agent", ua)
return nil return nil
}, nil) }, nil)
@ -262,8 +270,19 @@ func unsubscribeLive(buid, groupid int64) (err error) {
return bdb.insertOrUpdateLiveAndDynamic(bpMap) return bdb.insertOrUpdateLiveAndDynamic(bpMap)
} }
func getUserDynamicCard(buid int64) (cardList []gjson.Result, err error) { func getUserDynamicCard(buid int64, cookiecfg *bz.CookieConfig) (cardList []gjson.Result, err error) {
data, err := web.RequestDataWith(web.NewDefaultClient(), fmt.Sprintf(bz.SpaceHistoryURL, buid, 0), "GET", referer, ua, nil) data, err := web.RequestDataWithHeaders(web.NewDefaultClient(), fmt.Sprintf(bz.SpaceHistoryURL, buid, 0), "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.Add("User-Agent", ua)
return nil
}, nil)
if err != nil { if err != nil {
return return
} }
@ -289,7 +308,7 @@ func sendDynamic(ctx *zero.Ctx) error {
uids := bdb.getAllBuidByDynamic() uids := bdb.getAllBuidByDynamic()
for _, buid := range uids { for _, buid := range uids {
time.Sleep(2 * time.Second) time.Sleep(2 * time.Second)
cardList, err := getUserDynamicCard(buid) cardList, err := getUserDynamicCard(buid, cfg)
if err != nil { if err != nil {
return err return err
} }