From 54cdaeca6474f6bf16757e57d679769cc79683c5 Mon Sep 17 00:00:00 2001 From: qydysky Date: Wed, 27 Jan 2021 15:27:57 +0800 Subject: [PATCH] =?utf8?q?cookie=E4=BD=BF=E7=94=A8map=E5=82=A8=E5=AD=98=20?= =?utf8?q?=E5=B0=8F=E5=BF=83=E5=BF=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- CV/Var.go | 2 +- F/api.go | 168 ++++++++++++++++++++++++++++++++++++++++-------- Reply/F.go | 2 +- Reply/Reply.go | 4 +- Send/Send.go | 13 +--- Send/Send_pm.go | 15 +---- bili_danmu.go | 10 +-- 7 files changed, 155 insertions(+), 59 deletions(-) diff --git a/CV/Var.go b/CV/Var.go index c3341d8..739097c 100644 --- 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//营收 diff --git a/F/api.go b/F/api.go index 3a934e0..973aa95 100644 --- 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 diff --git a/Reply/F.go b/Reply/F.go index 9578999..38e50b5 100644 --- a/Reply/F.go +++ b/Reply/F.go @@ -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] diff --git a/Reply/Reply.go b/Reply/Reply.go index fc326f2..cf8a9c6 100644 --- a/Reply/Reply.go +++ b/Reply/Reply.go @@ -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) } //弹幕显示 diff --git a/Send/Send.go b/Send/Send.go index 3e72988..1693113 100644 --- a/Send/Send.go +++ b/Send/Send.go @@ -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) diff --git a/Send/Send_pm.go b/Send/Send_pm.go index f74a5f2..190c81f 100644 --- a/Send/Send_pm.go +++ b/Send/Send_pm.go @@ -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) diff --git a/bili_danmu.go b/bili_danmu.go index da796a8..da12bad 100644 --- a/bili_danmu.go +++ b/bili_danmu.go @@ -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) } {//附加功能 直播流保存 营收 -- 2.39.2