예제 #1
0
        public void StartRecord()
        {
            var num = 0;

            new Thread(() =>
            {
                while (true)
                {
                    var info = YoutubeApi.GetLiveChatInfo(LiveChatId);
                    if (info.PollingInterval == 0 && info.CommentsToken == null)
                    {
                        LiveStoppedEvent?.Invoke(LiveChatId, this);
                        break;
                    }
                    var comments = info.GetComments().Where(comment => RecordedComments.All(v => v.CommentId != comment.CommentId)).ToList();
                    if (comments.Any())
                    {
                        comments.ForEach(v => v.VideoId = VideoId);
                        try
                        {
                            _chatCollection.InsertMany(comments, new InsertManyOptions()
                            {
                                IsOrdered = false
                            });
                            RecordedComments.AddRange(comments);
                        }
                        catch (MongoBulkWriteException)
                        {
                            Thread.Sleep(10000);
                        }
                    }
                    if (++num % 20 == 0)
                    {
                        if (!CacheManager.Manager.LastCheckLiveStatus[Vtuber].IsLive)
                        {
                            LiveStoppedEvent?.Invoke(LiveChatId, this);
                            break;
                        }
                    }
                    Thread.Sleep(info.PollingInterval);
                }
            }).Start();
        }
예제 #2
0
        public void StartRecord()
        {
            var num = 0;

            Client = new LiveClient(RoomId);
            if (!Client.ConnectAsync().GetAwaiter().GetResult())
            {
                LogHelper.Error("Cannot connect to danmaku server.");
                return;
            }

            StillLive                     = true;
            Client.GotGiftEvent          += GotGiftEvent;
            Client.GotDanmuEvent         += GotDanmakuEvent;
            Client.SocketDisconnectEvent += client =>
            {
                if (StillLive)
                {
                    client.ConnectAsync().GetAwaiter().GetResult();
                }
            };
            Client.LiveStoppedEvent += client =>
            {
                StillLive = false;
                client.CloseConnect();
                LiveStoppedEvent?.Invoke(this);
            };
            new Thread(() =>
            {
                while (true)
                {
                    Thread.Sleep(10000);
                    if (!CacheManager.Manager.LastCheckLiveBStatus[Vtuber].AreLive)
                    {
                        StillLive = false;
                        Client.CloseConnect();
                        LiveStoppedEvent?.Invoke(this);
                        break;
                    }
                }
            }).Start();
        }
예제 #3
0
 public async Task BeginRecordAsync()
 {
     LiveClient = new BilibiliLiveClient(RoomId);
     if (!await LiveClient.ConnectAsync())
     {
         LogHelper.Error("Cannot connect to danmaku server. roomId:" + RoomId);
         return;
     }
     OnRecord = true;
     LiveClient.GotDanmakuEvent       += GotDanmakuEvent;
     LiveClient.GotGiftEvent          += GotGiftEvent;
     LiveClient.SocketDisconnectEvent += client =>
     {
         if (OnRecord)
         {
             client.ConnectAsync().GetAwaiter().GetResult();
         }
     };
     LiveClient.LiveStoppedEvent += client => LiveStoppedEvent?.Invoke(this);
 }
