Skip to content

Commit

Permalink
update bilibili
Browse files Browse the repository at this point in the history
  • Loading branch information
Sora233 committed May 24, 2023
1 parent cb8e78c commit 7cd0e3b
Show file tree
Hide file tree
Showing 14 changed files with 514 additions and 360 deletions.
19 changes: 11 additions & 8 deletions lsp/bilibili/XSpaceAccInfo.go
Expand Up @@ -12,14 +12,15 @@ import (
)

const (
PathXSpaceAccInfo = "/x/space/acc/info"
PathXSpaceAccInfo = "/x/space/wbi/acc/info"
)

type XSpaceAccInfoRequest struct {
Mid int64 `json:"mid"`
Platform string `json:"platform"`
Jsonp string `json:"jsonp"`
Token string `json:"token"`
Mid int64 `json:"mid"`
Platform string `json:"platform"`
Jsonp string `json:"jsonp"`
Token string `json:"token"`
WebLocation string `json:"web_location"`
}

var cj atomic.Pointer[cookiejar.Jar]
Expand Down Expand Up @@ -47,13 +48,15 @@ func XSpaceAccInfo(mid int64) (*XSpaceAccInfoResponse, error) {
}()
url := BPath(PathXSpaceAccInfo)
params, err := utils.ToParams(&XSpaceAccInfoRequest{
Mid: mid,
Platform: "web",
Jsonp: "jsonp",
Mid: mid,
Platform: "web",
Jsonp: "jsonp",
WebLocation: "1550101",
})
if err != nil {
return nil, err
}
signWbi(params)
var opts = []requests.Option{
requests.ProxyOption(proxy_pool.PreferNone),
requests.TimeoutOption(time.Second * 15),
Expand Down
67 changes: 62 additions & 5 deletions lsp/bilibili/XWebInterfaceNav.go
@@ -1,35 +1,92 @@
package bilibili

import (
"crypto/md5"
"encoding/hex"
"fmt"
"github.com/Sora233/DDBOT/proxy_pool"
"github.com/Sora233/DDBOT/requests"
"github.com/Sora233/DDBOT/utils"
"github.com/guonaihong/gout"
"github.com/samber/lo"
"sort"
"strings"
"time"
)

const PathXWebInterfaceNav = "/x/web-interface/nav"

func XWebInterfaceNav() (*WebInterfaceNavResponse, error) {
if !IsVerifyGiven() {
func refreshNavWbi() {
resp, err := XWebInterfaceNav(false)
if err != nil {
logger.Errorf("bilibili: refreshNavWbi error %v", err)
return
}
wbiImg := resp.GetData().GetWbiImg()
if wbiImg != nil {
wbi.Store(wbiImg)
}
logger.Trace("bilibili: refreshNavWbi ok")
}

func getWbi() (imgKey string, subKey string) {
wbi := wbi.Load()
getKey := func(url string) string {
path, _ := lo.Last(strings.Split(url, "/"))
key := strings.Split(path, ".")[0]
return key
}
imgKey = getKey(wbi.ImgUrl)
subKey = getKey(wbi.SubUrl)
return
}

func signWbi(params gout.H) {
imgKey, subKey := getWbi()
var sb strings.Builder
var orig = imgKey + subKey
for _, r := range mixinKeyEncTab {
sb.WriteRune(rune(orig[r]))
}
salt := sb.String()[:32]
params["wts"] = time.Now().Unix()
p := lo.MapToSlice(params, func(key string, value any) lo.Tuple2[string, any] {
return lo.Tuple2[string, any]{A: key, B: value}
})
sort.Slice(p, func(i, j int) bool {
return p[i].A < p[j].A
})

query := strings.Join(lo.Map(p, func(item lo.Tuple2[string, any], _ int) string {
return fmt.Sprintf("%v=%v", item.A, item.B)
}), "&")
hash := md5.New()
hash.Write([]byte(query + salt))
wbiSign := hex.EncodeToString(hash.Sum(nil))
params["w_rid"] = wbiSign
}

func XWebInterfaceNav(login bool) (*WebInterfaceNavResponse, error) {
if login && !IsVerifyGiven() {
return nil, ErrVerifyRequired
}
st := time.Now()
defer func() {
ed := time.Now()
logger.WithField("FuncName", utils.FuncName()).Tracef("cost %v", ed.Sub(st))
}()
url := BPath(PathXWebInterfaceNav)
path := BPath(PathXWebInterfaceNav)
var opts = []requests.Option{
requests.ProxyOption(proxy_pool.PreferNone),
requests.TimeoutOption(time.Second * 15),
AddUAOption(),
delete412ProxyOption,
}
if getVerify() != nil {
if login && getVerify() != nil {
opts = append(opts, getVerify().VerifyOpts...)
}
xwin := new(WebInterfaceNavResponse)
err := requests.Get(url, nil, xwin, opts...)
err := requests.Get(path, nil, xwin, opts...)
if err != nil {
return nil, err
}
Expand Down
6 changes: 4 additions & 2 deletions lsp/bilibili/XWebInterfaceNav_test.go
Expand Up @@ -6,6 +6,8 @@ import (
)

func TestXWebInterfaceNav(t *testing.T) {
_, err := XWebInterfaceNav()
assert.NotNil(t, err)
resp, err := XWebInterfaceNav(false)
assert.Nil(t, err)
assert.NotNil(t, resp.GetData().GetWbiImg().GetImgUrl())
assert.NotNil(t, resp.GetData().GetWbiImg().GetSubUrl())
}
11 changes: 9 additions & 2 deletions lsp/bilibili/bilibili.go
Expand Up @@ -62,6 +62,7 @@ var (
username string
password string
accountUid atomic.Int64
wbi atomic.Pointer[WebInterfaceNavResponse_Data_WbiImg]
delete412ProxyOption = func() requests.Option {
return requests.ProxyCallbackOption(func(out interface{}, proxy string) {
if out == nil {
Expand All @@ -72,6 +73,12 @@ var (
}
})
}()
mixinKeyEncTab = []rune{
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,
}
)

func Init() {
Expand Down Expand Up @@ -208,7 +215,7 @@ func GetVerifyInfo() *VerifyInfo {
}

func FreshSelfInfo() {
navResp, err := XWebInterfaceNav()
navResp, err := XWebInterfaceNav(true)
if err != nil {
logger.Errorf("获取个人信息失败 - %v,b站功能可能无法使用", err)
} else {
Expand Down Expand Up @@ -236,7 +243,7 @@ func FreshSelfInfo() {
}

func AddUAOption() requests.Option {
return requests.AddUAOption()
return requests.AddRandomUAOption(requests.Computer)
}

func AddReferOption(refer ...string) requests.Option {
Expand Down

0 comments on commit 7cd0e3b

Please sign in to comment.