public void CallYoutubeBeginLive(YoutubeLiveCallbackBody body)
 {
     if (!LiveList.ContainsKey(body.VtuberName))
     {
         LogHelper.Info($"Vtuber [{body.VtuberName}] 已开始直播 {body.LiveLink} ({body.LiveTitle}) -{DateTimeExtensions.TimestampToDateTime(body.ActualStartTime).AddHours(8):yyyy-MM-dd HH:mm:ss}");
         InsertEventLog($"[{DateTimeExtensions.TimestampToDateTime(body.ActualStartTime).AddHours(8):yyyy-MM-dd HH:mm:ss}] Vtuber [{body.VtuberName}] 开始直播 {body.LiveTitle} ({body.LiveLink})");
         if (DateTime.Now - DateTimeExtensions.TimestampToDateTime(body.ActualStartTime).AddHours(8) <
             TimeSpan.FromMinutes(10))
         {
             foreach (var vtuberBot in Bots)
             {
                 var groups = vtuberBot.GetGroupsAsync().GetAwaiter().GetResult();
                 if (groups == null)
                 {
                     continue;
                 }
                 foreach (var groupInfo in groups)
                 {
                     var config = Config.DefaultConfig.GroupConfigs.FirstOrDefault(v => v.GroupId == groupInfo.GroupId)
                                  ?.PublishConfigs.FirstOrDefault(v => v.VtuberName == body.VtuberName);
                     if (config == null || !config.YoutubeBeginLive || LiveList.ContainsKey(body.VtuberName))
                     {
                         continue;
                     }
                     try
                     {
                         vtuberBot.GetSendingService().SendGroupMessageAsync(groupInfo.GroupId,
                                                                             $"{body.VtuberName} 在 {DateTimeExtensions.TimestampToDateTime(body.ActualStartTime).AddHours(8):yyyy-MM-dd HH:mm:ss} 开始了直播 {body.LiveTitle}\r\n" +
                                                                             $"链接: {body.LiveLink}\r\n当前观众数量: {body.ViewersCount}\r\n" +
                                                                             $"原定开播时间: {DateTimeExtensions.TimestampToDateTime(body.ScheduledStartTime).AddHours(8):yyyy-MM-dd HH:mm:ss}\r\n" +
                                                                             $"实际开播时间: {DateTimeExtensions.TimestampToDateTime(body.ActualStartTime).AddHours(8):yyyy-MM-dd HH:mm:ss}")
                         .GetAwaiter().GetResult();
                     }
                     catch (Exception ex)
                     {
                         LogHelper.Error("Cannot send message.", true, ex);
                     }
                 }
             }
         }
         LiveList.Add(body.VtuberName, body.LiveLink);
         VtuberBeginYoutubeLiveEvent?.Invoke(body);
     }
 }
예제 #2
0
        private void YoutubeLiveCheckTimer(IEnumerable <VtuberEntity> vtubers)
        {
            var threadPool = new SimpleThreadPool()
            {
                MaxThread = 20
            };

            foreach (var vtuber in vtubers.Where(v => !string.IsNullOrEmpty(v.YoutubeChannelId)))
            {
                threadPool.Actions.Enqueue(() =>
                {
                    if (!LastCheckYoutubeLiveStatus.ContainsKey(vtuber))
                    {
                        LastCheckYoutubeLiveStatus.Add(vtuber, new YoutubeVideo());
                    }
                    var onLive = YoutubeApi.NowLive(vtuber.YoutubeChannelId).GetAwaiter().GetResult();
                    if (onLive)
                    {
                        var channelInfo = YoutubeApi.GetYoutubeChannelAsync(vtuber.YoutubeChannelId).GetAwaiter().GetResult();
                        if (!LastCheckYoutubeLiveStatus[vtuber].IsLive || channelInfo.LiveVideoId != LastCheckYoutubeLiveStatus[vtuber].VideoId)
                        {
                            var live = YoutubeApi.GetYoutubeVideoAsync(channelInfo.LiveVideoId).GetAwaiter().GetResult();
                            if (!live.IsLive)
                            {
                                return;
                            }
                            if (!LastCheckYoutubeLiveStatus[vtuber].IsLive)
                            {
                                LastCheckYoutubeLiveStatus[vtuber] = live;
                                var recorder = new YoutubeLiveChatRecorder(live.LiveDetails.LiveChatId, vtuber, live.VideoId)
                                {
                                    VtuberList = vtubers.ToList()
                                };
                                recorder.StartRecord();
                                recorder.LiveStoppedEvent += (id, sender) =>
                                {
                                    LogHelper.Info($"{vtuber.OriginalName} 已停止直播,正在保存评论数据");
                                    live     = YoutubeApi.GetYoutubeVideoAsync(channelInfo.LiveVideoId).Retry(5).GetAwaiter().GetResult() ?? live;
                                    var info = new YoutubeLiveInfo()
                                    {
                                        Title     = live.Title,
                                        Channel   = live.ChannelId,
                                        BeginTime = live.LiveDetails?.ActualStartTime ?? default(DateTime),
                                        EndTime   = live.LiveDetails?.ActualEndTime ?? DateTime.Now,
                                        VideoId   = live.VideoId
                                    };
                                    _youtubeLiveCollection.ReplaceOne(v => v.VideoId == live.VideoId, info,
                                                                      new UpdateOptions()
                                    {
                                        IsUpsert = true
                                    });
                                    LogHelper.Info("保存完毕");
                                    VtuberStoppedYoutubeLiveEvent?.Invoke(vtuber, live);
                                };
                                recorder.VtuberCommentedEvent += (author, message, sender) => VtuberCommentedYoutubeLiveEvent?.Invoke(author, sender.Vtuber, message, live);
                                VtuberBeginYoutubeLiveEvent?.Invoke(vtuber, live);
                            }
                        }
                    }
                    if (!onLive)
                    {
                        LastCheckYoutubeLiveStatus[vtuber] = new YoutubeVideo();
                    }
                });
            }
            threadPool.Run();
        }