From: qydysky Date: Wed, 1 May 2024 15:00:52 +0000 (+0000) Subject: Improve biliapi X-Git-Tag: v0.13.8~4 X-Git-Url: http://127.0.0.1:8081/?a=commitdiff_plain;h=ff5e6426d0cde0b41c55c0c83f155dab47fddfa3;p=bili_danmu%2F.git Improve biliapi --- diff --git a/CV/Var.go b/CV/Var.go index 239dad9..9efa979 100644 --- a/CV/Var.go +++ b/CV/Var.go @@ -74,15 +74,15 @@ type Common struct { AcceptQn map[int]string `json:"-"` //允许的直播流质量 Qn map[int]string `json:"-"` //全部直播流质量 // StreamType StreamType `json:"streamType"` //当前直播流类型 - AllStreamType map[string]StreamType `json:"-"` //直播流类型 - K_v syncmap.Map `json:"-"` //配置文件 - Log *log.Log_interface `json:"-"` //日志 - Danmu_Main_mq *mq.Msgq `json:"-"` //消息 - ReqPool *pool.Buf[reqf.Req] `json:"-"` //请求池 - SerF *web.WebPath `json:"-"` //web服务处理 - SerLimit *web.Limits `json:"-"` //Web服务连接限制 - StartT time.Time `json:"-"` //启动时间 - Cache syncmap.Map `json:"-"` //缓存 + AllStreamType map[string]StreamType `json:"-"` //直播流类型 + K_v syncmap.Map `json:"-"` //配置文件 + Log *log.Log_interface `json:"-"` //日志 + Danmu_Main_mq *mq.Msgq `json:"-"` //消息 + ReqPool *pool.Buf[reqf.Req] `json:"-"` //请求池 + SerF *web.WebPath `json:"-"` //web服务处理 + SerLimit *web.Limits `json:"-"` //Web服务连接限制 + StartT time.Time `json:"-"` //启动时间 + Cache syncmap.MapExceeded[string, any] `json:"-"` //缓存 } type LiveQn struct { @@ -167,7 +167,7 @@ func (t *Common) Copy() *Common { SerF: t.SerF, SerLimit: t.SerLimit, StartT: t.StartT, - Cache: t.Cache.Copy(), + Cache: *t.Cache.Copy(), } return &c diff --git a/F/api.go b/F/api.go index d7c1880..09a3690 100644 --- a/F/api.go +++ b/F/api.go @@ -33,6 +33,8 @@ import ( qr "github.com/skip2/go-qrcode" ) +const webImg = "webImg" + var apilog = c.C.Log.Base(`api`) var api_limit = limit.New(2, "1s", "30s") //频率限制2次/s,最大等待时间30s @@ -481,7 +483,7 @@ func (c *GetFunc) missRoomId() (missKey []string) { func (c *GetFunc) getRoomBaseInfo() (missKey []string) { fkey := `getRoomBaseInfo` - if v, ok := c.Cache.LoadV(fkey).(cacheItem); ok && v.exceeded.After(time.Now()) { + if _, ok := c.Cache.Load(fkey); ok { return } @@ -507,10 +509,7 @@ func (c *GetFunc) getRoomBaseInfo() (missKey []string) { c.Roomid = res.RoomID } - c.Cache.Store(fkey, cacheItem{ - data: nil, - exceeded: time.Now().Add(time.Second * 2), - }) + c.Cache.Store(fkey, nil, time.Second*2) return } @@ -518,7 +517,7 @@ func (c *GetFunc) getInfoByRoom() (missKey []string) { fkey := `getInfoByRoom` - if v, ok := c.Cache.LoadV(fkey).(cacheItem); ok && v.exceeded.After(time.Now()) { + if _, ok := c.Cache.Load(fkey); ok { return } @@ -547,10 +546,8 @@ func (c *GetFunc) getInfoByRoom() (missKey []string) { c.Locked = res.Locked } - c.Cache.Store(fkey, cacheItem{ - data: nil, - exceeded: time.Now().Add(time.Second * 2), - }) + c.Cache.Store(fkey, nil, time.Second*2) + return } @@ -756,66 +753,66 @@ func (t *GetFunc) Get_guardNum() (missKey []string) { return } -func (t *GetFunc) Info(UpUid int) (J.Info, error) { - fkey := `Info` - - if v, ok := t.Cache.LoadV(fkey).(cacheItem); ok && v.exceeded.After(time.Now()) { - return (v.data).(J.Info), nil - } - - // 超额请求阻塞,超时将取消 - apilog := apilog.Base_add(`Info`) - if api_limit.TO() { - return J.Info{}, os.ErrDeadlineExceeded - } - - query := fmt.Sprintf("mid=%d&token=&platform=web&web_location=1550101", UpUid) - // wbi - if e, queryE := biliApi.Wbi(query); e != nil { - return J.Info{}, e - } else { - query = queryE - } - - // html - { - Cookie := make(map[string]string) - t.Cookie.Range(func(k, v interface{}) bool { - Cookie[k.(string)] = v.(string) - return true - }) - req := t.ReqPool.Get() - defer t.ReqPool.Put(req) - - if err := req.Reqf(reqf.Rval{ - Url: `https://api.bilibili.com/x/space/wbi/acc/info?` + query, - Proxy: t.Proxy, - Timeout: 10 * 1000, - Retry: 2, - Header: map[string]string{ - `Accept`: "application/json, text/plain, */*", - `Cookie`: reqf.Map_2_Cookies_String(Cookie), - }, - }); err != nil { - apilog.L(`E: `, err) - return J.Info{}, err - } - - var info J.Info - - //Info - if e := json.Unmarshal(req.Respon, &info); e != nil { - apilog.L(`E: `, e) - return J.Info{}, e - } - - t.Cache.Store(fkey, cacheItem{ - data: info, - exceeded: time.Now().Add(time.Hour), - }) - return info, nil - } -} +// func (t *GetFunc) Info(UpUid int) (J.Info, error) { +// fkey := `Info` + +// if v, ok := t.Cache.LoadV(fkey).(cacheItem); ok && v.exceeded.After(time.Now()) { +// return (v.data).(J.Info), nil +// } + +// // 超额请求阻塞,超时将取消 +// apilog := apilog.Base_add(`Info`) +// if api_limit.TO() { +// return J.Info{}, os.ErrDeadlineExceeded +// } + +// query := fmt.Sprintf("mid=%d&token=&platform=web&web_location=1550101", UpUid) +// // wbi +// if e, queryE := biliApi.Wbi(query); e != nil { +// return J.Info{}, e +// } else { +// query = queryE +// } + +// // html +// { +// Cookie := make(map[string]string) +// t.Cookie.Range(func(k, v interface{}) bool { +// Cookie[k.(string)] = v.(string) +// return true +// }) +// req := t.ReqPool.Get() +// defer t.ReqPool.Put(req) + +// if err := req.Reqf(reqf.Rval{ +// Url: `https://api.bilibili.com/x/space/wbi/acc/info?` + query, +// Proxy: t.Proxy, +// Timeout: 10 * 1000, +// Retry: 2, +// Header: map[string]string{ +// `Accept`: "application/json, text/plain, */*", +// `Cookie`: reqf.Map_2_Cookies_String(Cookie), +// }, +// }); err != nil { +// apilog.L(`E: `, err) +// return J.Info{}, err +// } + +// var info J.Info + +// //Info +// if e := json.Unmarshal(req.Respon, &info); e != nil { +// apilog.L(`E: `, e) +// return J.Info{}, e +// } + +// t.Cache.Store(fkey, cacheItem{ +// data: info, +// exceeded: time.Now().Add(time.Hour), +// }) +// return info, nil +// } +// } // 调用记录 var boot_Get_cookie funcCtrl.FlashFunc //新的替代旧的 @@ -837,9 +834,10 @@ func (t *GetFunc) Get_cookie() (missKey []string) { `DedeUserID`, }); len(miss) == 0 { biliApi.SetCookies(reqf.Cookies_String_2_List(cookieString)) - if e := biliApi.GetNav(); e != nil { + if e, res := biliApi.GetNav(); e != nil { apilog.L(`E: `, e) } else { + t.Cache.Store(webImg, &res, time.Hour) apilog.L(`I: `, `已登录`) return } @@ -987,16 +985,13 @@ func Get_cookie_by_msg() { } // 牌子字段 -type FansMedalI struct { +// 获取牌子信息 +func Get_list_in_room() (array []struct { TargetID int IsLighted int MedalID int RoomID int -} - -// 获取牌子信息 -func Get_list_in_room() (array []FansMedalI) { - +}) { apilog := apilog.Base_add(`获取牌子`) //验证cookie if missKey := CookieCheck([]string{ @@ -1007,60 +1002,15 @@ func Get_list_in_room() (array []FansMedalI) { apilog.L(`T: `, `Cookie无Key:`, missKey) return } - Cookie := make(map[string]string) - c.C.Cookie.Range(func(k, v interface{}) bool { - Cookie[k.(string)] = v.(string) - return true - }) - - { //获取牌子列表 - var medalList []FansMedalI - r := c.C.ReqPool.Get() - defer c.C.ReqPool.Put(r) - for pageNum := 1; true; pageNum += 1 { - if e := r.Reqf(reqf.Rval{ - Url: `https://api.live.bilibili.com/xlive/app-ucenter/v1/fansMedal/panel?page=` + strconv.Itoa(pageNum) + `&page_size=10`, - Header: map[string]string{ - `Cookie`: reqf.Map_2_Cookies_String(Cookie), - }, - Proxy: c.C.Proxy, - Timeout: 10 * 1000, - Retry: 2, - }); e != nil { - apilog.L(`E: `, e) - return - } - - var res J.FansMedal - - if e := json.Unmarshal(r.Respon, &res); e != nil { - apilog.L(`E: `, e) - } - if res.Code != 0 { - apilog.L(`E: `, `返回code`, res.Code, res.Message) - return - } - - for i := 0; i < len(res.Data.List); i++ { - li := res.Data.List[i] - medalList = append(medalList, FansMedalI{ - TargetID: li.Medal.TargetID, - IsLighted: li.Medal.IsLighted, - MedalID: li.Medal.MedalID, - RoomID: li.RoomInfo.RoomID, - }) - } - - if res.Data.PageInfo.CurrentPage == res.Data.PageInfo.TotalPage { - break - } - - time.Sleep(time.Second) - } - - return medalList + //getHotRank + if err, res := biliApi.GetFansMedal(); err != nil { + apilog.L(`E: `, err) + } else { + return res } + + return } // 获取当前佩戴的牌子 @@ -1076,50 +1026,15 @@ func Get_weared_medal() (item J.GetWearedMedal_Data) { apilog.L(`T: `, `Cookie无Key:`, missKey) return } - Cookie := make(map[string]string) - c.C.Cookie.Range(func(k, v interface{}) bool { - Cookie[k.(string)] = v.(string) - return true - }) - - { //获取 - r := c.C.ReqPool.Get() - defer c.C.ReqPool.Put(r) - if e := r.Reqf(reqf.Rval{ - Url: `https://api.live.bilibili.com/live_user/v1/UserInfo/get_weared_medal`, - Header: map[string]string{ - `Cookie`: reqf.Map_2_Cookies_String(Cookie), - }, - Proxy: c.C.Proxy, - Timeout: 10 * 1000, - Retry: 2, - }); e != nil { - apilog.L(`E: `, e) - return - } - - var res J.GetWearedMedal - if e := json.Unmarshal(r.Respon, &res); e != nil { - apilog.L(`W: `, e) - return - } - - if res.Code != 0 { - apilog.L(`E: `, `返回code`, res.Code, res.Msg) - return - } - - switch res.Data.(type) { - case []interface{}: - default: - if data, err := json.Marshal(res.Data); err == nil { - _ = json.Unmarshal(data, &item) - } - } - return + if err, res := biliApi.GetWearedMedal(); err != nil { + apilog.L(`E: `, err) + } else { + item.Roominfo.RoomID = res.RoomID + item.TargetID = res.TargetID + item.TodayIntimacy = res.TodayIntimacy } - + return } func (c *GetFunc) CheckSwitch_FansMedal() (missKey []string) { @@ -2059,8 +1974,31 @@ func (c *GetFunc) SearchUP(s string) (list []searchresult) { }) query := "page=1&page_size=10&order=online&platform=pc&search_type=live_user&keyword=" + url.PathEscape(s) - // wbi - if e, queryE := biliApi.Wbi(query); e != nil { + + // get nav + vr, loaded, f := c.Cache.LoadOrStore(webImg) + if !loaded { + if e, res := biliApi.GetNav(); e != nil { + apilog.L(`E: `, e) + return + } else { + f(&res, time.Hour) + vr = &res + apilog.L(`I: `, `已登录`) + } + } + + if v, ok := vr.(*struct { + IsLogin bool + WbiImg struct { + ImgURL string + SubURL string + } + }); !ok { + apilog.L(`E: `, `类型错误`) + return + } else if e, queryE := biliApi.Wbi(query, v.WbiImg); e != nil { + // wbi apilog.L(`E: `, e) return } else { diff --git a/F/biliApiInterface.go b/F/biliApiInterface.go index ab5ebef..1c7f9d4 100644 --- a/F/biliApiInterface.go +++ b/F/biliApiInterface.go @@ -71,6 +71,26 @@ type biliApiInter interface { GetDanmuMedalAnchorInfo(uid string, Roomid int) (err error, rface string) GetPopularAnchorRank(uid, upUid, roomid int) (err error, note string) GetGuardNum(upUid, roomid int) (err error, GuardNum int) - GetNav() (err error) - Wbi(query string) (err error, queryEnc string) + GetNav() (err error, res struct { + IsLogin bool + WbiImg struct { + ImgURL string + SubURL string + } + }) + Wbi(query string, WbiImg struct { + ImgURL string + SubURL string + }) (err error, queryEnc string) + GetWearedMedal() (err error, res struct { + TodayIntimacy int + RoomID int + TargetID int + }) + GetFansMedal() (err error, res []struct { + TargetID int + IsLighted int + MedalID int + RoomID int + }) } diff --git a/go.mod b/go.mod index f5cb5b1..2bd7824 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.22.2 require ( github.com/gotk3/gotk3 v0.6.3 github.com/mdp/qrterminal/v3 v3.2.0 - github.com/qydysky/part v0.28.20240422183953 + github.com/qydysky/part v0.28.20240501130702 github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 golang.org/x/text v0.14.0 @@ -13,7 +13,7 @@ require ( require ( github.com/google/uuid v1.6.0 - github.com/qydysky/biliApi v0.0.0-20240427073909-0071239f2cde + github.com/qydysky/biliApi v0.0.0-20240501142006-df911ba3ac1f golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f ) diff --git a/go.sum b/go.sum index 6388b7e..dd5a82d 100644 --- a/go.sum +++ b/go.sum @@ -46,10 +46,10 @@ github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdh github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/qydysky/biliApi v0.0.0-20240427073909-0071239f2cde h1:edAOiTiG4WsWqaugVL+qKssIUaeKuwmgqc2f/wUZIaI= -github.com/qydysky/biliApi v0.0.0-20240427073909-0071239f2cde/go.mod h1:nhubuLaNo2LIkgoCt++A5KNtra53ttGPXsahjaGXCl4= -github.com/qydysky/part v0.28.20240422183953 h1:r8kZFzSIqNbFOOuFBJw5aobwLZ/fQ6GaEez5082aJEY= -github.com/qydysky/part v0.28.20240422183953/go.mod h1:VMq3GnrK1/7zUufW3foG5oLAk9dw567JmuMyKPTGswc= +github.com/qydysky/biliApi v0.0.0-20240501142006-df911ba3ac1f h1:n9nyaPuPV2CBSjDk7oLOO3tjzFets9PnNEV31QKpBAo= +github.com/qydysky/biliApi v0.0.0-20240501142006-df911ba3ac1f/go.mod h1:nhubuLaNo2LIkgoCt++A5KNtra53ttGPXsahjaGXCl4= +github.com/qydysky/part v0.28.20240501130702 h1:0riRs+hY628Hd3ohvxAgDN9GL0GpqgPe/G/YIPn8JiQ= +github.com/qydysky/part v0.28.20240501130702/go.mod h1:VMq3GnrK1/7zUufW3foG5oLAk9dw567JmuMyKPTGswc= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI=