]> 127.0.0.1 Git - bili_danmu/.git/commitdiff
简化代码&进入(每日)房间弹幕
authorqydysky <qydysky@foxmail.com>
Mon, 8 Feb 2021 05:23:59 +0000 (13:23 +0800)
committerqydysky <qydysky@foxmail.com>
Mon, 8 Feb 2021 05:23:59 +0000 (13:23 +0800)
.gitignore
F/api.go
F/xinxin.go
Reply/F.go
Reply/gtk.go
Reply/tts.go
bili_danmu.go
demo/config/config_K_v.json
demo/go.mod
demo/go.sum

index 63446831a012d855609485cbfff7cd826e249fc7..9eb99054cdd11c35460ce88fca976a1e758f5602 100644 (file)
@@ -17,3 +17,4 @@ demo/demo.run
 demo/ui/*.glade~
 demo/tts.mp3
 demo/cpu.pprof
+demo/qr.png
index 972482297e92dd1d298b7e7c454a33d99e5926bc..1b41a5edea75d19f2a0c7baa7c6bc8690088d70f 100644 (file)
--- 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 {
index 0c54afc417e39aec75b2ef8d787178931730f7b8..f824a37c5eede98d97a7730a58429bf2879a4a52 100644 (file)
@@ -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\"]",
index bb3068b9572797d3943bfb384b1981d0582c90f2..8b22c9eb1dee0f7a7ff7aaeaad9673ac4aac1f8b 100644 (file)
@@ -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
index 30caf38c2b98fc77a720d26c2297623fd9fec708..15530647bed8a4484a47d08bba6603ded8535bd1 100644 (file)
@@ -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):
index 563341743fb857fa20dabdcab865a92822868f58..60fbbfcc39260b713ee01668cc3f3349a1337939 100644 (file)
@@ -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
index 6641a007b14e1b2333ff767f0873e079a455ff01..239b8ba438dbc45f28c497e6e399c45390e8b4e0 100644 (file)
@@ -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()
                                                }
index c2c81c886d140b47458b335155961d5277c771c7..de93e4d4e39c354a01d6e4af6572f92ef0eb581a 100644 (file)
@@ -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
index f6f79b6804549ab8cb4c12ff14e7df23a340fa69..5b91541c11ec9ca5753af40afa438f3333e382e9 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.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
index 76a91bc37cda9e4e49409c6e2145f8f4dbf49e7e..37379bdd402789499c9cd9736919102ccbefd7c4 100644 (file)
@@ -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=