]> 127.0.0.1 Git - bili_danmu/.git/commitdiff
LIVE_BUVID获取方式改变
authorqydysky <qydysky@foxmail.com>
Wed, 17 Feb 2021 16:51:36 +0000 (00:51 +0800)
committerqydysky <qydysky@foxmail.com>
Wed, 17 Feb 2021 16:51:36 +0000 (00:51 +0800)
银瓜子兑换硬币
全局map协程安全

CV/Var.go
F/api.go
F/xinxin.go
Reply/F.go
Reply/Reply.go
Reply/gtk.go
bili_danmu.go
demo/go.mod
demo/go.sum

index 193daa0e68a55e3a0bf01a7a7eba2466df648c4a..80b9d0a6ad16077bf9843f1e2108a6c4bb87ea5d 100644 (file)
--- a/CV/Var.go
+++ b/CV/Var.go
@@ -3,6 +3,7 @@ package cv
 import (
        "time"
        tmplKV "github.com/qydysky/part/tmplKV"
+       syncmap "github.com/qydysky/part/map"
        mq "github.com/qydysky/part/msgq"
        s "github.com/qydysky/part/buf"
        log "github.com/qydysky/part/log"
@@ -52,13 +53,13 @@ var Log = log.New(log.Config{
 })
 
 //k-v
-var K_v =make(map[string]interface{})
+var K_v syncmap.Map
 
 func init() {
        buf := s.New()
        buf.Load("config/config_K_v.json")
        for k,v := range buf.B {
-               K_v[k] = v
+               K_v.Store(k, v)
        }
 }
 
index 1b41a5edea75d19f2a0c7baa7c6bc8690088d70f..9001d53ed51cfbd9db01f8a5209daaf02e0bb3cd 100644 (file)
--- a/F/api.go
+++ b/F/api.go
@@ -49,7 +49,7 @@ func New_api(Roomid int) (o *api) {
 
 func (i *api) Get_info() (o *api) {
        o = i
-       apilog := apilog.Base_add(`获取房号`)       
+       apilog := apilog.Base_add(`获取房号`).L(`T: `, `获取房号`)
 
        if o.Roomid == 0 {
                apilog.L(`E: `,"还未New_api")
@@ -57,7 +57,7 @@ func (i *api) Get_info() (o *api) {
        }
        if api_limit.TO() {return}//超额请求阻塞,超时将取消
 
-       o.Get_LIVE_BUVID()
+       defer o.Get_LIVE_BUVID()
        
        Roomid := strconv.Itoa(o.Roomid)
 
@@ -1057,26 +1057,24 @@ func Dosign() {
 func (i *api) Get_LIVE_BUVID() (o *api){
        o = i
        apilog := apilog.Base_add(`LIVE_BUVID`).L(`T: `,`获取LIVE_BUVID`)
+       if c.Cookie[`LIVE_BUVID`] != `` {apilog.L(`I: `,`存在`);return}
        if len(c.Cookie) == 0 {apilog.L(`E: `,`失败!无cookie`);return}
        if api_limit.TO() {apilog.L(`E: `,`超时!`);return}//超额请求阻塞,超时将取消
 
-       {//获取
+       for {//获取
                req := p.Req()
                if err := req.Reqf(p.Rval{
-                       Url:`https://live.bilibili.com/`+ strconv.Itoa(o.Roomid),
+                       Url:`https://api.live.bilibili.com/rc/v1/Title/webTitles`,
                        Header:map[string]string{
                                `Host`: `api.live.bilibili.com`,
                                `User-Agent`: `Mozilla/5.0 (X11; Linux x86_64; rv:83.0) Gecko/20100101 Firefox/83.0`,
-                               `Accept`: `text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8`,
+                               `Accept`: `application/json, text/plain, */*`,
                                `Accept-Language`: `zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2`,
                                `Accept-Encoding`: `gzip, deflate, br`,
                                `Origin`: `https://live.bilibili.com`,
                                `Connection`: `keep-alive`,
-                               `Pragma`: `no-cache`,
-                               `Cache-Control`: `no-cache`,
-                               `Referer`:"https://live.bilibili.com/all",
-                               `Upgrade-Insecure-Requests`: `1`,
-                               `Cookie`:p.Map_2_Cookies_String(c.Cookie),
+                               `Referer`:"https://live.bilibili.com",
+                               `DNT`: `1`,
                        },
                        Timeout:3,
                        Retry:2,
@@ -1086,10 +1084,26 @@ func (i *api) Get_LIVE_BUVID() (o *api){
                }
 
                //cookie
+               var has bool
                for k,v := range p.Cookies_List_2_Map(req.Response.Cookies()){
                        c.Cookie[k] = v
+                       if k == `LIVE_BUVID` {has = true}
+               }
+               if has {
+                       apilog.L(`I: `,`获取到LIVE_BUVID,保存cookie`)
+                       break
+               } else {
+                       apilog.L(`I: `,`未获取到,重试`)
+                       time.Sleep(time.Second)
                }
        }
+
+       f := p.File()
+       f.FileWR(p.Filel{
+               File: `cookie.txt`,
+               Write: true,
+               Context: []interface{}{p.Map_2_Cookies_String(c.Cookie)},
+       })
        return
 }
 
@@ -1207,7 +1221,25 @@ func (i *api) F_x25Kn() (o *api) {
        }
 
        {//loop
-               for loop_num < 24*5 {
+               for loop_num < (24+2)*5 {
+                       //查看今天小心心数量
+                       if loop_num%5 == 0 {//每5min
+                               {//查看今天小心心数量
+                                       var num = 0
+                                       for _,v := range Gift_list() {
+                                               if v.Gift_id == 30607 && v.Expire_at - int(p.Sys().GetSTime()) > 6 * 86400 {
+                                                       num = v.Gift_num
+                                               }
+                                       }
+                                       if num == 24 {
+                                               apilog.L(`I: `,`今天小心心已满!`);return
+                                       } else {
+                                               apilog.L(`I: `,`获取了今天的第`,num,`个小心心`)
+                                       }
+                               }
+                       }
+                       loop_num += 1
+
                        <- time.After(time.Second*time.Duration(res.Data.Heartbeat_interval))
                        
                        if !c.Bootmap.Check(`api.F_x25Kn`, func_id) {
@@ -1216,8 +1248,6 @@ func (i *api) F_x25Kn() (o *api) {
                        }//有新会话产生,旧的退出
                        func_id = c.Bootmap.Set(`api.F_x25Kn`)//刷新
 
-                       loop_num += 1
-                       
                        var rt_obj = RT{
                                R:R{
                                        Id:`[`+strconv.Itoa(o.Parent_area_id)+`,`+strconv.Itoa(o.Area_id)+`,`+strconv.Itoa(loop_num)+`,`+strconv.Itoa(o.Roomid)+`]`,
@@ -1283,23 +1313,6 @@ func (i *api) F_x25Kn() (o *api) {
                                apilog.L(`E: `,res.Message)
                                return
                        }
-
-                       //查看今天小心心数量
-                       if loop_num%5 == 0 {//每5min
-                               {//查看今天小心心数量
-                                       var num = 0
-                                       for _,v := range Gift_list() {
-                                               if v.Gift_id == 30607 && v.Expire_at - int(p.Sys().GetSTime()) > 6 * 86400 {
-                                                       num = v.Gift_num
-                                               }
-                                       }
-                                       if num == 24 {
-                                               apilog.L(`I: `,`今天小心心已满!`);return
-                                       } else {
-                                               apilog.L(`I: `,`获取了今天的第`,num,`个小心心`)
-                                       }
-                               }
-                       }
                }
        }
        return
@@ -1366,3 +1379,175 @@ func Gift_list() (list []Gift_list_type_Data_List) {
        apilog.L(`I: `,`成功`)
        return res.Data.List
 }
+
+//银瓜子2硬币
+func Silver_2_coin() {
+       apilog := apilog.Base_add(`银瓜子=>硬币`).L(`T: `,`银瓜子=>硬币`)
+       if len(c.Cookie) == 0 {apilog.L(`E: `,`失败!无cookie`);return}
+       if api_limit.TO() {apilog.L(`E: `,`超时!`);return}//超额请求阻塞,超时将取消
+
+       var Silver int
+       {//验证是否还有机会
+               req := p.Req()
+               if err := req.Reqf(p.Rval{
+                       Url:`https://api.live.bilibili.com/pay/v1/Exchange/getStatus`,
+                       Header:map[string]string{
+                               `Host`: `api.live.bilibili.com`,
+                               `User-Agent`: `Mozilla/5.0 (X11; Linux x86_64; rv:83.0) Gecko/20100101 Firefox/83.0`,
+                               `Accept`: `application/json, text/plain, */*`,
+                               `Accept-Language`: `zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2`,
+                               `Accept-Encoding`: `gzip, deflate, br`,
+                               `Origin`: `https://link.bilibili.com`,
+                               `Connection`: `keep-alive`,
+                               `Pragma`: `no-cache`,
+                               `Cache-Control`: `no-cache`,
+                               `Referer`:`https://link.bilibili.com/p/center/index`,
+                               `Cookie`:p.Map_2_Cookies_String(c.Cookie),
+                       },
+                       Timeout:3,
+                       Retry:2,
+               });err != nil {
+                       apilog.L(`E: `,err)
+                       return
+               }
+       
+               var res struct{
+                       Code int `json:"code"`
+                       Msg string `json:"msg"`
+                       Message string `json:"message"`
+                       Data struct{
+                               Silver int `json:"silver"`
+                               Silver_2_coin_left int `json:"silver_2_coin_left"`
+                       } `json:"data"`
+               }
+       
+               if e := json.Unmarshal(req.Respon, &res);e != nil{
+                       apilog.L(`E: `, e)
+                       return
+               }
+       
+               if res.Code != 0{
+                       apilog.L(`E: `, res.Message)
+                       return
+               }
+
+               if res.Data.Silver_2_coin_left == 0{
+                       apilog.L(`I: `, `今天次数已用完`)
+                       return
+               }
+
+               apilog.L(`I: `, `现在有银瓜子`, res.Data.Silver, `个`)
+               Silver = res.Data.Silver
+       }
+
+       {//获取交换规则,验证数量足够
+               req := p.Req()
+               if err := req.Reqf(p.Rval{
+                       Url:`https://api.live.bilibili.com/pay/v1/Exchange/getRule`,
+                       Header:map[string]string{
+                               `Host`: `api.live.bilibili.com`,
+                               `User-Agent`: `Mozilla/5.0 (X11; Linux x86_64; rv:83.0) Gecko/20100101 Firefox/83.0`,
+                               `Accept`: `application/json, text/plain, */*`,
+                               `Accept-Language`: `zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2`,
+                               `Accept-Encoding`: `gzip, deflate, br`,
+                               `Origin`: `https://link.bilibili.com`,
+                               `Connection`: `keep-alive`,
+                               `Pragma`: `no-cache`,
+                               `Cache-Control`: `no-cache`,
+                               `Referer`:`https://link.bilibili.com/p/center/index`,
+                               `Cookie`:p.Map_2_Cookies_String(c.Cookie),
+                       },
+                       Timeout:3,
+                       Retry:2,
+               });err != nil {
+                       apilog.L(`E: `,err)
+                       return
+               }
+       
+               var res struct{
+                       Code int `json:"code"`
+                       Msg string `json:"msg"`
+                       Message string `json:"message"`
+                       Data struct{
+                               Silver_2_coin_price int `json:"silver_2_coin_price"`
+                       } `json:"data"`
+               }
+       
+               if e := json.Unmarshal(req.Respon, &res);e != nil{
+                       apilog.L(`E: `, e)
+                       return
+               }
+       
+               if res.Code != 0{
+                       apilog.L(`E: `, res.Message)
+                       return
+               }
+
+               if Silver < res.Data.Silver_2_coin_price{
+                       apilog.L(`E: `, `当前银瓜子数量不足`)
+                       return
+               }
+       }
+       
+       {//交换
+               csrf := c.Cookie[`bili_jct`]
+               if csrf == `` {apilog.L(`E: `,"Cookie错误,无bili_jct=");return}
+               
+               post_str := `csrf_token=`+csrf+`&csrf=`+csrf
+               req := p.Req()
+               if err := req.Reqf(p.Rval{
+                       Url:`https://api.live.bilibili.com/pay/v1/Exchange/silver2coin`,
+                       PostStr:url.PathEscape(post_str),
+                       Header:map[string]string{
+                               `Host`: `api.live.bilibili.com`,
+                               `User-Agent`: `Mozilla/5.0 (X11; Linux x86_64; rv:83.0) Gecko/20100101 Firefox/83.0`,
+                               `Accept`: `application/json, text/plain, */*`,
+                               `Accept-Language`: `zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2`,
+                               `Accept-Encoding`: `gzip, deflate, br`,
+                               `Origin`: `https://link.bilibili.com`,
+                               `Connection`: `keep-alive`,
+                               `Pragma`: `no-cache`,
+                               `Cache-Control`: `no-cache`,
+                               `Content-Type`: `application/x-www-form-urlencoded`,
+                               `Referer`:`https://link.bilibili.com/p/center/index`,
+                               `Cookie`:p.Map_2_Cookies_String(c.Cookie),
+                       },
+                       Timeout:3,
+                       Retry:2,
+               });err != nil {
+                       apilog.L(`E: `,err)
+                       return
+               }
+       
+               save_cookie(req.Response.Cookies())
+
+               var res struct{
+                       Code int `json:"code"`
+                       Msg string `json:"msg"`
+                       Message string `json:"message"`
+               }
+       
+               if e := json.Unmarshal(req.Respon, &res);e != nil{
+                       apilog.L(`E: `, e)
+                       return
+               }
+       
+               if res.Code != 0{
+                       apilog.L(`E: `, res.Message)
+                       return
+               }
+               apilog.L(`I: `, res.Message)
+       }
+}
+
+func save_cookie(Cookies []*http.Cookie){
+       for k,v := range p.Cookies_List_2_Map(Cookies){
+               c.Cookie[k] = v
+       }
+       f := p.File()
+       f.FileWR(p.Filel{
+               File: `cookie.txt`,
+               Write: true,
+               Context: []interface{}{p.Map_2_Cookies_String(c.Cookie)},
+       })
+}
\ No newline at end of file
index f824a37c5eede98d97a7730a58429bf2879a4a52..95320907e636b2a77a480ce3ed625b973ba280ca 100644 (file)
@@ -49,7 +49,7 @@ var (
 func init() {
        go func() {
                for {
-                       v,ok := c.K_v["get_xiao_xinxin"]
+                       v,ok := c.K_v.Load("get_xiao_xinxin")
                        if !ok {
                                time.Sleep(time.Second)
                                continue
index 8b22c9eb1dee0f7a7ff7aaeaad9673ac4aac1f8b..3864150beef637ab46367053a4413e4dff2c49c9 100644 (file)
@@ -736,7 +736,7 @@ func init(){
 }
 
 func Save_to_json(Loc int,Context []interface{}) {
-       if path,ok := c.K_v[`save_to_json`].(string);ok && path != ``{
+       if path,ok := c.K_v.LoadV(`save_to_json`).(string);ok && path != ``{
                p.File().FileWR(p.Filel{
                        File:path,
                        Write:true,
@@ -748,7 +748,7 @@ func Save_to_json(Loc int,Context []interface{}) {
 
 //进入房间发送弹幕
 func Entry_danmu(){
-       if s,ok := c.K_v[`进入房间发送弹幕`].(string);ok && s != ``{
+       if s,ok := c.K_v.LoadV(`进入房间发送弹幕`).(string);ok && s != ``{
                send.Danmu_s(s,p.Map_2_Cookies_String(c.Cookie),c.Roomid)
        }
 }
\ No newline at end of file
index 0a60e14d7fd5225881ed38ab8f4e593f54d1f9da..d95d6c3286e752b4886d406b0551bccd8294f2d6 100644 (file)
@@ -185,13 +185,13 @@ func (replyF) user_toast_msg(s string){
                if uid != 0 {
                        c.Danmu_Main_mq.Push_tag(`pm`,send.Pm_item{
                                Uid:int(uid.(float64)),
-                               Msg:c.K_v[`上舰私信`].(string),
+                               Msg:c.K_v.LoadV(`上舰私信`).(string),
                        })//上舰私信
                }
-               if c.K_v[`额外私信对象`].(float64) != 0 {
+               if c.K_v.LoadV(`额外私信对象`).(float64) != 0 {
                        c.Danmu_Main_mq.Push_tag(`pm`,send.Pm_item{
-                               Uid:int(c.K_v[`额外私信对象`].(float64)),
-                               Msg:c.K_v[`上舰私信(额外)`].(string),
+                               Uid:int(c.K_v.LoadV(`额外私信对象`).(float64)),
+                               Msg:c.K_v.LoadV(`上舰私信(额外)`).(string),
                        })//上舰私信-对额外
                }
        }
@@ -369,7 +369,7 @@ func (replyF) send_gift(s string){
        //小于设定
        {
                var tmp = 20.0
-               if v,ok := c.K_v[`弹幕_礼物金额显示阈值`];ok {
+               if v,ok := c.K_v.Load(`弹幕_礼物金额显示阈值`);ok {
                        tmp = v.(float64)
                }
                if allprice < tmp {msglog.L(`T: `, sh_log...);return}
@@ -719,13 +719,13 @@ func Msg_showdanmu(auth interface{}, m ...string) {
                        if i,e := strconv.Atoi(m[1]);e == nil {
                                c.Danmu_Main_mq.Push_tag(`pm`,send.Pm_item{
                                        Uid:i,
-                                       Msg:c.K_v[`弹幕私信`].(string),
+                                       Msg:c.K_v.LoadV(`弹幕私信`).(string),
                                })//上舰私信
                        }
-                       if c.K_v[`额外私信对象`].(float64) != 0 {
+                       if c.K_v.LoadV(`额外私信对象`).(float64) != 0 {
                                c.Danmu_Main_mq.Push_tag(`pm`,send.Pm_item{
-                                       Uid:int(c.K_v[`额外私信对象`].(float64)),
-                                       Msg:c.K_v[`弹幕私信(额外)`].(string),
+                                       Uid:int(c.K_v.LoadV(`额外私信对象`).(float64)),
+                                       Msg:c.K_v.LoadV(`弹幕私信(额外)`).(string),
                                })//上舰私信-对额外
                        }
                }
index 15530647bed8a4484a47d08bba6603ded8535bd1..dffa4840eed749701fc03dacca0d90fb251c2d57 100644 (file)
@@ -367,7 +367,7 @@ func Gtk_danmu() {
                                } else {
                                        in_smooth_roll = false
                                        tmp.SetValue(max)
-                                       if v,ok := c.K_v[`gtk_保留弹幕数量`].(float64);ok {
+                                       if v,ok := c.K_v.LoadV(`gtk_保留弹幕数量`).(float64);ok {
                                                loc -= int(v)
                                        } else {
                                                loc -= 25
@@ -512,7 +512,7 @@ func load_face(uid string) (loc string) {
                loc = Gtk_img_path + `/` + uid
                return
        }
-       if v,ok := c.K_v[`gtk_头像获取等待最大数量`].(float64);ok && len(gtkGetList) > int(v) {return}
+       if v,ok := c.K_v.LoadV(`gtk_头像获取等待最大数量`).(float64);ok && len(gtkGetList) > int(v) {return}
        select{
                case gtkGetList <- uid:
                default:
@@ -565,7 +565,7 @@ func show(s,img_src string,to_grid ...int){
                        pixbuf,e = gdk.PixbufNewFromFileAtSize(img_src, 40, 40);
                        if e == nil {
                                imgbuf.Lock()
-                               if v,ok := c.K_v[`gtk_内存头像数量`].(float64);ok && len(imgbuf.b) > int(v) + 10 {
+                               if v,ok := c.K_v.LoadV(`gtk_内存头像数量`).(float64);ok && len(imgbuf.b) > int(v) + 10 {
                                        for k,_ := range imgbuf.b {
                                                delete(imgbuf.b,k)
                                                if len(imgbuf.b) <= int(v) {break}
index 239b8ba438dbc45f28c497e6e399c45390e8b4e0..e7168a8316576c43fc06b6e04ed843a76c58728a 100644 (file)
@@ -155,7 +155,9 @@ func Demo(roomid ...int) {
                        //获取房间相关信息
                        api := F.New_api(c.Roomid).Get_host_Token().Get_live()
                        c.Roomid = api.Roomid
-
+                       
+                       //每日兑换硬币
+                       F.Silver_2_coin()
                        //每日签到
                        F.Dosign()
                        //获取用户版本
@@ -237,6 +239,8 @@ func Demo(roomid ...int) {
                                                                return true
                                                        },
                                                        `new day`:func(data interface{})(bool){//日期更换
+                                                               //每日兑换硬币
+                                                               go F.Silver_2_coin()
                                                                //小心心
                                                                go api.F_x25Kn()
                                                                //每日签到
index 5b91541c11ec9ca5753af40afa438f3333e382e9..dfea8c65da87eba4899e6dffda0dc8277c4a69cc 100644 (file)
@@ -11,7 +11,7 @@ require (
        github.com/miekg/dns v1.1.38 // indirect
        github.com/mitchellh/mapstructure v1.4.1 // indirect
        github.com/qydysky/bili_danmu v0.5.7
-       github.com/qydysky/part v0.3.15 // indirect
+       github.com/qydysky/part v0.4.2 // indirect
        github.com/shirou/gopsutil v3.21.1+incompatible // indirect
        github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e // indirect
        github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 // indirect
index 37379bdd402789499c9cd9736919102ccbefd7c4..0a5ae03727f62cac1f5b6009beff6913ee0ec1ed 100644 (file)
@@ -141,6 +141,8 @@ github.com/qydysky/part v0.3.14 h1:A73pP/LqScwOuh9eEAaOb1y+CFLPr4A4eev+8Jv5Q9o=
 github.com/qydysky/part v0.3.14/go.mod h1:43opuciW71sZvOR67kye50jgMDSDrn/t6+LefNdlXPg=
 github.com/qydysky/part v0.3.15 h1:dRdGUfMk3XMR88jE0xx54FoKwsz6e5Vh4gSFuY7oxpc=
 github.com/qydysky/part v0.3.15/go.mod h1:43opuciW71sZvOR67kye50jgMDSDrn/t6+LefNdlXPg=
+github.com/qydysky/part v0.4.2 h1:8guI/1YEwVsjTWuIPJ1euiSQbHYejWft4Tuhp8CVjAE=
+github.com/qydysky/part v0.4.2/go.mod h1:43opuciW71sZvOR67kye50jgMDSDrn/t6+LefNdlXPg=
 github.com/qydysky/part/msgq v0.0.0-20201213031129-ca3253dc72ad h1:Jtzf509lQrkUMGTV0Sc6IDCAiR1VrBcHrIban7hpye4=
 github.com/qydysky/part/msgq v0.0.0-20201213031129-ca3253dc72ad/go.mod h1:w32TkJNVtTJd4LOS09cq+4uYG6itcN2vsqw+slp44Rg=
 github.com/qydysky/part/msgq v0.0.0-20201213120821-f36e49c32bba h1:1ew9dRpc0Rux0WkWeT/4AE15ynYWmL2D7onJEJIFOB8=