]> 127.0.0.1 Git - bili_danmu/.git/commitdiff
refactor
authorqydysky <qydysky@foxmail.com>
Thu, 7 Apr 2022 22:49:05 +0000 (06:49 +0800)
committerqydysky <qydysky@foxmail.com>
Thu, 7 Apr 2022 22:49:05 +0000 (06:49 +0800)
CV/Var.go
F/api.go
Reply/steam/stream.go [new file with mode: 0644]

index c17aa5ca4cca3b47f1ae45007d1c4cd1c8855f17..ddd383b4bda9dd965f8e078e96e0f11054474f19 100644 (file)
--- a/CV/Var.go
+++ b/CV/Var.go
@@ -10,6 +10,36 @@ import (
        syncmap "github.com/qydysky/part/sync"
 )
 
+type Common struct {
+       Uid               int            //client uid
+       Live              []string       //直播流链接
+       Live_qn           int            //当前直播流质量
+       Live_want_qn      int            //期望直播流质量
+       Roomid            int            //房间ID
+       Cookie            syncmap.Map    //Cookie
+       Title             string         //直播标题
+       Uname             string         //主播名
+       UpUid             int            //主播uid
+       Rev               float64        //营收
+       Renqi             int            //人气
+       GuardNum          int            //舰长数
+       ParentAreaID      int            //父分区
+       AreaID            int            //子分区
+       Locked            bool           //直播间封禁
+       Note              string         //分区排行
+       Live_Start_Time   time.Time      //直播开始时间
+       Liveing           bool           //是否在直播
+       Wearing_FansMedal int            //当前佩戴的粉丝牌
+       Token             string         //弹幕钥
+       WSURL             []string       //弹幕链接
+       LIVE_BUVID        bool           //cookies含LIVE_BUVID
+       Stream_url        string         //直播Web服务
+       Proxy             string         //全局代理
+       AcceptQn          map[int]string //允许的直播流质量
+       Qn                map[int]string //全部直播流质量
+       K_v               syncmap.Map    //配置文件
+}
+
 var (
        Uid = 0 //client uid
 
index 6b2f856bc675f8d789bdbed1bd1bfd4f7e49068f..3257eecc4e9e5a0328c5eae5cf25d6f3a65e9040 100644 (file)
--- a/F/api.go
+++ b/F/api.go
@@ -29,170 +29,176 @@ import (
 var apilog = c.Log.Base(`api`)
 var api_limit = limit.New(1, 2000, 30000) //频率限制1次/2s,最大等待时间30s
 
-func Get(key string) {
+type GetFunc struct {
+       c.Common
+}
+
+func (c *GetFunc) Get(key string) {
        apilog := apilog.Base_add(`Get`)
 
        if api_limit.TO() {
                return
        } //超额请求阻塞,超时将取消
 
-       var api_can_get = map[string][]func() []string{
-               `Cookie`: { //Cookie
-                       Get_cookie,
-               },
-               `Uid`: { //用戶uid
-                       GetUid,
-               },
-               `UpUid`: { //主播uid
-                       getInfoByRoom,
-                       getRoomPlayInfo,
-                       Html,
-               },
-               `Live_Start_Time`: { //直播开始时间
-                       getInfoByRoom,
-                       getRoomPlayInfo,
-                       Html,
-               },
-               `Liveing`: { //是否在直播
-                       getInfoByRoom,
-                       getRoomPlayInfo,
-                       Html,
-               },
-               `Title`: { //直播间标题
-                       getInfoByRoom,
-                       Html,
-               },
-               `Uname`: { //主播名
-                       getInfoByRoom,
-                       Html,
-               },
-               `ParentAreaID`: { //分区
-                       getInfoByRoom,
-                       Html,
-               },
-               `AreaID`: { //子分区
-                       getInfoByRoom,
-                       Html,
-               },
-               `Roomid`: { //房间id
-                       missRoomId,
-               },
-               `GuardNum`: { //舰长数
-                       Get_guardNum,
-                       getInfoByRoom,
-                       getRoomPlayInfo,
-                       Html,
-               },
-               `Note`: { //分区排行
-                       Get_HotRank,
-                       getInfoByRoom,
-                       Html,
-               },
-               `Locked`: { //直播间是否被封禁
-                       getInfoByRoom,
-                       Html,
-               },
-               `Live_qn`: { //当前直播流质量
-                       getRoomPlayInfo,
-                       Html,
-               },
-               `AcceptQn`: { //允许的清晰度
-                       getRoomPlayInfo,
-                       Html,
-               },
-               `Live`: { //直播流链接
-                       getRoomPlayInfoByQn,
-                       getRoomPlayInfo,
-                       Html,
-               },
-               `Token`: { //弹幕钥
-                       getDanmuInfo,
-               },
-               `WSURL`: { //弹幕链接
-                       getDanmuInfo,
-               },
-               // `VERSION`:[]func()([]string){//客户版本  不再需要
-               //      Get_Version,
-               // },
-               `LIVE_BUVID`: { //LIVE_BUVID
-                       Get_LIVE_BUVID,
-               },
+       var (
+               api_can_get = map[string][]func() []string{
+                       `Cookie`: { //Cookie
+                               c.Get_cookie,
+                       },
+                       `Uid`: { //用戶uid
+                               c.GetUid,
+                       },
+                       `UpUid`: { //主播uid
+                               c.getInfoByRoom,
+                               c.getRoomPlayInfo,
+                               c.Html,
+                       },
+                       `Live_Start_Time`: { //直播开始时间
+                               c.getInfoByRoom,
+                               c.getRoomPlayInfo,
+                               c.Html,
+                       },
+                       `Liveing`: { //是否在直播
+                               c.getInfoByRoom,
+                               c.getRoomPlayInfo,
+                               c.Html,
+                       },
+                       `Title`: { //直播间标题
+                               c.getInfoByRoom,
+                               c.Html,
+                       },
+                       `Uname`: { //主播名
+                               c.getInfoByRoom,
+                               c.Html,
+                       },
+                       `ParentAreaID`: { //分区
+                               c.getInfoByRoom,
+                               c.Html,
+                       },
+                       `AreaID`: { //子分区
+                               c.getInfoByRoom,
+                               c.Html,
+                       },
+                       `Roomid`: { //房间id
+                               c.missRoomId,
+                       },
+                       `GuardNum`: { //舰长数
+                               c.Get_guardNum,
+                               c.getInfoByRoom,
+                               c.getRoomPlayInfo,
+                               c.Html,
+                       },
+                       `Note`: { //分区排行
+                               c.Get_HotRank,
+                               c.getInfoByRoom,
+                               c.Html,
+                       },
+                       `Locked`: { //直播间是否被封禁
+                               c.getInfoByRoom,
+                               c.Html,
+                       },
+                       `Live_qn`: { //当前直播流质量
+                               c.getRoomPlayInfo,
+                               c.Html,
+                       },
+                       `AcceptQn`: { //允许的清晰度
+                               c.getRoomPlayInfo,
+                               c.Html,
+                       },
+                       `Live`: { //直播流链接
+                               c.getRoomPlayInfoByQn,
+                               c.getRoomPlayInfo,
+                               c.Html,
+                       },
+                       `Token`: { //弹幕钥
+                               c.getDanmuInfo,
+                       },
+                       `WSURL`: { //弹幕链接
+                               c.getDanmuInfo,
+                       },
+                       // `VERSION`:[]func()([]string){//客户版本  不再需要
+                       //      Get_Version,
+                       // },
+                       `LIVE_BUVID`: { //LIVE_BUVID
+                               c.Get_LIVE_BUVID,
+                       },
 
-               `Silver_2_coin`: { //银瓜子2硬币
-                       Silver_2_coin,
-               },
-               `CheckSwitch_FansMedal`: { //切换粉丝牌
-                       CheckSwitch_FansMedal,
-               },
-       }
-       var check = map[string]func() bool{
-               `Uid`: func() bool { //用戶uid
-                       return c.Uid != 0
-               },
-               `UpUid`: func() bool { //主播uid
-                       return c.UpUid != 0
-               },
-               `Live_Start_Time`: func() bool { //直播开始时间
-                       return c.Live_Start_Time != time.Time{}
-               },
-               `Liveing`: func() bool { //是否在直播
-                       return true
-               },
-               `Title`: func() bool { //直播间标题
-                       return c.Title != ``
-               },
-               `Uname`: func() bool { //主播名
-                       return c.Uname != ``
-               },
-               `ParentAreaID`: func() bool { //分区
-                       return c.ParentAreaID != 0
-               },
-               `AreaID`: func() bool { //子分区
-                       return c.AreaID != 0
-               },
-               `Roomid`: func() bool { //房间id
-                       return c.Roomid != 0
-               },
-               `GuardNum`: func() bool { //舰长数
-                       return c.GuardNum != 0
-               },
-               `Note`: func() bool { //分区排行
-                       return c.Note != ``
-               },
-               `Locked`: func() bool { //直播间是否被封禁
-                       return true
-               },
-               `Live_qn`: func() bool { //当前直播流质量
-                       return c.Live_qn != 0
-               },
-               `AcceptQn`: func() bool { //允许的清晰度
-                       return len(c.AcceptQn) != 0
-               },
-               `Live`: func() bool { //直播流链接
-                       return len(c.Live) != 0
-               },
-               `Token`: func() bool { //弹幕钥
-                       return c.Token != ``
-               },
-               `WSURL`: func() bool { //弹幕链接
-                       return len(c.WSURL) != 0
-               },
-               // `VERSION`:func()(bool){//客户版本  不再需要
-               //      return c.VERSION != `2.0.11`
-               // },
-               `LIVE_BUVID`: func() bool { //LIVE_BUVID
-                       return c.LIVE_BUVID
-               },
-               `Silver_2_coin`: func() bool { //银瓜子2硬币
-                       return true
-               },
-               `CheckSwitch_FansMedal`: func() bool { //切换粉丝牌
-                       return true
-               },
-               `Cookie`: func() bool { //Cookie
-                       return true
-               },
-       }
+                       `Silver_2_coin`: { //银瓜子2硬币
+                               c.Silver_2_coin,
+                       },
+                       `CheckSwitch_FansMedal`: { //切换粉丝牌
+                               c.CheckSwitch_FansMedal,
+                       },
+               }
+               check = map[string]func() bool{
+                       `Uid`: func() bool { //用戶uid
+                               return c.Uid != 0
+                       },
+                       `UpUid`: func() bool { //主播uid
+                               return c.UpUid != 0
+                       },
+                       `Live_Start_Time`: func() bool { //直播开始时间
+                               return c.Live_Start_Time != time.Time{}
+                       },
+                       `Liveing`: func() bool { //是否在直播
+                               return true
+                       },
+                       `Title`: func() bool { //直播间标题
+                               return c.Title != ``
+                       },
+                       `Uname`: func() bool { //主播名
+                               return c.Uname != ``
+                       },
+                       `ParentAreaID`: func() bool { //分区
+                               return c.ParentAreaID != 0
+                       },
+                       `AreaID`: func() bool { //子分区
+                               return c.AreaID != 0
+                       },
+                       `Roomid`: func() bool { //房间id
+                               return c.Roomid != 0
+                       },
+                       `GuardNum`: func() bool { //舰长数
+                               return c.GuardNum != 0
+                       },
+                       `Note`: func() bool { //分区排行
+                               return c.Note != ``
+                       },
+                       `Locked`: func() bool { //直播间是否被封禁
+                               return true
+                       },
+                       `Live_qn`: func() bool { //当前直播流质量
+                               return c.Live_qn != 0
+                       },
+                       `AcceptQn`: func() bool { //允许的清晰度
+                               return len(c.AcceptQn) != 0
+                       },
+                       `Live`: func() bool { //直播流链接
+                               return len(c.Live) != 0
+                       },
+                       `Token`: func() bool { //弹幕钥
+                               return c.Token != ``
+                       },
+                       `WSURL`: func() bool { //弹幕链接
+                               return len(c.WSURL) != 0
+                       },
+                       // `VERSION`:func()(bool){//客户版本  不再需要
+                       //      return c.VERSION != `2.0.11`
+                       // },
+                       `LIVE_BUVID`: func() bool { //LIVE_BUVID
+                               return c.LIVE_BUVID
+                       },
+                       `Silver_2_coin`: func() bool { //银瓜子2硬币
+                               return true
+                       },
+                       `CheckSwitch_FansMedal`: func() bool { //切换粉丝牌
+                               return true
+                       },
+                       `Cookie`: func() bool { //Cookie
+                               return true
+                       },
+               }
+       )
 
        if fList, ok := api_can_get[key]; ok {
                for _, fItem := range fList {
@@ -208,7 +214,7 @@ func Get(key string) {
                                                apilog.L(`W: `, `missKey equrt key`, key, missKey)
                                                continue
                                        }
-                                       Get(misskeyitem)
+                                       c.Get(misskeyitem)
                                }
                                missKey := fItem()
                                if len(missKey) > 0 {
@@ -223,7 +229,7 @@ func Get(key string) {
        }
 }
 
-func GetUid() (missKey []string) {
+func (c *GetFunc) GetUid() (missKey []string) {
        if uid, ok := c.Cookie.LoadV(`DedeUserID`).(string); !ok { //cookie中无DedeUserID
                missKey = append(missKey, `Cookie`)
        } else if uid, e := strconv.Atoi(uid); e != nil {
@@ -234,40 +240,7 @@ func GetUid() (missKey []string) {
        return
 }
 
-func Info(UpUid int) (info J.Info) {
-       apilog := apilog.Base_add(`Info`)
-       if api_limit.TO() {
-               return
-       } //超额请求阻塞,超时将取消
-
-       //html
-       {
-               req := reqf.New()
-               if err := req.Reqf(reqf.Rval{
-                       Url:     `https://api.bilibili.com/x/space/acc/info?mid=` + strconv.Itoa(UpUid) + `&jsonp=jsonp`,
-                       Proxy:   c.Proxy,
-                       Timeout: 10 * 1000,
-                       Retry:   2,
-               }); err != nil {
-                       apilog.L(`E: `, err)
-                       return
-               }
-
-               //Info
-               {
-                       if e := json.Unmarshal(req.Respon, &info); e != nil {
-                               apilog.L(`E: `, e)
-                               return
-                       } else if info.Code != 0 {
-                               apilog.L(`E: `, info.Message)
-                               return
-                       }
-               }
-       }
-       return
-}
-
-func Html() (missKey []string) {
+func (c *GetFunc) Html() (missKey []string) {
        apilog := apilog.Base_add(`html`)
 
        if c.Roomid == 0 {
@@ -436,12 +409,12 @@ func Html() (missKey []string) {
        return
 }
 
-func missRoomId() (missKey []string) {
+func (c *GetFunc) missRoomId() (missKey []string) {
        apilog.Base_add(`missRoomId`).L(`E: `, `missRoomId`)
        return
 }
 
-func getInfoByRoom() (missKey []string) {
+func (c *GetFunc) getInfoByRoom() (missKey []string) {
        apilog := apilog.Base_add(`getInfoByRoom`)
 
        if c.Roomid == 0 {
@@ -516,7 +489,7 @@ func getInfoByRoom() (missKey []string) {
        return
 }
 
-func getRoomPlayInfo() (missKey []string) {
+func (c *GetFunc) getRoomPlayInfo() (missKey []string) {
        apilog := apilog.Base_add(`getRoomPlayInfo`)
 
        if c.Roomid == 0 {
@@ -671,7 +644,7 @@ func getRoomPlayInfo() (missKey []string) {
        return
 }
 
-func getRoomPlayInfoByQn() (missKey []string) {
+func (c *GetFunc) getRoomPlayInfoByQn() (missKey []string) {
        apilog := apilog.Base_add(`getRoomPlayInfoByQn`)
 
        if c.Roomid == 0 {
@@ -853,7 +826,7 @@ func getRoomPlayInfoByQn() (missKey []string) {
        return
 }
 
-func getDanmuInfo() (missKey []string) {
+func (c *GetFunc) getDanmuInfo() (missKey []string) {
        apilog := apilog.Base_add(`getDanmuInfo`)
 
        if c.Roomid == 0 {
@@ -953,7 +926,7 @@ func Get_face_src(uid string) string {
        return rface.(string) + `@58w_58h`
 }
 
-func Get_HotRank() (missKey []string) {
+func (c *GetFunc) Get_HotRank() (missKey []string) {
        apilog := apilog.Base_add(`Get_HotRank`)
 
        if c.UpUid == 0 {
@@ -1028,7 +1001,7 @@ func Get_HotRank() (missKey []string) {
        return
 }
 
-func Get_guardNum() (missKey []string) {
+func (c *GetFunc) Get_guardNum() (missKey []string) {
        apilog := apilog.Base_add(`Get_guardNum`)
 
        if c.UpUid == 0 {
@@ -1158,11 +1131,44 @@ func Get_guardNum() (missKey []string) {
 //     return
 // }
 
+func Info(UpUid int) (info J.Info) {
+       apilog := apilog.Base_add(`Info`)
+       if api_limit.TO() {
+               return
+       } //超额请求阻塞,超时将取消
+
+       //html
+       {
+               req := reqf.New()
+               if err := req.Reqf(reqf.Rval{
+                       Url:     `https://api.bilibili.com/x/space/acc/info?mid=` + strconv.Itoa(UpUid) + `&jsonp=jsonp`,
+                       Proxy:   c.Proxy,
+                       Timeout: 10 * 1000,
+                       Retry:   2,
+               }); err != nil {
+                       apilog.L(`E: `, err)
+                       return
+               }
+
+               //Info
+               {
+                       if e := json.Unmarshal(req.Respon, &info); e != nil {
+                               apilog.L(`E: `, e)
+                               return
+                       } else if info.Code != 0 {
+                               apilog.L(`E: `, info.Message)
+                               return
+                       }
+               }
+       }
+       return
+}
+
 //调用记录
 var boot_Get_cookie funcCtrl.FlashFunc //新的替代旧的
 
 //扫码登录
-func Get_cookie() (missKey []string) {
+func (c *GetFunc) Get_cookie() (missKey []string) {
        if v, ok := c.K_v.LoadV(`扫码登录`).(bool); !ok || !v {
                return
        }
@@ -1512,7 +1518,7 @@ func Get_weared_medal() (item J.GetWearedMedal_Data) {
 
 }
 
-func CheckSwitch_FansMedal() (missKey []string) {
+func (c *GetFunc) CheckSwitch_FansMedal() (missKey []string) {
 
        if !c.LIVE_BUVID {
                missKey = append(missKey, `LIVE_BUVID`)
@@ -1732,7 +1738,7 @@ func Dosign() {
 }
 
 //LIVE_BUVID
-func Get_LIVE_BUVID() (missKey []string) {
+func (c *GetFunc) Get_LIVE_BUVID() (missKey []string) {
        apilog := apilog.Base_add(`LIVE_BUVID`).L(`T: `, `获取`)
 
        if live_buvid, ok := c.Cookie.LoadV(`LIVE_BUVID`).(string); ok && live_buvid != `` {
@@ -2166,7 +2172,7 @@ func Gift_list() (list []Gift_list_type_Data_List) {
 }
 
 //银瓜子2硬币
-func Silver_2_coin() (missKey []string) {
+func (c *GetFunc) Silver_2_coin() (missKey []string) {
        apilog := apilog.Base_add(`银瓜子=>硬币`)
 
        if !c.LIVE_BUVID {
diff --git a/Reply/steam/stream.go b/Reply/steam/stream.go
new file mode 100644 (file)
index 0000000..5269aa4
--- /dev/null
@@ -0,0 +1,49 @@
+package stream
+
+import (
+       "path/filepath"
+
+       c "github.com/qydysky/bili_danmu/CV"
+
+       log "github.com/qydysky/part/log"
+       signal "github.com/qydysky/part/signal"
+)
+
+type Stream struct {
+       Status *signal.Signal //IsLive()是否运行中
+       log    *log.Log_interface
+       config Stream_Config //配置
+}
+
+type Stream_Config struct {
+       save_path     string //直播流保存目录
+       want_qn       int    //直播流清晰度
+       want_type     string //直播流类型
+       bufsize       int    //直播hls流缓冲
+       banlance_host bool   //直播hls流均衡
+}
+
+func (t *Stream) LoadConfig() {
+       //读取配置
+       if path, ok := c.K_v.LoadV("直播流保存位置").(string); ok {
+               if path, err := filepath.Abs(path); err == nil {
+                       t.config.save_path = path + "/"
+               }
+       }
+       if v, ok := c.K_v.LoadV(`直播hls流缓冲`).(float64); ok && v > 0 {
+               t.config.bufsize = int(v)
+       }
+       if v, ok := c.K_v.LoadV(`直播hls流均衡`).(bool); ok {
+               t.config.banlance_host = v
+       }
+       if v, ok := c.K_v.LoadV(`直播流清晰度`).(int); ok {
+               t.config.want_qn = v
+       }
+       if v, ok := c.K_v.LoadV(`直播流类型`).(string); ok {
+               t.config.want_type = v
+       }
+}
+
+func (t *Stream) Start() {
+       t.log = c.Log.Base(`直播流保存`)
+}