]> 127.0.0.1 Git - bili_danmu/.git/commitdiff
cookie使用map储存 小心心
authorqydysky <qydysky@foxmail.com>
Wed, 27 Jan 2021 07:27:57 +0000 (15:27 +0800)
committerqydysky <qydysky@foxmail.com>
Wed, 27 Jan 2021 07:27:57 +0000 (15:27 +0800)
CV/Var.go
F/api.go
Reply/F.go
Reply/Reply.go
Send/Send.go
Send/Send_pm.go
bili_danmu.go

index c3341d822929453b151ff9ab077509d2f0c6cedc..739097c7c8df069b180ee6fe0ec0a1f3fedcc717 100644 (file)
--- a/CV/Var.go
+++ b/CV/Var.go
@@ -13,7 +13,7 @@ var (
        Live []string//直播链接
        Live_qn string
        Roomid int
-       Cookie string
+       Cookie = make(map[string]string)
        Title string//直播标题
        Uname string//主播名
        Rev float64//营收
index 3a934e064c954baa1fc97d882aa2bfe7cedc3317..973aa95758e528e100fd79314ea2ce5ea7c9b761 100644 (file)
--- a/F/api.go
+++ b/F/api.go
@@ -10,6 +10,7 @@ import (
     "context"
        "encoding/json"
        "net/http"
+       "net/url"
 
        qr "github.com/skip2/go-qrcode"
        c "github.com/qydysky/bili_danmu/CV"
@@ -50,6 +51,8 @@ func (i *api) Get_info() (o *api) {
        }
        if api_limit.TO() {return}//超额请求阻塞,超时将取消
 
+       o.Get_LIVE_BUVID()
+
        Roomid := strconv.Itoa(o.Roomid)
 
        r := g.Get(p.Rval{
@@ -94,7 +97,7 @@ func (i *api) Get_info() (o *api) {
                        Url:"https://api.live.bilibili.com/xlive/web-room/v1/index/getInfoByRoom?room_id=" + Roomid,
                        Header:map[string]string{
                                `Referer`:"https://live.bilibili.com/" + Roomid,
-                               `Cookie`:c.Cookie,
+                               `Cookie`:p.Map_2_Cookies_String(c.Cookie),
                        },
                        Timeout:10,
                        Retry:2,
@@ -188,7 +191,7 @@ func (i *api) Get_live(qn ...string) (o *api) {
        }
        if api_limit.TO() {return}//超额请求阻塞,超时将取消
 
-       Cookie := c.Cookie
+       Cookie := p.Map_2_Cookies_String(c.Cookie)
        if i := strings.Index(Cookie, "PVID="); i != -1 {
                if d := strings.Index(Cookie[i:], ";"); d == -1 {
                        Cookie = Cookie[:i]
@@ -368,7 +371,7 @@ func (i *api) Get_host_Token() (o *api) {
                Url:"https://api.live.bilibili.com/xlive/web-room/v1/index/getDanmuInfo?type=0&id=" + Roomid,
                Header:map[string]string{
                        `Referer`:"https://live.bilibili.com/" + Roomid,
-                       `Cookie`:c.Cookie,
+                       `Cookie`:p.Map_2_Cookies_String(c.Cookie),
                },
                Timeout:10,
                Retry:2,
@@ -417,7 +420,7 @@ func Get_face_src(uid string) (string) {
                Url:"https://api.live.bilibili.com/xlive/web-room/v1/index/getDanmuMedalAnchorInfo?ruid=" + uid,
                Header:map[string]string{
                        `Referer`:"https://live.bilibili.com/" + strconv.Itoa(c.Roomid),
-                       `Cookie`:c.Cookie,
+                       `Cookie`:p.Map_2_Cookies_String(c.Cookie),
                },
                Timeout:10,
                Retry:2,
@@ -466,7 +469,7 @@ func (i *api) Get_OnlineGoldRank() {
                                `Pragma`: `no-cache`,
                                `Cache-Control`: `no-cache`,
                                `Referer`:"https://live.bilibili.com/" + strconv.Itoa(c.Roomid),
-                               `Cookie`:c.Cookie,
+                               `Cookie`:p.Map_2_Cookies_String(c.Cookie),
                        },
                        Timeout:3,
                        Retry:2,
@@ -543,7 +546,7 @@ func (i *api) Get_HotRank() {
                        `Pragma`: `no-cache`,
                        `Cache-Control`: `no-cache`,
                        `Referer`:"https://live.bilibili.com/" + strconv.Itoa(c.Roomid),
-                       `Cookie`:c.Cookie,
+                       `Cookie`:p.Map_2_Cookies_String(c.Cookie),
                },
                Timeout:3,
                Retry:2,
@@ -600,7 +603,7 @@ func (i *api) Get_guardNum() {
                        `Pragma`: `no-cache`,
                        `Cache-Control`: `no-cache`,
                        `Referer`:"https://live.bilibili.com/" + strconv.Itoa(c.Roomid),
-                       `Cookie`:c.Cookie,
+                       `Cookie`:p.Map_2_Cookies_String(c.Cookie),
                },
                Timeout:3,
                Retry:2,
@@ -765,7 +768,7 @@ func Get_cookie() {
                                Header:map[string]string{
                                        `Content-Type`:`application/x-www-form-urlencoded; charset=UTF-8`,
                                        `Referer`: `https://passport.bilibili.com/login`,
-                                       `Cookie`:c.Cookie,
+                                       `Cookie`:p.Map_2_Cookies_String(c.Cookie),
                                },
                                Timeout:10,
                                Retry:2,        
@@ -802,7 +805,9 @@ func Get_cookie() {
                }
        }
        {//写入cookie.txt
-               c.Cookie = cookie
+               for k,v := range p.Cookies_String_2_Map(cookie){
+                       c.Cookie[k] = v
+               }
                f := p.File()
                f.FileWR(p.Filel{
                        File:`cookie.txt`,
@@ -823,16 +828,16 @@ func Get_cookie() {
 }
 
 func (i *api) Switch_FansMedal() {
-       if c.Cookie == `` {return}
+       if len(c.Cookie) == 0 {return}
        if api_limit.TO() {return}//超额请求阻塞,超时将取消
        apilog := apilog.Base_add(`切换粉丝牌`)
-
+       Cookie := p.Map_2_Cookies_String(c.Cookie)
        {//验证是否本直播间牌子
                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,
+                               `Cookie`:Cookie,
                        },
                        Timeout:10,
                        Retry:2,
@@ -851,7 +856,7 @@ func (i *api) Switch_FansMedal() {
                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,
+                               `Cookie`:p.Map_2_Cookies_String(c.Cookie),
                        },
                        Timeout:10,
                        Retry:2,
@@ -889,17 +894,9 @@ func (i *api) Switch_FansMedal() {
                post_str string
        )
        {//生成佩戴信息
-               var csrf string
-               if i := strings.Index(c.Cookie, "bili_jct="); i == -1 {
-                       apilog.L(`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]
-                       }
-               }
+               csrf := c.Cookie[`bili_jct`]
+               if csrf == `` {apilog.L(`E: `,"Cookie错误,无bili_jct=");return}
+               
                post_str = `csrf_token=`+csrf+`&csrf=`+csrf
                if medal_id == 0 {//无牌,不佩戴牌子
                        post_url = `https://api.live.bilibili.com/xlive/web-room/v1/fansMedal/take_off`
@@ -914,7 +911,7 @@ func (i *api) Switch_FansMedal() {
                        Url:post_url,
                        PostStr:post_str,
                        Header:map[string]string{
-                               `Cookie`:c.Cookie,
+                               `Cookie`:Cookie,
                                `Content-Type`:`application/x-www-form-urlencoded; charset=UTF-8`,
                                `Referer`: `https://passport.bilibili.com/login`,
                        },
@@ -940,7 +937,8 @@ func (i *api) Switch_FansMedal() {
 //签到
 func Dosign() {
        apilog := apilog.Base_add(`签到`).L(`T: `,`签到`)
-       if c.Cookie == `` {apilog.L(`E: `,`失败!无cookie`);return}
+       if len(c.Cookie) == 0 {apilog.L(`E: `,`失败!无cookie`);return}
+       if api_limit.TO() {return}//超额请求阻塞,超时将取消
 
        {//检查是否签到
                req := p.Req()
@@ -957,7 +955,7 @@ func Dosign() {
                                `Pragma`: `no-cache`,
                                `Cache-Control`: `no-cache`,
                                `Referer`:"https://live.bilibili.com/all",
-                               `Cookie`:c.Cookie,
+                               `Cookie`:p.Map_2_Cookies_String(c.Cookie),
                        },
                        Timeout:3,
                        Retry:2,
@@ -997,7 +995,7 @@ func Dosign() {
                                `Pragma`: `no-cache`,
                                `Cache-Control`: `no-cache`,
                                `Referer`:"https://live.bilibili.com/all",
-                               `Cookie`:c.Cookie,
+                               `Cookie`:p.Map_2_Cookies_String(c.Cookie),
                        },
                        Timeout:3,
                        Retry:2,
@@ -1019,4 +1017,118 @@ func Dosign() {
                if msg.Code == 0 {apilog.L(`I: `,`签到成功!本月已签到`, msg.Data.HadSignDays,`天`);return}
                apilog.L(`E: `,msg.Message)
        }
+}
+
+//LIVE_BUVID
+func (i *api) Get_LIVE_BUVID() (o *api){
+       o = i
+       apilog := apilog.Base_add(`LIVE_BUVID`).L(`T: `,`获取LIVE_BUVID`)
+       if len(c.Cookie) == 0 {apilog.L(`E: `,`失败!无cookie`);return}
+       if api_limit.TO() {apilog.L(`E: `,`超时!`);return}//超额请求阻塞,超时将取消
+
+       {//获取
+               req := p.Req()
+               if err := req.Reqf(p.Rval{
+                       Url:`https://live.bilibili.com/`+ strconv.Itoa(o.Roomid),
+                       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-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),
+                       },
+                       Timeout:3,
+                       Retry:2,
+               });err != nil {
+                       apilog.L(`E: `,err)
+                       return
+               }
+
+               //cookie
+               for k,v := range p.Cookies_List_2_Map(req.Response.Cookies()){
+                       c.Cookie[k] = v
+               }
+       }
+       return
+}
+
+//小心心
+type E_json struct{
+       Code int `json:"code"`
+       Message string `json:"message"`
+       Ttl int `json:"ttl"`
+       Data struct{
+               Timestamp int `json:"timestamp"`
+               Heartbeat_interval int `json:"heartbeat_interval"`
+               Secret_key string `json:"secret_key"`
+               Secret_rule []int `json:"secret_rule"`
+               Patch_status int `json:"patch_status"`
+       } `json:"data"`
+}
+
+func F_x25Kn() {
+       apilog := apilog.Base_add(`LIVE_BUVID`).L(`T: `,`获取LIVE_BUVID`)
+       if len(c.Cookie) == 0 {apilog.L(`E: `,`失败!无cookie`);return}
+       if c.Cookie[`LIVE_BUVID`] == `` {apilog.L(`E: `,`失败!无LIVE_BUVID`);return}
+       if api_limit.TO() {apilog.L(`E: `,`超时!`);return}//超额请求阻塞,超时将取消
+
+       var res E_json
+
+       {//初始化
+
+               csrf := c.Cookie[`bili_jct`]
+               if csrf == `` {apilog.L(`E: `,"Cookie错误,无bili_jct=");return}
+
+               PostStr := `id=`
+               PostStr += `&device=`
+               PostStr += `&ts=`
+               PostStr += `&is_patch=0&`
+               PostStr += `heart_beat=[]&`
+               PostStr += `ua=Mozilla/5.0 (X11; Linux x86_64; rv:84.0) Gecko/20100101 Firefox/84.0&`
+               PostStr += `csrf_token=`+csrf+`&csrf=`+csrf+`&`
+               PostStr += `visit_id=`
+
+               req := p.Req()
+               if err := req.Reqf(p.Rval{
+                       Url:`https://live-trace.bilibili.com/xlive/data-interface/v1/x25Kn/E`,
+                       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, */*`,
+                               `Content-Type`: `application/x-www-form-urlencoded`,
+                               `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/"+strconv.Itoa(c.Roomid),
+                               `Cookie`:p.Map_2_Cookies_String(c.Cookie),
+                       },
+                       PostStr:url.QueryEscape(PostStr),
+                       Timeout:3,
+                       Retry:2,
+               });err != nil {
+                       apilog.L(`E: `,err)
+                       return
+               }
+
+               if e := json.Unmarshal(req.Respon,&res);e != nil {
+                       apilog.L(`E: `,e)
+                       return
+               }
+
+               if res.Code != 0{
+                       apilog.L(`E: `,res.Message)
+                       return
+               }
+       }
+       
 }
\ No newline at end of file
index 9578999c8e2b9dddf7a11fa772587be916840342..38e50b5d148f1a926245709295de5191242bbe35 100644 (file)
@@ -244,7 +244,7 @@ func Saveflvf(){
                        os.Rename(saveflv.path+".flv.dtmp", saveflv.path+".flv")
                }()
 
-               Cookie := c.Cookie
+               Cookie := p.Map_2_Cookies_String(c.Cookie)
                if i := strings.Index(Cookie, "PVID="); i != -1 {
                        if d := strings.Index(Cookie[i:], ";"); d == -1 {
                                Cookie = Cookie[:i]
index fc326f2b69e8ec822b94d2c694f1fea443706417..cf8a9c697534abda77342c84fd26c8dcec95a1d9 100644 (file)
@@ -682,11 +682,11 @@ func (replyF) danmu(s string) {
 //传入字符串即可发送
 //需要cookie
 func Msg_senddanmu(msg string){
-       if c.Cookie == "" || c.Roomid == 0 {
+       if len(c.Cookie) == 0 || c.Roomid == 0 {
                msglog.L(`I: `, `c.Cookie == "" || c.Roomid == 0`)
                return
        }
-       send.Danmu_s(msg, c.Cookie, c.Roomid)
+       send.Danmu_s(msg, p.Map_2_Cookies_String(c.Cookie), c.Roomid)
 }
 
 //弹幕显示
index 3e72988d9ff81b3be2e9bd4290d5498260ab659c..16931131f4442467a63760c7fc6a5b84bb3ea12a 100644 (file)
@@ -35,17 +35,8 @@ func Danmu_s(msg,Cookie string, roomid int) {
                }
        }
 
-       var csrf string
-       if i := strings.Index(Cookie, "bili_jct="); i == -1 {
-               l.L(`E: `,"Cookie错误,无bili_jct=")
-               return
-       } else {
-               if d := strings.Index(Cookie[i + 9:], ";"); d == -1 {
-                       csrf = Cookie[i + 9:]
-               } else {
-                       csrf = Cookie[i + 9:][:d]
-               }
-       }
+       csrf := c.Cookie[`bili_jct`]
+       if csrf == `` {l.L(`E: `,"Cookie错误,无bili_jct=");return}
 
        PostStr := `color=16777215&fontsize=25&mode=1&msg=` + msg + `&rnd=` + strconv.Itoa(int(p.Sys().GetSTime())) + `&roomid=` + strconv.Itoa(roomid) + `&bubble=0&csrf_token=` + csrf + `&csrf=` + csrf
        l.L(`I: `,"发送", msg, "至", roomid)
index f74a5f29088c246d0052f2a27fc04fb7c405b3bc..190c81fd495f6355b1b3a0ca83d1136387074622 100644 (file)
@@ -33,17 +33,8 @@ func Send_pm(uid int, msg string) error {
                return errors.New(`不能发送给自己`)
        }
 
-       var csrf string
-       if i := strings.Index(c.Cookie, "bili_jct="); i == -1 {
-               log.L(`E: `,`Cookie错误,无bili_jct=`)
-               return errors.New("Cookie错误,无bili_jct=")
-       } else {
-               if d := strings.Index(c.Cookie[i + 9:], ";"); d == -1 {
-                       csrf = c.Cookie[i + 9:]
-               } else {
-                       csrf = c.Cookie[i + 9:][:d]
-               }
-       }
+       csrf := c.Cookie[`bili_jct`]
+       if csrf == `` {return errors.New("Cookie错误,无bili_jct=")}
 
        var new_uuid string
        {
@@ -76,7 +67,7 @@ func Send_pm(uid int, msg string) error {
                        `Pragma`: `no-cache`,
                        `Cache-Control`: `no-cache`,
                        `Referer`:"https://message.bilibili.com",
-                       `Cookie`:c.Cookie,
+                       `Cookie`:p.Map_2_Cookies_String(c.Cookie),
                },
        });e != nil {
                log.L(`E: `,e)
index da796a8bc175d15bfe8301fc890e375d70c7cd48..da12bad4dbd2ed96f8b5758fa0946f1e4d1b2b13 100644 (file)
@@ -109,7 +109,7 @@ func Demo(roomid ...int) {
                                        danmulog.L(`I: `, "未检测到cookie.txt,如果需要登录请在本机打开以下网址扫码登录,不需要请忽略")
                                        //获取cookie
                                        F.Get_cookie()
-                                       if c.Cookie != `` {
+                                       if len(c.Cookie) != 0 {
                                                danmulog.L(`I: `,"你已登录,刷新房间!")
                                                //刷新
                                                c.Danmu_Main_mq.Push_tag(`change_room`,nil)
@@ -118,7 +118,9 @@ func Demo(roomid ...int) {
                                if p.Checkfile().IsExist("cookie.txt") {
                                        q.File = "cookie.txt"
                                        f := p.File().FileWR(q)
-                                       c.Cookie = f
+                                       for k,v := range p.Cookies_String_2_Map(f){
+                                               c.Cookie[k] = v
+                                       }
                                        if tmp_uid,e := g.SS(f,`DedeUserID=`,`;`,0,0);e == nil {
                                                if v,e := strconv.Atoi(tmp_uid);e == nil {
                                                        c.Uid = v
@@ -164,7 +166,7 @@ func Demo(roomid ...int) {
                                        Func_abort_close:func(){danmulog.L(`I: `,`服务器连接中断`)},
                                        Func_normal_close:func(){danmulog.L(`I: `,`服务器连接关闭`)},
                                        Header: map[string]string{
-                                               `Cookie`:c.Cookie,
+                                               `Cookie`:p.Map_2_Cookies_String(c.Cookie),
                                                `Host`: u.Hostname(),
                                                `User-Agent`: `Mozilla/5.0 (X11; Linux x86_64; rv:84.0) Gecko/20100101 Firefox/84.0`,
                                                `Accept`: `*/*`,
@@ -220,7 +222,7 @@ func Demo(roomid ...int) {
                                                        },
                                                })
 
-                                               if c.Cookie != `` {//附加功能 弹幕机 无cookie无法发送弹幕
+                                               if len(c.Cookie) != 0 {//附加功能 弹幕机 无cookie无法发送弹幕
                                                        reply.Danmuji_auto(1)
                                                }
                                                {//附加功能 直播流保存 营收