Example #1
0
        public static void CheckVideosDifferentRadius(string smallRadius, string bigRadius)
        {
            string searchTerm = CommandLine.RequestUserInput <string>("Search term: ");

            YouTubeAPIv3         api    = new YouTubeAPIv3();
            RadiusComparerResult result = api.CheckVideosDifferentRadius(smallRadius, bigRadius, searchTerm);

            CommandLine.WriteLine("\n");
            CommandLine.WriteLine(String.Format("Total Small Radius: {0}", result.SmallRadiusItems.Count));
            CommandLine.WriteLine(String.Format("Total Big Radius: {0}", result.BigRadiusItems.Count));
            CommandLine.WriteLine(String.Format("Total Elements in Small Radius that aren't in Big Radius: {0}", result.DifferentItems.Count));

            CommandLine.PressAnyKeyToExit();
        }
Example #2
0
        /// <summary>
        /// Checks result differences between two radius.
        /// </summary>
        /// <param name="smallRadius"></param>
        /// <param name="bigRadius"></param>
        /// <returns></returns>
        public RadiusComparerResult CheckVideosDifferentRadius(string smallRadius, string bigRadius, string searchTerm)
        {
            var youtube = new YouTubeService(new BaseClientService.Initializer());

            // Performs search operation for the small radius
            SearchResource.ListRequest listRequest = youtube.Search.List("snippet");
            listRequest.Fields     = "nextPageToken, prevPageToken, pageInfo, items(id,snippet(title, description, publishedAt, thumbnails, channelId, channelTitle))";
            listRequest.Key        = API_KEY;
            listRequest.Type       = ResourceTypes.Video;
            listRequest.MaxResults = MAX_RESULTS_PER_PAGE;
            if (!string.IsNullOrEmpty(LOCATION))
            {
                listRequest.Location = LOCATION;
            }
            if (!string.IsNullOrEmpty(LOCATION_RADIUS))
            {
                listRequest.LocationRadius = smallRadius;
            }
            listRequest.Q     = searchTerm;
            listRequest.Order = SearchResource.ListRequest.OrderEnum.Date;
            // Splits the query in days
            DateTime dateFrom = DateTime.ParseExact(PUBLISHED_FROM_DATE, "yyyy/MM/dd", CultureInfo.InvariantCulture);

            listRequest.PublishedAfter  = dateFrom;
            listRequest.PublishedBefore = dateFrom.AddMinutes(SMALL_RADIUS_FREQ_MINUTES);

            SearchListResponse  searchResponse     = null;
            List <SearchResult> resultsSmallRadius = new List <SearchResult>();
            string nextPageToken = string.Empty;

            while (listRequest.PublishedAfter < DateTime.Today)
            {
                searchResponse = listRequest.Execute();
                resultsSmallRadius.AddRange(searchResponse.Items);
                nextPageToken = searchResponse.NextPageToken;

                while (searchResponse.Items.Count == MAX_RESULTS_PER_PAGE && !string.IsNullOrEmpty(nextPageToken))
                {
                    listRequest.PageToken = nextPageToken;
                    searchResponse        = listRequest.Execute();
                    resultsSmallRadius.AddRange(searchResponse.Items);
                    nextPageToken = searchResponse.NextPageToken;
                }

                listRequest.PublishedAfter  = listRequest.PublishedAfter.Value.AddMinutes(SMALL_RADIUS_FREQ_MINUTES);
                listRequest.PublishedBefore = listRequest.PublishedBefore.Value.AddMinutes(SMALL_RADIUS_FREQ_MINUTES);
                listRequest.PageToken       = null;
            }

            // Performs search operation for the big radius
            listRequest.LocationRadius = bigRadius;
            listRequest.Order          = SearchResource.ListRequest.OrderEnum.Date;
            // Splits the query in days
            listRequest.PublishedAfter  = dateFrom;
            listRequest.PublishedBefore = dateFrom.AddMinutes(BIG_RADIUS_FREQ_MINUTES);

            List <SearchResult> resultsBigRadius = new List <SearchResult>();

            while (listRequest.PublishedAfter < DateTime.Today)
            {
                searchResponse = listRequest.Execute();
                resultsBigRadius.AddRange(searchResponse.Items);
                nextPageToken = searchResponse.NextPageToken;

                int totalItems = searchResponse.Items.Count;
                while (searchResponse.Items.Count == MAX_RESULTS_PER_PAGE && !string.IsNullOrEmpty(nextPageToken))
                {
                    listRequest.PageToken = nextPageToken;
                    searchResponse        = listRequest.Execute();
                    resultsBigRadius.AddRange(searchResponse.Items);
                    nextPageToken = searchResponse.NextPageToken;
                    totalItems   += searchResponse.Items.Count;
                }

                listRequest.PublishedAfter  = listRequest.PublishedAfter.Value.AddMinutes(BIG_RADIUS_FREQ_MINUTES);
                listRequest.PublishedBefore = listRequest.PublishedBefore.Value.AddMinutes(BIG_RADIUS_FREQ_MINUTES);
                listRequest.PageToken       = null;
            }

            // Checks for items that are in the small radius and aren't in big radius
            RadiusComparerResult result = new RadiusComparerResult();

            result.SmallRadiusItems = resultsSmallRadius;
            result.BigRadiusItems   = resultsBigRadius;

            return(result);
        }