Пример #1
0
        /// <summary>
        /// Gets the messages for a live chat.
        /// </summary>
        /// <param name="broadcast">The broadcast of the live chat</param>
        /// <param name="nextResultsToken">The token for querying the next set of results from a previous query</param>
        /// <param name="maxResults">The maximum results to return</param>
        /// <returns>The list of chat messages</returns>
        public async Task <LiveChatMessagesResultModel> GetMessages(LiveBroadcast broadcast, string nextResultsToken = null, int maxResults = 0)
        {
            Validator.ValidateVariable(broadcast, "broadcast");
            return(await this.YouTubeServiceWrapper(async() =>
            {
                LiveChatMessagesResource.ListRequest request = this.connection.GoogleYouTubeService.LiveChatMessages.List(broadcast.Snippet.LiveChatId, "id,snippet,authorDetails");

                if (maxResults > 0)
                {
                    request.MaxResults = maxResults;
                }

                if (!string.IsNullOrEmpty(nextResultsToken))
                {
                    request.PageToken = nextResultsToken;
                }

                LogRequest(request);

                LiveChatMessageListResponse response = await request.ExecuteAsync();
                LogResponse(request, response);

                return new LiveChatMessagesResultModel(response);
            }));
        }
Пример #2
0
        /// <summary>
        /// Youtubeのチャット監視スレッドメイン処理
        /// </summary>
        /// <param name="videoId"></param>
        private void CommentMonitorTask(string videoId)
        {
            try
            {
                _logger.Info($"Youtubes監視スレッド実行開始 API={YouTubeAPIKey}, Video={videoId}");

                using (YouTubeService Youtube = new YouTubeService(new BaseClientService.Initializer()
                {
                    ApiKey = YouTubeAPIKey
                }))
                {
                    string liveChatId = GetliveChatID(videoId, Youtube);

                    _logger.Info($"YouTubeチャットID成功 ChatId={liveChatId}");
                    string nextPageToken = null;

                    LiveChatMessagesResource.ListRequest liveChatRequest = Youtube.LiveChatMessages.List(liveChatId, "snippet,authorDetails");

                    bool fastflg = false;

                    while (true)
                    {
                        liveChatRequest.PageToken = nextPageToken;

                        var task = liveChatRequest.ExecuteAsync();
                        task.Wait(_TokenSource.Token);
                        var liveChatResponse = task.Result;

                        // 途中起動を考慮して初回の読み込みコメントは無視
                        if (fastflg == true)
                        {
                            foreach (var liveChat in liveChatResponse.Items)
                            {
                                _logger.Info($"コメント:{liveChat.Snippet.DisplayMessage},{liveChat.AuthorDetails.DisplayName}");
                                // コメントを出力
                                PushText(liveChat.Snippet.DisplayMessage);
                            }
                        }
                        else
                        {
                            fastflg = true;
                        }

                        System.Threading.Thread.Sleep((int)liveChatResponse.PollingIntervalMillis);
                        nextPageToken = liveChatResponse.NextPageToken;
                    }
                }
            }catch (OperationCanceledException ex)
            {
                _logger.Info($"Youtubes監視スレッドで終了要求検知");
            }
            catch (Exception ex)
            {
                _logger.Error($"Youtubes監視スレッドで例外 {ex.Message}");
            }
        }