Пример #1
0
        public static async Task <CommentThreadListResponse> ExecuteAllAsync(this CommentThreadsResource.ListRequest request, CancellationToken ct = default(CancellationToken))
        {
            request.MaxResults = request.MaxResults ?? 100;
            var response = await request.ExecuteAsync(ct);

            if (!response.Items.Any())
            {
                return(response);
            }
            var collection = response.Items.ToList();

            while (!ct.IsCancellationRequested)
            {
                if (string.IsNullOrWhiteSpace(response.NextPageToken))
                {
                    break;
                }
                request.PageToken = response.NextPageToken;
                response          = await request.ExecuteAsync(ct);

                if (response.Items.Any())
                {
                    collection.AddRange(response.Items);
                }
            }

            response.Items = collection;
            return(response);
        }
        internal async Task <IEnumerable <CommentThread> > GetCommentThreads(Channel channel = null, bool relatedTo = false, Video video = null, string id = null, int maxResults = 1)
        {
            return(await this.YouTubeServiceWrapper(async() =>
            {
                List <CommentThread> results = new List <CommentThread>();
                string pageToken = null;
                do
                {
                    CommentThreadsResource.ListRequest request = this.connection.GoogleYouTubeService.CommentThreads.List("snippet,replies");
                    if (channel != null)
                    {
                        if (relatedTo)
                        {
                            request.AllThreadsRelatedToChannelId = channel.Id;
                        }
                        else
                        {
                            request.ChannelId = channel.Id;
                        }
                    }
                    else if (video != null)
                    {
                        request.VideoId = video.Id;
                    }
                    else if (!string.IsNullOrEmpty(id))
                    {
                        request.Id = id;
                    }
                    request.MaxResults = Math.Min(maxResults, 50);
                    request.PageToken = pageToken;
                    LogRequest(request);

                    CommentThreadListResponse response = await request.ExecuteAsync();
                    LogResponse(request, response);
                    results.AddRange(response.Items);
                    maxResults -= response.Items.Count;
                    pageToken = response.NextPageToken;
                } while (maxResults > 0 && !string.IsNullOrEmpty(pageToken));
                return results;
            }));
        }
Пример #3
0
        public static async Task <dynamic> CommentThreads(AppSettings appSettings, JObject requestBody)
        {
            string  methodName = "CommentThreads";
            dynamic result     = new ExpandoObject();

            try
            {
                // https://developers.google.com/youtube/v3/docs/commentThreads/list
                YouTubeService youtubeService = new YouTubeService(new BaseClientService.Initializer()
                {
                    ApiKey = requestBody["apikey"]?.ToString()
                });
                CommentThreadsResource.ListRequest listRequest = youtubeService.CommentThreads.List("id,replies,snippet");
                if (requestBody["pagetoken"] != null)
                {
                    listRequest.PageToken = requestBody["pagetoken"].ToString();
                }
                switch (requestBody["type"]?.ToString())
                {
                case "channel":
                    listRequest.ChannelId = requestBody["id"].ToString();
                    break;

                case "video":
                    listRequest.VideoId = requestBody["id"].ToString();
                    break;

                default:
                    listRequest.AllThreadsRelatedToChannelId = requestBody["id"].ToString();
                    break;
                }
                listRequest.MaxResults = 100;
                if (requestBody["order"] != null)
                {
                    //Accepted values: time, relevance
                    listRequest.Order = Enum.Parse <CommentThreadsResource.ListRequest.OrderEnum>(requestBody["order"].ToString());
                }
                if (requestBody["q"] != null)
                {
                    listRequest.SearchTerms = requestBody["q"].ToString();
                }
                listRequest.TextFormat = Enum.Parse <CommentThreadsResource.ListRequest.TextFormatEnum>(requestBody["textFormat"]?.ToString() ?? "PlainText");
                CommentThreadListResponse listResponse = await listRequest.ExecuteAsync();

                Dictionary <string, object> response = new Dictionary <string, object>();
                response["totalresults"] = listResponse.PageInfo.TotalResults;
                response["pagetoken"]    = listResponse.NextPageToken;

                List <Dictionary <string, object> > items = new List <Dictionary <string, object> >();
                listResponse.Items.ToList().ForEach(i =>
                {
                    Dictionary <string, object> item = new Dictionary <string, object>();
                    item["commentid"]           = i.Id;
                    item["channelid"]           = i.Snippet.ChannelId;
                    item["videoid"]             = i.Snippet.VideoId;
                    item["likecount"]           = i.Snippet.TopLevelComment.Snippet.LikeCount;
                    item["viewerrating"]        = i.Snippet.TopLevelComment.Snippet.ViewerRating;
                    item["userchannelid"]       = i.Snippet.TopLevelComment.Snippet.AuthorChannelId.Value;
                    item["userchannelurl"]      = i.Snippet.TopLevelComment.Snippet.AuthorChannelUrl;
                    item["username"]            = i.Snippet.TopLevelComment.Snippet.AuthorDisplayName;
                    item["userprofileimageurl"] = i.Snippet.TopLevelComment.Snippet.AuthorProfileImageUrl;
                    item["text"]          = i.Snippet.TopLevelComment.Snippet.TextDisplay;
                    item["publisheddate"] = i.Snippet.TopLevelComment.Snippet.PublishedAt;
                    item["updateddate"]   = i.Snippet.TopLevelComment.Snippet.UpdatedAt;
                    if (i.Replies != null)
                    {
                        List <Dictionary <string, object> > replies = new List <Dictionary <string, object> >();
                        i.Replies.Comments.ToList().ForEach(c =>
                        {
                            Dictionary <string, object> reply = new Dictionary <string, object>();
                            reply["replyid"]             = c.Id;
                            reply["userchannelid"]       = c.Snippet.AuthorChannelId.Value;
                            reply["userchannelurl"]      = c.Snippet.AuthorChannelUrl;
                            reply["username"]            = c.Snippet.AuthorDisplayName;
                            reply["userprofileimageurl"] = c.Snippet.AuthorProfileImageUrl;
                            reply["text"]          = c.Snippet.TextDisplay;
                            reply["publisheddate"] = c.Snippet.PublishedAt;
                            reply["updateddate"]   = c.Snippet.UpdatedAt;
                            replies.Add(reply);
                        });
                        item["replies"] = replies;
                    }
                    items.Add(item);
                });
                response["items"] = items;

                return(response);
            }
            catch (Exception e)
            {
                Log.Write(appSettings, LogEnum.ERROR.ToString(), label, className, methodName, $"ERROR: {JsonConvert.SerializeObject(requestBody)}");
                Log.Write(appSettings, LogEnum.ERROR.ToString(), label, className, methodName, $"ERROR: {e.Source + Environment.NewLine + e.Message + Environment.NewLine + e.StackTrace}");
                throw e;
            }
        }