]> 127.0.0.1 Git - bili_danmu/.git/commitdiff
银瓜子礼物自动赠送
authorqydysky <qydysky@foxmail.com>
Fri, 5 Mar 2021 16:37:41 +0000 (00:37 +0800)
committerqydysky <qydysky@foxmail.com>
Fri, 5 Mar 2021 16:37:41 +0000 (00:37 +0800)
CV/Var.go
F/api.go
Reply/F.go
Send/README.md
Send/Send_gift.go
bili_danmu.go
demo/config/config_K_v.json

index 5c3ff0c104413b35a328137989f65900d8327e9a..bdfd6351f6c1dd1e38d49c2852f937d5a54e7200 100644 (file)
--- a/CV/Var.go
+++ b/CV/Var.go
@@ -18,6 +18,7 @@ var (
        Cookie syncmap.Map
        Title string//直播标题
        Uname string//主播名
+       UpUid int//主播uid
        Rev float64//营收
        Renqi int//人气
        GuardNum int//舰长数
index e75fd7d1aeadbb785e79f518747517e4fda7a335..4b58d14429278c3e7cbd43658c9fba90beaa5fa6 100644 (file)
--- a/F/api.go
+++ b/F/api.go
@@ -34,7 +34,7 @@ type api struct {
 }
 
 var apilog = c.Log.Base(`api`)
-var api_limit = p.Limit(1,2000,15000)//频率限制1次/2s,最大等待时间15s
+var api_limit = p.Limit(1,2000,30000)//频率限制1次/2s,最大等待时间30s
 
 func New_api(Roomid int) (o *api) {
        apilog.Base_add(`新建`).L(`T: `,"ok")
@@ -70,6 +70,7 @@ func (i *api) Get_info() (o *api) {
                apilog.L(`E: `,"uid", err)
        } else {
                o.Uid = i
+               c.UpUid = i
        }
        //Title
        if e := r.S(`"title":"`, `",`, 0, 0).Err;e == nil {
@@ -163,6 +164,7 @@ func (i *api) Get_info() (o *api) {
                //主播id
                if tmp.Data.Room_info.Uid != 0{
                        o.Uid = tmp.Data.Room_info.Uid
+                       c.UpUid = tmp.Data.Room_info.Uid
                } else {
                        apilog.L(`W: `,"data.room_info.Uid = 0")
                        return
@@ -1558,7 +1560,9 @@ type Gift_list_type_Data struct {
 }
 
 type Gift_list_type_Data_List struct{
+       Bag_id int `json:"bag_id"`
        Gift_id int `json:"gift_id"`
+       Gift_name string `json:"gift_name"`
        Gift_num int `json:"gift_num"`
        Expire_at int `json:"expire_at"`
 }
index 5f7c314d853a2b2734ff6b099cc08cbb34351de4..542955a875fb1c1f54ea09e2b152dc216585b8bb 100644 (file)
@@ -784,4 +784,21 @@ func Keep_medal_light() {
                time.Sleep(time.Second)
        }
        flog.L(`I: `,`完成`)
+}
+
+//自动发送即将过期的银瓜子礼物
+func AutoSend_silver_gift() {
+       day,_ := c.K_v.LoadV(`发送还有几天过期的礼物`).(float64)
+       if day <= 0 {
+               return
+       }
+
+       flog := flog.Base_add(`自动送礼`).L(`T: `,`自动送礼`)
+
+       for _,v := range F.Gift_list() {
+               if time.Now().Add(time.Hour * time.Duration(24 * int(day))).Unix() > int64(v.Expire_at) {
+                       send.Send_gift(v.Gift_id, v.Bag_id, v.Gift_num)
+               }
+       }
+       flog.L(`T: `,`完成`)
 }
\ No newline at end of file
index 60f1f2c33951adfeb0b7e67b3c52d1aa6af9af19..f1fda92ab8873b737dc779b3b909793118c2f50f 100644 (file)
@@ -6,4 +6,5 @@
 |./|发送数据区|
 |Send.go|弹幕发送|
 |Send_pm.go|私信发送|
+|Send_gift.go|礼物发送|
 ---
\ No newline at end of file
index 521a7a5245d64c54416e9da5bbc1f5c32adbaac8..5fac6f6d227508ca4b274677dd2a0683c8dee2f9 100644 (file)
 package send
 
-func Send_gift(){
-       {//参数来源1
-               //https://api.live.bilibili.com/xlive/web-room/v1/gift/bag_list?t=1614667412767&room_id=22347054
+import (
+       "time"
+       "net/url"
+       "encoding/json"
+       "strconv"
+
+       p "github.com/qydysky/part"
+       c "github.com/qydysky/bili_danmu/CV"
+)
+
+//每2s一个令牌,最多等10秒
+var gift_limit = p.Limit(1, 2000, 10000)
+
+func Send_gift(gift_id,bag_id,gift_num int) {
+       log := c.Log.Base_add(`发送礼物`)
+
+       if gift_limit.TO() {log.L(`W: `,"超时");return}
 
-               /*
-               {
-                       "code": 0,
-                       "message": "0",
-                       "ttl": 1,
-                       "data": {
-                               "list": [
-                                       {
-                                               "bag_id": 235312419,
-                                               "gift_id": 1,
-                                               "gift_name": "辣条",
-                                               "gift_num": 1,
-                                               "gift_type": 5,
-                                               "expire_at": 1614700800,
-                                               "corner_mark": "1天",
-                                               "corner_color": "",
-                                               "count_map": [
-                                                       {
-                                                               "num": 1,
-                                                               "text": ""
-                                                       }
-                                               ],
-                                               "bind_roomid": 0,
-                                               "bind_room_text": "",
-                                               "type": 1,
-                                               "card_image": "",
-                                               "card_gif": "",
-                                               "card_id": 0,
-                                               "card_record_id": 0,
-                                               "is_show_send": false
-                                       },
-                                       {
-                                               "bag_id": 235157184,
-                                               "gift_id": 30607,
-                                               "gift_name": "小心心",
-                                               "gift_num": 16,
-                                               "gift_type": 5,
-                                               "expire_at": 1615046400,
-                                               "corner_mark": "5天",
-                                               "corner_color": "",
-                                               "count_map": [
-                                                       {
-                                                               "num": 1,
-                                                               "text": ""
-                                                       },
-                                                       {
-                                                               "num": 16,
-                                                               "text": "全部"
-                                                       }
-                                               ],
-                                               "bind_roomid": 0,
-                                               "bind_room_text": "",
-                                               "type": 1,
-                                               "card_image": "",
-                                               "card_gif": "",
-                                               "card_id": 0,
-                                               "card_record_id": 0,
-                                               "is_show_send": false
-                                       },
-                                       {
-                                               "bag_id": 235162609,
-                                               "gift_id": 30607,
-                                               "gift_name": "小心心",
-                                               "gift_num": 24,
-                                               "gift_type": 5,
-                                               "expire_at": 1615132800,
-                                               "corner_mark": "6天",
-                                               "corner_color": "",
-                                               "count_map": [
-                                                       {
-                                                               "num": 1,
-                                                               "text": ""
-                                                       },
-                                                       {
-                                                               "num": 24,
-                                                               "text": "全部"
-                                                       }
-                                               ],
-                                               "bind_roomid": 0,
-                                               "bind_room_text": "",
-                                               "type": 1,
-                                               "card_image": "",
-                                               "card_gif": "",
-                                               "card_id": 0,
-                                               "card_record_id": 0,
-                                               "is_show_send": false
-                                       },
-                                       {
-                                               "bag_id": 235077495,
-                                               "gift_id": 1,
-                                               "gift_name": "辣条",
-                                               "gift_num": 20,
-                                               "gift_type": 5,
-                                               "expire_at": 1615132800,
-                                               "corner_mark": "6天",
-                                               "corner_color": "",
-                                               "count_map": [
-                                                       {
-                                                               "num": 1,
-                                                               "text": ""
-                                                       },
-                                                       {
-                                                               "num": 20,
-                                                               "text": "全部"
-                                                       }
-                                               ],
-                                               "bind_roomid": 0,
-                                               "bind_room_text": "",
-                                               "type": 1,
-                                               "card_image": "",
-                                               "card_gif": "",
-                                               "card_id": 0,
-                                               "card_record_id": 0,
-                                               "is_show_send": false
-                                       },
-                                       {
-                                               "bag_id": 235282499,
-                                               "gift_id": 30607,
-                                               "gift_name": "小心心",
-                                               "gift_num": 3,
-                                               "gift_type": 5,
-                                               "expire_at": 1615219200,
-                                               "corner_mark": "7天",
-                                               "corner_color": "",
-                                               "count_map": [
-                                                       {
-                                                               "num": 1,
-                                                               "text": ""
-                                                       },
-                                                       {
-                                                               "num": 3,
-                                                               "text": "全部"
-                                                       }
-                                               ],
-                                               "bind_roomid": 0,
-                                               "bind_room_text": "",
-                                               "type": 1,
-                                               "card_image": "",
-                                               "card_gif": "",
-                                               "card_id": 0,
-                                               "card_record_id": 0,
-                                               "is_show_send": false
-                                       }
-                               ],
-                               "time": 0
-                       }
-               }
-               */
-       }
        {//发送请求(银瓜子礼物)
-               //https://api.live.bilibili.com/gift/v2/live/bag_send
+               csrf := c.Cookie.LoadV(`bili_jct`).(string)
+               if csrf == `` {log.L(`E: `,"Cookie错误,无bili_jct=");return}
 
-               // req
-               // {
-               //      "uid": "29183321",//客户uid
-               //      "gift_id": "1",//礼物id from 1
-               //      "ruid": "623441612",//主播uid
-               //      "send_ruid": "0",//固定值
-               //      "gift_num": "1",//发送数量 from 1
-               //      "bag_id": "235312419",//礼物包 from 1
-               //      "platform": "pc",//平台固定值
-               //      "biz_code": "live",//固定值
-               //      "biz_id": "22347054",//房间id
-               //      "rnd": "1614667054",//时间戳
-               //      "storm_beat_id": "0",//固定值
-               //      "metadata": "",//固定值
-               //      "price": "0",//价值
-               //      "csrf_token": "××",//from cookie
-               //      "csrf": "××",//from cookie
-               //      "visit_id": ""//固定值
-               // }
+               var sendStr = 
+               `uid=`+strconv.Itoa(c.Uid)+`&`+
+               `gift_id=`+strconv.Itoa(gift_id)+`&`+
+               `ruid=`+strconv.Itoa(c.UpUid)+`&`+
+               `send_ruid=0&`+
+               `gift_num=`+strconv.Itoa(gift_num)+`&`+
+               `coin_type=silver&`+
+               `bag_id=`+strconv.Itoa(bag_id)+`&`+
+               `platform=pc&`+
+               `biz_code=live&`+
+               `biz_id=`+strconv.Itoa(c.Roomid)+`&`+
+               `rnd=`+strconv.Itoa(int(time.Now().Unix()))+`&`+
+               `storm_beat_id=0&`+
+               `metadata=&`+
+               `price=0&`+
+               `csrf_token=`+csrf+`&`+
+               `csrf=`+csrf+`&`+
+               `visit_id=`
 
-               // res
-               // {
-               //      "code": 0,
-               //      "msg": "success",
-               //      "message": "success",
-               //      "data": {
-               //              "tid": "1614667076110100001",
-               //              "uid": 29183321,
-               //              "uname": "qydysky丶",
-               //              "face": "https://i2.hdslb.com/bfs/face/8c6d1ce3f96dc86d0fc7876a2824910c92ae6802.jpg",
-               //              "guard_level": 0,
-               //              "ruid": 623441612,
-               //              "rcost": 18194089,
-               //              "gift_id": 1,
-               //              "gift_type": 5,
-               //              "gift_name": "辣条",
-               //              "gift_num": 1,
-               //              "gift_action": "投喂",
-               //              "gift_price": 100,
-               //              "coin_type": "silver",
-               //              "total_coin": 100,
-               //              "pay_coin": 100,
-               //              "metadata": "",
-               //              "fulltext": "",
-               //              "rnd": "1614667054",
-               //              "tag_image": "",
-               //              "effect_block": 1,
-               //              "extra": {
-               //                      "wallet": null,
-               //                      "gift_bag": {
-               //                              "bag_id": 235312419,
-               //                              "gift_num": 1
-               //                      },
-               //                      "top_list": [],
-               //                      "follow": null,
-               //                      "medal": null,
-               //                      "title": null,
-               //                      "pk": {
-               //                              "pk_gift_tips": "",
-               //                              "crit_prob": -1
-               //                      },
-               //                      "fulltext": "",
-               //                      "event": {
-               //                              "event_score": 0,
-               //                              "event_redbag_num": 0
-               //                      },
-               //                      "capsule": null,
-               //                      "lottery_id": ""
-               //              },
-               //              "blow_switch": 0,
-               //              "send_tips": "赠送成功",
-               //              "discount_id": 0,
-               //              "gift_effect": {
-               //                      "super": 0,
-               //                      "combo_timeout": 0,
-               //                      "super_gift_num": 0,
-               //                      "super_batch_gift_num": 0,
-               //                      "batch_combo_id": "",
-               //                      "broadcast_msg_list": [],
-               //                      "small_tv_list": [],
-               //                      "beat_storm": null,
-               //                      "combo_id": "",
-               //                      "smallTVCountFlag": true
-               //              },
-               //              "send_master": null,
-               //              "crit_prob": -1,
-               //              "combo_stay_time": 3,
-               //              "combo_total_coin": 0,
-               //              "demarcation": 1,
-               //              "magnification": 1,
-               //              "combo_resources_id": 1,
-               //              "is_special_batch": 0,
-               //              "send_gift_countdown": 6,
-               //              "bp_cent_balance": 0,
-               //              "price": 0,
-               //              "left_num": 0,
-               //              "need_num": 0,
-               //              "available_num": 0
-               //      }
-               // }
+               Cookie := make(map[string]string)
+               c.Cookie.Range(func(k,v interface{})(bool){
+                       Cookie[k.(string)] = v.(string)
+                       return true
+               })
+               
+               req := p.Req()
+               if e:= req.Reqf(p.Rval{
+                       Url:`https://api.live.bilibili.com/gift/v2/Live/send`,
+                       PostStr:url.PathEscape(sendStr),
+                       Timeout:10,
+                       Header:map[string]string{
+                               `Host`: `api.vc.bilibili.com`,
+                               `User-Agent`: `Mozilla/5.0 (X11; Linux x86_64; rv:83.0) Gecko/20100101 Firefox/83.0`,
+                               `Accept`: `application/json, text/javascript, */*; q=0.01`,
+                               `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`,
+                               `Content-Type`: `application/x-www-form-urlencoded; charset=UTF-8`,
+                               `Origin`: `https://message.bilibili.com`,
+                               `Connection`: `keep-alive`,
+                               `Pragma`: `no-cache`,
+                               `Cache-Control`: `no-cache`,
+                               `Referer`:"https://message.bilibili.com",
+                               `Cookie`:p.Map_2_Cookies_String(Cookie),
+                       },
+               });e != nil {
+                       log.L(`E: `,e)
+                       return
+               }
+       
+               var res struct{
+                       Code int `json:"code"`
+                       Message string `json:"message"`
+                       Data struct{
+                               Gift_name string `json:"gift_name"`
+                               Gift_num int `json:"gift_num"`
+                       } `json:"data"`
+               }
+
+               if e := json.Unmarshal(req.Respon, &res);e != nil {
+                       log.L(`E: `,e)
+                       return
+               }
+
+               if res.Code != 0 {
+                       log.L(`W: `,res.Message)
+                       return
+               }
+               log.L(`I: `,`给`,c.Roomid,`赠送了`,res.Data.Gift_num,`个`,res.Data.Gift_name)
        }
 }
index df0bfcb3a7f248b0de45f74912ead55422df919e..21e990419d428b6893b2061531313f5bc34130ab 100644 (file)
@@ -265,6 +265,8 @@ func Demo(roomid ...int) {
                                                                go reply.Entry_danmu()
                                                                //附加功能 保持牌子点亮
                                                                go reply.Keep_medal_light()
+                                                               //附加功能 自动发送即将过期礼物
+                                                               go reply.AutoSend_silver_gift()
                                                                return false
                                                        },
                                                })
@@ -285,6 +287,8 @@ func Demo(roomid ...int) {
                                                        go reply.ShowRevf()
                                                        //附加功能 保持牌子点亮
                                                        go reply.Keep_medal_light()
+                                                       //附加功能 自动发送即将过期礼物
+                                                       go reply.AutoSend_silver_gift()
                                                }
                                        }()
                                }
index c40db6bb1fe5a9cc316c3077e8e70b9b5eebca15..ca83c4a90dae8e09f88079c1a4e7a2a597282c1f 100644 (file)
@@ -31,6 +31,7 @@
     "进房弹幕_仅发首日弹幕":true,
 
     "其他":"",
+    "发送还有几天过期的礼物":3,
     "保持所有牌子亮着":true,
     "扫码登录自动打开标签页":true,
     "日志显示":["T: ","I: ","W: ","E: "],