From: qydysky Date: Mon, 8 Feb 2021 05:23:59 +0000 (+0800) Subject: 简化代码&进入(每日)房间弹幕 X-Git-Tag: v0.5.7~1^2~11 X-Git-Url: http://127.0.0.1:8081/?a=commitdiff_plain;h=e84f355ede5e6b124e5761746065cadc298acdb5;p=bili_danmu%2F.git 简化代码&进入(每日)房间弹幕 --- diff --git a/.gitignore b/.gitignore index 6344683..9eb9905 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,4 @@ demo/demo.run demo/ui/*.glade~ demo/tts.mp3 demo/cpu.pprof +demo/qr.png diff --git a/F/api.go b/F/api.go index 9724822..1b41a5e 100644 --- a/F/api.go +++ b/F/api.go @@ -13,7 +13,9 @@ import ( "net/url" qr "github.com/skip2/go-qrcode" + msgq "github.com/qydysky/part/msgq" c "github.com/qydysky/bili_danmu/CV" + web "github.com/qydysky/part/web" g "github.com/qydysky/part/get" p "github.com/qydysky/part" uuid "github.com/gofrs/uuid" @@ -725,53 +727,63 @@ func Get_cookie() { apilog.L(`E: `,e) return } - res := string(r.Respon) - if v,ok := p.Json().GetValFromS(res, "status").(bool);!ok || !v { - apilog.L(`E: `,`getLoginUrl status failed!`) + var res struct{ + Code int `json:"code"` + Status bool `json:"status"` + Data struct{ + Url string `json:"url"` + OauthKey string `json:"oauthKey"` + } `json:"data"` + } + if e := json.Unmarshal(r.Respon, &res);e != nil { + apilog.L(`E: `, e) 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.L(`E: `,`img_url:`,img_url,` oauth:`,oauth) + if res.Code != 0 { + apilog.L(`E: `, `code != 0`) return } + if !res.Status { + apilog.L(`E: `, `status == false`) + return + } + + if res.Data.Url == `` { + apilog.L(`E: `, `Data.Urls == ""`) + return + } else {img_url = res.Data.Url} + if res.Data.OauthKey == `` { + apilog.L(`E: `, `Data.OauthKey == ""`) + return + } else {oauth = res.Data.OauthKey} } - var server *http.Server + var server = new(http.Server) {//生成二维码 qr.WriteFile(img_url,qr.Medium,256,`qr.png`) if !p.Checkfile().IsExist(`qr.png`) { apilog.L(`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的类型 + //启动web + s := web.New(server) + s.Handle(map[string]func(http.ResponseWriter,*http.Request){ + `/`:func(w http.ResponseWriter,r *http.Request){ 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.L(`W: `,`打开链接扫码登录:`,`http://`+server.Addr+`/qr.png`) - server.ListenAndServe() - }() + if path == `` {path = `index.html`} + http.ServeFile(w, r, path) + }, + `/exit`:func(w http.ResponseWriter,r *http.Request){ + s.Server.Shutdown(context.Background()) + }, + }) + apilog.L(`W: `,`打开链接扫码登录:`,`http://`+server.Addr+`/qr.png`) p.Sys().Timeoutf(1) } var cookie string {//3s刷新查看是否通过 max_try := 20 change_room_sign := false - c.Danmu_Main_mq.Pull_tag(map[string]func(interface{})(bool){ + c.Danmu_Main_mq.Pull_tag(msgq.FuncMap{ `change_room`:func(data interface{})(bool){//房间改变 change_room_sign = true return true @@ -1230,7 +1242,7 @@ func (i *api) F_x25Kn() (o *api) { PostStr += `csrf_token=`+csrf+`&csrf=`+csrf+`&` PostStr += `visit_id=` - if wasm := Wasm(0, rt_obj);wasm == `` {//0全局 + if wasm := Wasm(3, 0, rt_obj);wasm == `` {//0全局 apilog.L(`E: `,`发生错误`) return } else { diff --git a/F/xinxin.go b/F/xinxin.go index 0c54afc..f824a37 100644 --- a/F/xinxin.go +++ b/F/xinxin.go @@ -6,6 +6,7 @@ import ( "time" "github.com/skratchdot/open-golang/open" websocket "github.com/qydysky/part/websocket" + msgq "github.com/qydysky/part/msgq" web "github.com/qydysky/part/web" c "github.com/qydysky/bili_danmu/CV" ) @@ -40,7 +41,7 @@ type S struct { //全局对象 var ( wslog = c.Log.Base(`api`).Base_add(`小心心加密`) //日志 - rec_chan = make(chan S)//收通道 + rec_chan = make(chan S,5)//收通道 webpath string//web地址,由于实时获取空闲端口,故将在稍后web启动后赋值 ws = websocket.New_server()//新建websocket实例 ) @@ -66,7 +67,7 @@ func init() { func server() { { ws_mq := ws.Interface()//获取websocket操作对象 - ws_mq.Pull_tag(map[string]func(interface{})(bool){ + ws_mq.Pull_tag(msgq.FuncMap{ `recv`:func(data interface{})(bool){ if tmp,ok := data.(websocket.Uinterface);ok {//websocket接收并响应 //websocket.Uinterface{ @@ -80,7 +81,7 @@ func server() { wslog.L(`E: `, e, string(tmp.Data)) } - select{//无多用户上传不同数据的情况,仅接收1个,其余会因通道无传出而忽略 + select{ case rec_chan <- s: default: } @@ -116,7 +117,9 @@ func server() { wslog.L(`I: `,`如需加密,会自动打开`,webpath) } -func Wasm(uid uintptr,s RT) (o string) { +func Wasm(maxloop int, uid uintptr,s RT) (o string) {//maxloop 超时重试 + if maxloop <= 0 {return} + for try:=5;try > 0 && ws.Len() == 0;try-=1 {//没有从池中取出 open.Run(webpath) wslog.L(`I: `,`浏览器打开`,webpath) @@ -139,8 +142,9 @@ func Wasm(uid uintptr,s RT) (o string) { case r :=<- rec_chan: if r.Id != s.R.Id {break}//或许接收到之前的请求,校验Id字段 return r.S - case <- time.After(time.Second): + case <- time.After(time.Second*time.Duration(3)): wslog.L(`E: `,`超时!响应>1s,确认保持`,webpath,`开启`) + o = Wasm(maxloop-1, uid, s) return } } @@ -148,7 +152,7 @@ func Wasm(uid uintptr,s RT) (o string) { func test(uid uintptr) bool { time.Sleep(time.Second*time.Duration(3)) - if s := Wasm(uid, RT{ + if s := Wasm(3, uid, RT{ R:R{ Id: "[9,371,1,22613059]", Device: "[\"AUTO8216117272375373\",\"77bee604-b591-4664-845b-b69603f8c71c\"]", diff --git a/Reply/F.go b/Reply/F.go index bb3068b..8b22c9e 100644 --- a/Reply/F.go +++ b/Reply/F.go @@ -12,8 +12,10 @@ import ( c "github.com/qydysky/bili_danmu/CV" F "github.com/qydysky/bili_danmu/F" + send "github.com/qydysky/bili_danmu/Send" "github.com/christopher-dG/go-obs-websocket" p "github.com/qydysky/part" + msgq "github.com/qydysky/part/msgq" b "github.com/qydysky/part/buf" s "github.com/qydysky/part/signal" ) @@ -725,7 +727,7 @@ func Jiezouf(s []string) bool { //保存所有消息到json func init(){ Save_to_json(0, []interface{}{`[`}) - c.Danmu_Main_mq.Pull_tag(map[string]func(interface{})(bool){ + c.Danmu_Main_mq.Pull_tag(msgq.FuncMap{ `change_room`:func(data interface{})(bool){//房间改变 Save_to_json(0, []interface{}{`[`}) return false @@ -742,4 +744,11 @@ func Save_to_json(Loc int,Context []interface{}) { Context:Context, }) } +} + +//进入房间发送弹幕 +func Entry_danmu(){ + if s,ok := c.K_v[`进入房间发送弹幕`].(string);ok && s != ``{ + send.Danmu_s(s,p.Map_2_Cookies_String(c.Cookie),c.Roomid) + } } \ No newline at end of file diff --git a/Reply/gtk.go b/Reply/gtk.go index 30caf38..1553064 100644 --- a/Reply/gtk.go +++ b/Reply/gtk.go @@ -17,6 +17,7 @@ import ( "github.com/gotk3/gotk3/gtk" "github.com/gotk3/gotk3/gdk" p "github.com/qydysky/part" + msgq "github.com/qydysky/part/msgq" F "github.com/qydysky/bili_danmu/F" c "github.com/qydysky/bili_danmu/CV" s "github.com/qydysky/part/buf" @@ -72,7 +73,7 @@ func init(){ if!IsOn("Gtk") {return} go Gtk_danmu() //使用带tag的消息队列在功能间传递消息 - Danmu_mq.Pull_tag(map[string]func(interface{})(bool){ + Danmu_mq.Pull_tag(msgq.FuncMap{ `danmu`:func(data interface{})(bool){//弹幕 select{ case Gtk_danmu_chan <- data.(Danmu_mq_t): diff --git a/Reply/tts.go b/Reply/tts.go index 5633417..60fbbfc 100644 --- a/Reply/tts.go +++ b/Reply/tts.go @@ -6,6 +6,7 @@ import ( "strings" p "github.com/qydysky/part" c "github.com/qydysky/bili_danmu/CV" + msgq "github.com/qydysky/part/msgq" s "github.com/qydysky/part/buf" ) @@ -61,7 +62,7 @@ func init(){ //消息队列接收tts类消息,并传送到TTS朗读 //使用带tag的消息队列在功能间传递消息 - c.Danmu_Main_mq.Pull_tag(map[string]func(interface{})(bool){ + c.Danmu_Main_mq.Pull_tag(msgq.FuncMap{ `tts`:func(data interface{})(bool){//tts if _,ok := tts_setting[data.(Danmu_mq_t).uid];ok { tts_List <- data diff --git a/bili_danmu.go b/bili_danmu.go index 6641a00..239b8ba 100644 --- a/bili_danmu.go +++ b/bili_danmu.go @@ -12,6 +12,7 @@ import ( p "github.com/qydysky/part" ws "github.com/qydysky/part/websocket" g "github.com/qydysky/part/get" + msgq "github.com/qydysky/part/msgq" reply "github.com/qydysky/bili_danmu/Reply" send "github.com/qydysky/bili_danmu/Send" c "github.com/qydysky/bili_danmu/CV" @@ -69,7 +70,7 @@ func Demo(roomid ...int) { }() //使用带tag的消息队列在功能间传递消息 - c.Danmu_Main_mq.Pull_tag(map[string]func(interface{})(bool){ + c.Danmu_Main_mq.Pull_tag(msgq.FuncMap{ `change_room`:func(data interface{})(bool){//房间改变 c.Rev = 0.0 //营收 c.Renqi = 1//人气置1 @@ -98,7 +99,7 @@ func Demo(roomid ...int) { }, }) //单独,避免队列执行耗时block从而无法接收更多消息 - c.Danmu_Main_mq.Pull_tag(map[string]func(interface{})(bool){ + c.Danmu_Main_mq.Pull_tag(msgq.FuncMap{ `pm`:func(data interface{})(bool){//私信 if tmp,ok := data.(send.Pm_item);ok{ send.Send_pm(tmp.Uid,tmp.Msg) @@ -227,7 +228,7 @@ func Demo(roomid ...int) { //订阅消息,以便刷新舰长数 api.Get_guardNum() //使用带tag的消息队列在功能间传递消息 - c.Danmu_Main_mq.Pull_tag(map[string]func(interface{})(bool){ + c.Danmu_Main_mq.Pull_tag(msgq.FuncMap{ `guard_update`:func(data interface{})(bool){//舰长更新 go api.Get_guardNum() return false @@ -240,6 +241,8 @@ func Demo(roomid ...int) { go api.F_x25Kn() //每日签到 F.Dosign() + //附加功能 每日发送弹幕 + go reply.Entry_danmu() return false }, }) @@ -247,7 +250,8 @@ func Demo(roomid ...int) { if len(c.Cookie) != 0 {//附加功能 弹幕机 无cookie无法发送弹幕 reply.Danmuji_auto(1) } - {//附加功能 直播流保存 营收 + {//附加功能 进房间发送弹幕 直播流保存 营收 + go reply.Entry_danmu() go reply.Saveflvf() go reply.ShowRevf() } diff --git a/demo/config/config_K_v.json b/demo/config/config_K_v.json index c2c81c8..de93e4d 100644 --- a/demo/config/config_K_v.json +++ b/demo/config/config_K_v.json @@ -23,5 +23,7 @@ "save_to_json":"", "get_xiao_xinxin-help":"获取小心心", - "get_xiao_xinxin":true + "get_xiao_xinxin":true, + + "进入房间发送弹幕":" " } \ No newline at end of file diff --git a/demo/go.mod b/demo/go.mod index f6f79b6..5b91541 100644 --- a/demo/go.mod +++ b/demo/go.mod @@ -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.13 // indirect + github.com/qydysky/part v0.3.15 // 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 diff --git a/demo/go.sum b/demo/go.sum index 76a91bc..37379bd 100644 --- a/demo/go.sum +++ b/demo/go.sum @@ -137,6 +137,10 @@ github.com/qydysky/part v0.3.12 h1:RWBGvieO2pCAEvk7378otIzoHlB6CcL2Ho5FfXDLk3w= github.com/qydysky/part v0.3.12/go.mod h1:43opuciW71sZvOR67kye50jgMDSDrn/t6+LefNdlXPg= github.com/qydysky/part v0.3.13 h1:GN5SvmdcGwGbzP+N7cgxEgIzL2CdSSFoMXGbPIfM9os= github.com/qydysky/part v0.3.13/go.mod h1:43opuciW71sZvOR67kye50jgMDSDrn/t6+LefNdlXPg= +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/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=