Пример #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);
        }
Пример #2
0
        private void GetComments_OnClick(object sender, RoutedEventArgs e)
        {
            var videoReq = youtubeService.Videos.List("statistics");

            videoReq.Id = VideoId.Text;
            var videoRes = videoReq.Execute();

            MessageBox.Show(String.Format("{0} comments", videoRes.Items[0].Statistics.CommentCount));

            var    list          = new List <Comment>();
            string nextPageToken = "";

            while (nextPageToken != null)
            {
                CommentThreadsResource.ListRequest req = youtubeService.CommentThreads.List("snippet");
                req.MaxResults = 100;
                req.Fields     =
                    "items(snippet(topLevelComment(snippet(textDisplay,authorDisplayName,authorProfileImageUrl)))),nextPageToken";
                req.VideoId    = VideoId.Text;
                req.TextFormat = CommentThreadsResource.ListRequest.TextFormatEnum.PlainText;
                req.PageToken  = nextPageToken;
                CommentThreadListResponse res = req.Execute();
                if (res.Items != null)
                {
                    foreach (CommentThread re in res.Items)
                    {
                        var data = new Comment
                        {
                            Content           = re.Snippet.TopLevelComment.Snippet.TextDisplay,
                            AuthorDisplayName = re.Snippet.TopLevelComment.Snippet.AuthorDisplayName,
                            AvatarUrl         = re.Snippet.TopLevelComment.Snippet.AuthorProfileImageUrl
                        };
                        list.Add(data);
                    }
                }
                nextPageToken = String.IsNullOrWhiteSpace(res.NextPageToken) ? null : res.NextPageToken;
            }
            MessageBox.Show("pobrane: " + list.Count.ToString());
            list = list.Where(a => a.Content.Contains("27")).ToList();
            Comments.ItemsSource = list;
            MessageBox.Show("z liczbą: " + list.Count.ToString());
            var t = list.GroupBy(a => a.Content);

            foreach (var k in t)
            {
                if (k.Count() > 1)
                {
                    //MessageBox.Show(String.Format("{0}: {1}", k.FirstOrDefault().AuthorDisplayName, k.Key));
                }
            }
        }
        public static CommentThreadListResponse GetCommentThreadListResponse(string videoId, int maxComments)
        {
            // Initialize the youtube service with the API key
            YouTubeService youtubeService = GetYoutubeService();

            // Construct the request
            CommentThreadsResource.ListRequest commentThreadsListRequest = youtubeService.CommentThreads.List("snippet");
            commentThreadsListRequest.MaxResults = maxComments;
            commentThreadsListRequest.TextFormat = CommentThreadsResource.ListRequest.TextFormatEnum.PlainText;
            commentThreadsListRequest.VideoId    = videoId;

            // Retrieve the response
            CommentThreadListResponse commentThreadsListResponse = commentThreadsListRequest.Execute();

            // Return the response
            return(commentThreadsListResponse);
        }
        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;
            }));
        }
Пример #5
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;
            }
        }