]> 127.0.0.1 Git - bili_danmu/.git/commitdiff
自动切换粉丝牌,api限频
authorqydysky <qydysky@foxmail.com>
Sun, 20 Dec 2020 13:22:59 +0000 (21:22 +0800)
committerqydysky <qydysky@foxmail.com>
Sun, 20 Dec 2020 13:22:59 +0000 (21:22 +0800)
CV/Var.go
F/api.go
Reply/gtk.go
bili_danmu.go

index db5066840bf3cbc41653ffc3f223996a71a90754..aef157dc7a6ec0c8d5f0fee2602005b9f893542e 100644 (file)
--- a/CV/Var.go
+++ b/CV/Var.go
@@ -12,7 +12,7 @@ var (
        Roomid int
        Cookie string
        Title string//直播标题
-       Uname string//主播id
+       Uname string//主播
        Rev float64//营收
        Renqi int//人气
        GuardNum int//舰长数
index 60be5101cb6dbf9bc25897505eb3a458c0fd5d8a..ee61ec695953be1dfe0f3e1a967a5324606802df 100644 (file)
--- a/F/api.go
+++ b/F/api.go
@@ -26,6 +26,8 @@ type api struct {
 }
 
 var apilog = p.Logf().New().Base(-1, "api.go").Level(2)
+var api_limit = p.Limit(1,2000,15000)//频率限制1次/2s,最大等待时间15s
+
 func New_api(Roomid int) (o *api) {
        apilog.Base(-1, "新建")
        defer apilog.Base(0)
@@ -47,6 +49,8 @@ func (i *api) Get_info() (o *api) {
                apilog.E("还未New_api")
                return
        }
+       if api_limit.TO() {return}//超额请求阻塞,超时将取消
+
        Roomid := strconv.Itoa(o.Roomid)
 
        r := g.Get(p.Rval{
@@ -149,6 +153,7 @@ func (i *api) Get_live(qn ...string) (o *api) {
                apilog.E("还未New_api")
                return
        }
+       if api_limit.TO() {return}//超额请求阻塞,超时将取消
 
        Cookie := c.Cookie
        if i := strings.Index(Cookie, "PVID="); i != -1 {
@@ -323,6 +328,7 @@ func (i *api) Get_host_Token() (o *api) {
                return
        }
        Roomid := strconv.Itoa(o.Roomid)
+       if api_limit.TO() {return}//超额请求阻塞,超时将取消
 
 
        req := p.Req()
@@ -370,6 +376,7 @@ func (i *api) Get_host_Token() (o *api) {
 
 func Get_face_src(uid string) (string) {
        if uid == "" {return ""}
+       if api_limit.TO() {return ""}//超额请求阻塞,超时将取消
 
        req := p.Req()
        if err := req.Reqf(p.Rval{
@@ -402,6 +409,8 @@ func (i *api) Get_OnlineGoldRank() {
                apilog.Base(1, "Get_OnlineGoldRank").E("i.Uid == 0 || c.Roomid == 0")
                return
        }
+       if api_limit.TO() {return}//超额请求阻塞,超时将取消
+
        var session_roomid = c.Roomid
        var self_loop func(page int)
        self_loop = func(page int){
@@ -481,13 +490,12 @@ func (i *api) Get_OnlineGoldRank() {
        return
 }
 
-var guard_num_get_limit = p.Limit(1,1000,2000)//频率限制1次/1s,最大等待时间2s
 func (i *api) Get_guardNum() {
        if i.Uid == 0 || c.Roomid == 0 {
                apilog.Base(1, "Get_guardNum").E("i.Uid == 0 || c.Roomid == 0")
                return
        }
-       if guard_num_get_limit.TO() {return}//超额请求阻塞,超时将取消
+       if api_limit.TO() {return}//超额请求阻塞,超时将取消
 
        req := p.Req()
        if err := req.Reqf(p.Rval{
@@ -527,6 +535,7 @@ func (i *api) Get_guardNum() {
 
 func (i *api) Get_Version() {
        Roomid := strconv.Itoa(i.Roomid)
+       if api_limit.TO() {return}//超额请求阻塞,超时将取消
 
        var player_js_url string
        {//获取player_js_url
@@ -581,12 +590,16 @@ func (i *api) Get_Version() {
 }
 
 func (i *api) Get_cookie() {
+       if api_limit.TO() {return}//超额请求阻塞,超时将取消
+
        var img_url string
        var oauth string
        {//获取二维码
                r := p.Req()
                if e := r.Reqf(p.Rval{
                        Url:`https://passport.bilibili.com/qrcode/getLoginUrl`,
+                       Timeout:10,
+                       Retry:2,
                });e != nil {
                        apilog.Base(1,`Get_cookie`).E(e)
                        return
@@ -646,6 +659,8 @@ func (i *api) Get_cookie() {
                                        `Content-Type`:`application/x-www-form-urlencoded; charset=UTF-8`,
                                        `Referer`: `https://passport.bilibili.com/login`,
                                },
+                               Timeout:10,
+                               Retry:2,        
                        });e != nil {
                                apilog.Base(1,`Get_cookie`).E(e)
                                return
@@ -697,4 +712,106 @@ func (i *api) Get_cookie() {
                        return
                }
        }
+}
+
+func (i *api) Switch_FansMedal() {
+       if c.Cookie == `` {return}
+       if api_limit.TO() {return}//超额请求阻塞,超时将取消
+
+       {//验证是否本直播间牌子
+               r := p.Req()
+               if e := r.Reqf(p.Rval{
+                       Url:`https://api.live.bilibili.com/live_user/v1/UserInfo/get_weared_medal`,
+                       Header:map[string]string{
+                               `Cookie`:c.Cookie,
+                       },
+                       Timeout:10,
+                       Retry:2,
+               });e != nil {
+                       apilog.Base(1,`Switch_FansMedal`).E(e)
+                       return
+               }
+               res := string(r.Respon)
+               if v,ok := p.Json().GetValFromS(res, "data.roominfo.room_id").(float64);ok && int(v) == c.Roomid {
+                       return
+               }
+       }
+       var medal_id int
+       {//获取牌子列表
+               r := p.Req()
+               if e := r.Reqf(p.Rval{
+                       Url:`https://api.live.bilibili.com/fans_medal/v1/FansMedal/get_list_in_room`,
+                       Header:map[string]string{
+                               `Cookie`:c.Cookie,
+                       },
+                       Timeout:10,
+                       Retry:2,
+               });e != nil {
+                       apilog.Base(1,`Switch_FansMedal`).E(e)
+                       return
+               }
+               res := string(r.Respon)
+               if v,ok := p.Json().GetValFromS(res, "code").(float64);!ok || v != 0 {
+                       apilog.Base(1,`Switch_FansMedal`).E(`Get_FansMedal get_list_in_room code`, v)
+                       return
+               } else {
+                       if v,ok := p.Json().GetValFromS(res, "data").([]interface{});ok{
+                               for _,item := range v {
+                                       if room_id,ok := p.Json().GetValFrom(item, "room_id").(float64);!ok || int(room_id) != c.Roomid {
+                                               continue
+                                       } else {
+                                               if tmp_medal_id,ok := p.Json().GetValFrom(item, "medal_id").(float64);!ok {
+                                                       apilog.Base(1,`Switch_FansMedal`).E(`medal_id error`)
+                                                       return
+                                               } else {
+                                                       medal_id = int(tmp_medal_id)
+                                                       break
+                                               }
+                                       }
+                               }
+                       } else {
+                               apilog.Base(1,`Switch_FansMedal`).E(`data error`)
+                               return
+                       }
+               }
+               if medal_id == 0 {return}
+       }
+       {//切换牌子
+               var csrf string
+               if i := strings.Index(c.Cookie, "bili_jct="); i == -1 {
+                       apilog.Base(1,`Switch_FansMedal`).E("Cookie错误,无bili_jct=")
+                       return
+               } else {
+                       if d := strings.Index(c.Cookie[i + 9:], ";"); d == -1 {
+                               csrf = c.Cookie[i + 9:]
+                       } else {
+                               csrf = c.Cookie[i + 9:][:d]
+                       }
+               }
+               r := p.Req()
+               if e := r.Reqf(p.Rval{
+                       Url:`https://api.live.bilibili.com/xlive/web-room/v1/fansMedal/wear`,
+                       PostStr:`medal_id=`+strconv.Itoa(medal_id)+`&csrf_token=`+csrf+`&csrf=`+csrf,
+                       Header:map[string]string{
+                               `Cookie`:c.Cookie,
+                               `Content-Type`:`application/x-www-form-urlencoded; charset=UTF-8`,
+                               `Referer`: `https://passport.bilibili.com/login`,
+                       },
+                       Timeout:10,
+                       Retry:2,
+               });e != nil {
+                       apilog.Base(1,`Switch_FansMedal`).E(e)
+                       return
+               }
+               res := string(r.Respon)
+               if v,ok := p.Json().GetValFromS(res, "code").(float64);ok && v == 0 {
+                       apilog.Base(1,`Switch_FansMedal`).W(`自动切换粉丝牌`)
+                       return
+               }
+               if v,ok := p.Json().GetValFromS(res, "message").(string);ok {
+                       apilog.Base(1,`Switch_FansMedal`).E(`Get_FansMedal wear message`, v)
+               } else {
+                       apilog.Base(1,`Switch_FansMedal`).E(`Get_FansMedal wear message nil`)
+               }
+       }
 }
\ No newline at end of file
index ca0fb6486ef503665d6e088de37f6d062bc59780..0036a2c90ca046a7e1509f2a9efb10b0690cd711 100644 (file)
@@ -544,7 +544,7 @@ func Gtk_danmu() {
                        }
                        {//房间id
                                if !w2_Entry0_editting {
-                                       if t,e := w2_Entry0.GetText();e == nil && t == `` && c.Roomid != 0{
+                                       if t,e := w2_Entry0.GetText();e == nil && t != strconv.Itoa(c.Roomid) {//未编辑时,显示为长id
                                                w2_Entry0.SetText(strconv.Itoa(c.Roomid))
                                        }
                                }
index 7f7f1e697627c6a135613aee6b5001b6d2246b9d..0ecb6ba91797b9fea8ac9203c1be52d516727ee8 100644 (file)
@@ -93,6 +93,8 @@ func Demo(roomid ...int) {
                for !exit_sign {
                        //获取房间相关信息
                        api := F.New_api(c.Roomid).Get_host_Token().Get_live()
+                       c.Roomid = api.Roomid
+
                        //获取cookies
                        {
                                var q = p.Filel{
@@ -118,6 +120,8 @@ func Demo(roomid ...int) {
                        }
                        //获取用户版本
                        api.Get_Version()
+                       //切换粉丝牌,只在cookie存在时启用
+                       api.Switch_FansMedal()
                        if len(api.Url) == 0 || api.Roomid == 0 || api.Token == "" || api.Uid == 0 || api.Locked {
                                danmulog.E("some err")
                                return
@@ -134,7 +138,7 @@ func Demo(roomid ...int) {
                                //SendChan 传入发送[]byte
                                //RecvChan 接收[]byte
                                danmulog.I("连接", v)
-                               ws.SendChan <- F.HelloGen(api.Roomid, api.Token)
+                               ws.SendChan <- F.HelloGen(c.Roomid, api.Token)
                                if F.HelloChe(<- ws.RecvChan) {
                                        danmulog.I("已连接到房间", c.Uname, `(`, c.Roomid, `)`)
                                        reply.Gui_show(`进入直播间: `+c.Uname+` (`+strconv.Itoa(c.Roomid)+`)`, `0room`)
@@ -150,7 +154,6 @@ func Demo(roomid ...int) {
                                                ws.Heartbeat(1000 * heartinterval, heartbeatmsg)
 
                                                //传输变量,以便响应弹幕"弹幕机在么"
-                                               c.Roomid = api.Roomid
                                                c.Live = api.Live
                                                //获取过往营收 舰长数量
                                                // go api.Get_OnlineGoldRank()//高能榜显示的是在线观众的打赏