예제 #4
0
        private void ProcessCommand(byte[] playLoad)
        {
            var json = JObject.Parse(Encoding.UTF8.GetString(playLoad));
            var a    = json.ToString();
            var cmd  = json["cmd"].ToObject <string>();

            if (cmd.StartsWith("SEND_GIFT"))//礼物
            {
                var giftInfo = json["data"].ToObject <BilibiliGiftInfo>();
                GotGiftEvent?.Invoke(giftInfo);
            }
            else if (cmd.StartsWith("DANMU_MSG"))
            {
                var info = new BilibiliDanmakuInfo()
                {
                    Username    = json["info"][2][1].ToString(),
                    Userid      = json["info"][2][0].ToObject <long>(),
                    Suffix      = json["info"][3].Any() ? json["info"][3][1].ToString() : string.Empty,
                    SuffixRoom  = json["info"][3].Any() ? json["info"][3][2].ToString() : string.Empty,
                    SuffixLevel = json["info"][3].Any() ? json["info"][3][0].ToObject <int>() : 0,
                    Message     = json["info"][1].ToString(),
                    IsAdmin     = json["info"][2][2].ToString() == "1",
                    IsVip       = json["info"][2][3].ToString() == "1"
                };
                GotDanmakuEvent?.Invoke(info);
            }
            else if (cmd.StartsWith("GUARD_BUY"))
            {
                var userGuardLevel = json["data"]["guard_level"].ToObject <int>();
                var guardInfo      = new BilibiliGiftInfo()
                {
                    Userid   = json["data"]["uid"].ToObject <long>(),
                    Username = json["data"]["username"].ToString(),
                    GiftName = userGuardLevel == 3 ? "舰长" :
                               userGuardLevel == 2 ? "提督" :
                               userGuardLevel == 1 ? "总督" : string.Empty,
                    CoinType = "gold",
                    CostCoin = userGuardLevel == 3 ? 190000 :
                               userGuardLevel == 2 ? 2000000 :
                               userGuardLevel == 1 ? 20000000 : 0,
                    Count = json["data"]["num"].ToObject <int>()
                };
                GotGiftEvent?.Invoke(guardInfo);
            }
            else if (cmd.StartsWith("LIVE"))
            {
                LiveBeginEvent?.Invoke(this);
            }
            else if (cmd.StartsWith("PREPARING"))
            {
                LiveStoppedEvent?.Invoke(this);
            }

            /*
             * switch (cmd)
             * {
             *  case "SEND_GIFT":
             *      var giftInfo = json["data"].ToObject<BilibiliGiftInfo>();
             *      GotGiftEvent?.Invoke(giftInfo);
             *      break;
             *  case "DANMU_MSG": //弹幕
             *      var info = new BilibiliDanmakuInfo()
             *      {
             *          Username = json["info"][2][1].ToString(),
             *          Userid = json["info"][2][0].ToObject<long>(),
             *          Suffix = json["info"][3].Any() ? json["info"][3][1].ToString() : string.Empty,
             *          SuffixRoom = json["info"][3].Any() ? json["info"][3][2].ToString() : string.Empty,
             *          SuffixLevel = json["info"][3].Any() ? json["info"][3][0].ToObject<int>() : 0,
             *          Message = json["info"][1].ToString(),
             *          IsAdmin = json["info"][2][2].ToString() == "1",
             *          IsVip = json["info"][2][3].ToString() == "1"
             *      };
             *      GotDanmakuEvent?.Invoke(info);
             *      break;
             *  case "GUARD_BUY": //购买舰长
             *      var userGuardLevel = json["data"]["guard_level"].ToObject<int>();
             *      var guardInfo = new BilibiliGiftInfo()
             *      {
             *          Userid = json["data"]["uid"].ToObject<long>(),
             *          Username = json["data"]["username"].ToString(),
             *          GiftName = userGuardLevel == 3 ? "舰长" :
             *              userGuardLevel == 2 ? "提督" :
             *              userGuardLevel == 1 ? "总督" : string.Empty,
             *          CoinType = "gold",
             *          CostCoin = userGuardLevel == 3 ? 190000 :
             *              userGuardLevel == 2 ? 2000000 :
             *              userGuardLevel == 1 ? 20000000 : 0,
             *          Count = json["data"]["num"].ToObject<int>()
             *      };
             *      GotGiftEvent?.Invoke(guardInfo);
             *      break;
             *  case "LIVE":
             *      LiveBeginEvent?.Invoke(this);
             *      break;
             *  case "PREPARING":
             *      LiveStoppedEvent?.Invoke(this);
             *      break;
             *  default:
             *      //Console.WriteLine(json.ToString(Formatting.Indented));
             *      break;
             * }
             */
        }
        public void StartRecord()
        {
            var num = 0;

            new Thread(() =>
            {
                while (true)
                {
                    var info = YoutubeApi.GetLiveChatInfoAsync(LiveChatId).GetAwaiter().GetResult();
                    if (info == null || (info.PollingInterval == 0 && info.CommentsToken == null))
                    {
                        LiveStoppedEvent?.Invoke(LiveChatId, this);
                        break;
                    }
                    var channelInfo = YoutubeApi.GetYoutubeChannelAsync(Vtuber.YoutubeChannelId).Retry(5).GetAwaiter()
                                      .GetResult();
                    var comments = info.GetComments().Where(comment => RecordedLiveChats.All(v => v.CommentId != comment.CommentId)).ToList();
                    if (channelInfo != null && channelInfo.LiveViewerCount > 0)
                    {
                        comments.ForEach(v => v.ViewerCount = channelInfo.LiveViewerCount);
                    }
                    if (comments.Any())
                    {
                        comments.ForEach(v => v.VideoId = VideoId);
                        try
                        {
                            _chatCollection.InsertMany(comments, new InsertManyOptions()
                            {
                                IsOrdered = false
                            });
                            RecordedLiveChats.AddRange(comments);
                            if (VtuberList != null)
                            {
                                var comment = comments.Where(v => VtuberList.Any(t => t.YoutubeChannelId == v.AuthorChannelId));
                                foreach (var chat in comment)
                                {
                                    var target = VtuberList.First(v => v.YoutubeChannelId == chat.AuthorChannelId);
                                    VtuberCommentedEvent?.Invoke(target, chat, this);
                                }
                            }
                        }
                        catch (Exception)
                        {
                            Thread.Sleep(10000);
                        }
                    }
                    if (++num % 10 == 0)
                    {
                        if (RecordedLiveChats.Count > 100)
                        {
                            RecordedLiveChats.RemoveRange(0, RecordedLiveChats.Count - 100);
                        }
                        if (!CacheManager.Manager.LastCheckYoutubeLiveStatus[Vtuber].IsLive || CacheManager.Manager.LastCheckYoutubeLiveStatus[Vtuber].VideoId != VideoId)
                        {
                            LiveStoppedEvent?.Invoke(LiveChatId, this);
                            break;
                        }
                    }
                    Thread.Sleep(info.PollingInterval < 2000 ? info.PollingInterval : info.PollingInterval - 1000);
                }
            }).Start();
        }