Beispiel #1
0
 public IActionResult YoutubeStopLiveCallback([FromBody] YoutubeLiveCallbackBody body)
 {
     new Thread(() =>
     {
         var videoId = body.LiveLink.Split('=').Last();
         LogHelper.Info($"将在两个小时后开始爬取 {body.LiveTitle} ({videoId}) 的Live chat replay.");
         Thread.Sleep(1000 * 60 * 60 * 2);
         LogHelper.Info($"开始爬取 {body.LiveTitle} ({videoId}) 的Live chat replay.");
         try
         {
             var replay = YoutubeApi.GetWebLiveChatReplayAsync(videoId).GetAwaiter().GetResult();
             if (replay == null)
             {
                 LogHelper.Info("获取失败.");
                 return;
             }
             Program.Database.GetCollection <YoutubeWebLiveChat>("youtube-web-live-chats").InsertMany(replay);
             LogHelper.Info($"爬取完成,已保存 {replay.Count()} 条live chat.");
         }
         catch (Exception ex)
         {
             LogHelper.Error("出现异常", ex: ex);
         }
     })
     {
         IsBackground = true
     }.Start();
     return(Ok());
 }
 public void CallYoutubeStopLive(YoutubeLiveCallbackBody body)
 {
     if (LiveList.ContainsKey(body.VtuberName))
     {
         LiveList.Remove(body.VtuberName);
         InsertEventLog($"[{DateTimeExtensions.TimestampToDateTime(body.ActualStartTime).AddHours(8):yyyy-MM-dd HH:mm:ss}] Vtuber [{body.VtuberName}] 停止直播");
         VtuberStopYoutubeLiveEvent?.Invoke(body);
     }
 }
 public async Task CallYoutubeStopLiveAsync(VtuberEntity vtuber, YoutubeVideo live)
 {
     using (var client = HttpClientExtensions.CreateClient())
     {
         var body = new YoutubeLiveCallbackBody()
         {
             VtuberName         = vtuber.OriginalName,
             LiveTitle          = live.Title,
             LiveLink           = live.VideoLink,
             ScheduledStartTime = live.LiveDetails.ScheduledStartTime.ToTimestamp(),
             ActualStartTime    = live.LiveDetails.ActualStartTime?.ToTimestamp() ?? DateTime.Now.ToTimestamp(),
             ViewersCount       = 0,
             Sign = Sign
         };
         await client.PostJsonAsync(Url + "youtube/live/stop", body);
     }
 }
 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);
     }
 }