From b9d07bef88dd437eeb7d9878932500ee8be7270c Mon Sep 17 00:00:00 2001 From: qydysky Date: Sun, 20 Dec 2020 17:02:08 +0800 Subject: [PATCH] =?utf8?q?=E6=89=AB=E7=A0=81=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- F/api.go | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 2 + Send/Send.go | 5 +- bili_danmu.go | 35 +++++++++----- demo/go.mod | 1 + demo/go.sum | 2 + 6 files changed, 152 insertions(+), 16 deletions(-) diff --git a/F/api.go b/F/api.go index b2de625..60be510 100644 --- a/F/api.go +++ b/F/api.go @@ -3,9 +3,13 @@ package F import ( "time" "fmt" + "os" "strconv" "strings" + "context" + "net/http" + qr "github.com/skip2/go-qrcode" c "github.com/qydysky/bili_danmu/CV" g "github.com/qydysky/part/get" p "github.com/qydysky/part" @@ -574,4 +578,123 @@ func (i *api) Get_Version() { c.VERSION = r.RS[0] apilog.W("api version", c.VERSION) } +} + +func (i *api) Get_cookie() { + var img_url string + var oauth string + {//获取二维码 + r := p.Req() + if e := r.Reqf(p.Rval{ + Url:`https://passport.bilibili.com/qrcode/getLoginUrl`, + });e != nil { + apilog.Base(1,`Get_cookie`).E(e) + return + } + res := string(r.Respon) + if v,ok := p.Json().GetValFromS(res, "status").(bool);!ok || !v { + apilog.Base(1,`Get_cookie`).E(`getLoginUrl status failed!`) + return + } else { + if v,ok := p.Json().GetValFromS(res, "data.url").(string);ok { + img_url = v + } + if v,ok := p.Json().GetValFromS(res, "data.oauthKey").(string);ok { + oauth = v + } + } + if img_url == `` || oauth == `` { + apilog.Base(1,`Get_cookie`).E(`img_url:`,img_url,` oauth:`,oauth) + return + } + } + var server *http.Server + {//生成二维码 + qr.WriteFile(img_url,qr.Medium,256,`qr.png`) + if !p.Checkfile().IsExist(`qr.png`) { + apilog.Base(1,`Get_cookie`).E(`qr error`) + return + } + go func(){//启动web + web := http.NewServeMux() + web.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Access-Control-Allow-Origin", "*") //允许访问所有域 + w.Header().Add("Access-Control-Allow-Headers", "Content-Type") //header的类型 + var path string = r.URL.Path[1:] + http.ServeFile(w, r,`./`+path) + }) + server = &http.Server{ + Addr: `127.0.0.1:`+strconv.Itoa(p.Sys().GetFreePort()), + WriteTimeout: time.Second * 10, + Handler: web, + } + apilog.Base(1,`Get_cookie`).W(`打开链接扫码登录:`,`http://`+server.Addr+`/qr.png`) + server.ListenAndServe() + }() + } + var cookie string + {//3s刷新查看是否通过 + max_try := 20 + for max_try > 0 { + max_try -= 1 + p.Sys().Timeoutf(3) + r := p.Req() + if e := r.Reqf(p.Rval{ + Url:`https://passport.bilibili.com/qrcode/getLoginInfo`, + PostStr:`oauthKey=`+oauth, + Header:map[string]string{ + `Content-Type`:`application/x-www-form-urlencoded; charset=UTF-8`, + `Referer`: `https://passport.bilibili.com/login`, + }, + });e != nil { + apilog.Base(1,`Get_cookie`).E(e) + return + } + res := string(r.Respon) + if v,ok := p.Json().GetValFromS(res, "status").(bool);!ok { + apilog.Base(1,`Get_cookie`).E(`getLoginInfo status false`) + return + } else if !v { + if v,ok := p.Json().GetValFromS(res, "message").(string);ok { + apilog.Base(1,`Get_cookie`).W(`登录中`,v) + } + continue + } else { + apilog.Base(1,`Get_cookie`).W(`登录,并保存了cookie`) + if v := r.Response.Cookies();len(v) == 0 { + apilog.Base(1,`Get_cookie`).E(`getLoginInfo cookies len == 0`) + return + } else { + cookie = p.Map_2_Cookies_String(p.Cookies_List_2_Map(v))//cookie to string + } + if cookie == `` { + apilog.Base(1,`Get_cookie`).E(`getLoginInfo cookies ""`) + return + } else {break} + } + } + if max_try <= 0 { + apilog.Base(1,`Get_cookie`).W(`登录取消`) + return + } + } + {//写入cookie.txt + c.Cookie = cookie + f := p.File() + f.FileWR(p.Filel{ + File:`cookie.txt`, + Write:true, + Loc:0, + Context:[]interface{}{cookie}, + }) + } + {//关闭web + if err := server.Shutdown(context.Background()); err != nil { + apilog.Base(1,`Get_cookie`).E("HTTP server Shutdown:", err.Error()) + } + if p.Checkfile().IsExist(`qr.png`) { + os.RemoveAll(`qr.png`) + return + } + } } \ No newline at end of file diff --git a/README.md b/README.md index 06e9ae9..c1558a0 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@ golang go version go1.15.5 linux/amd64 - [github.com/qydysky/part](https://github.com/qydysky/part) under [MIT](https://raw.githubusercontent.com/qydysky/part/master/LICENSE) - [github.com/christopher-dG/go-obs-websocket](https://github.com/christopher-dG/go-obs-websocket) under [MIT](https://raw.githubusercontent.com/christopher-dG/go-obs-websocket/master/LICENSE) - [github.com/gorilla/websocket](https://github.com/gorilla/websocket) under [BSD 2-Clause](https://raw.githubusercontent.com/gorilla/websocket/master/LICENSE) +- [github.com/skip2/go-qrcode](https://github.com/skip2/go-qrcode) under [MIT](https://github.com/skip2/go-qrcode/blob/master/LICENSE) - [7z](https://www.7-zip.org/) under [LICENSE](https://www.7-zip.org/license.txt) --- @@ -57,6 +58,7 @@ golang go version go1.15.5 linux/amd64 #### 当前支持功能 以下内容可能过时,点击查看[当前支持功能](https://github.com/qydysky/bili_danmu/blob/master/Reply/F.go#L16) +- [x] 扫码登录 - [x] 自定义语音提醒 - [x] GTK弹幕窗 - [x] GTK信息窗 diff --git a/Send/Send.go b/Send/Send.go index 45342f0..d7a38e5 100644 --- a/Send/Send.go +++ b/Send/Send.go @@ -27,10 +27,7 @@ func Danmu_s(msg,Cookie string, roomid int) { return } - if i := strings.Index(Cookie, "PVID="); i == -1 { - l.E("Cookie错误,无PVID=") - return - } else { + if i := strings.Index(Cookie, "PVID="); i != -1 {//删除PVID if d := strings.Index(Cookie[i:], ";"); d == -1 { Cookie = Cookie[:i] } else { diff --git a/bili_danmu.go b/bili_danmu.go index dc4bcea..7f7f1e6 100644 --- a/bili_danmu.go +++ b/bili_danmu.go @@ -90,21 +90,32 @@ func Demo(roomid ...int) { <-change_room_chan - //cookies - { - var q = p.Filel{ - Write:false, - } - if p.Checkfile().IsExist("cookie.txt") { - q.File = "cookie.txt" - } - f := p.File().FileWR(q) - c.Cookie = f - } - for !exit_sign { //获取房间相关信息 api := F.New_api(c.Roomid).Get_host_Token().Get_live() + //获取cookies + { + var q = p.Filel{ + Write:false, + } + if p.Checkfile().IsExist("cookie.txt") { + q.File = "cookie.txt" + f := p.File().FileWR(q) + c.Cookie = f + } else { + danmulog.I("未检测到cookie.txt,如果需要登录请在本机打开以下网址扫码登录,不需要请忽略") + go func(){//获取cookie + api.Get_cookie() + if c.Cookie != `` { + danmulog.I("你已登录,刷新房间!") + c.Danmu_Main_mq.Push(c.Danmu_Main_mq_item{//刷新 + Class:`change_room`, + }) + } + }() + p.Sys().Timeoutf(3) + } + } //获取用户版本 api.Get_Version() if len(api.Url) == 0 || api.Roomid == 0 || api.Token == "" || api.Uid == 0 || api.Locked { diff --git a/demo/go.mod b/demo/go.mod index 2309282..9cff862 100644 --- a/demo/go.mod +++ b/demo/go.mod @@ -12,6 +12,7 @@ require ( github.com/qydysky/bili_danmu v0.5.4 github.com/qydysky/part v0.3.4 // indirect github.com/shirou/gopsutil v3.20.11+incompatible // indirect + github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e // indirect golang.org/x/crypto v0.0.0-20201217014255-9d1352758620 // indirect golang.org/x/net v0.0.0-20201216054612-986b41b23924 // indirect golang.org/x/sys v0.0.0-20201218084310-7d0127a74742 // indirect diff --git a/demo/go.sum b/demo/go.sum index f5dc0cc..840c2c1 100644 --- a/demo/go.sum +++ b/demo/go.sum @@ -87,6 +87,8 @@ github.com/shirou/gopsutil v3.20.11+incompatible h1:LJr4ZQK4mPpIV5gOa4jCOKOGb4ty github.com/shirou/gopsutil v3.20.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= github.com/sirupsen/logrus v1.4.1 h1:GL2rEmy6nsikmW0r8opw9JIRScdMF5hA8cOYLH7In1k= github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q= +github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0= +github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= -- 2.39.2