public void Assert_GetTrendingVideo_max_result()
        {
            string expected      = "https://www.googleapis.com/youtube/v3/videos?regionCode=regionCode&key=apiKey&maxResults=50&part=snippet,statistics&chart=mostPopular";
            string trendingVideo = YoutubeApiStrings.GetTrendingVideo("regionCode", "apiKey", maxResults: 500);

            Assert.AreEqual(expected, trendingVideo);
        }
        public void Assert_GetTrendingVideo_page_token_is_not_null()
        {
            string expected      = "https://www.googleapis.com/youtube/v3/videos?regionCode=regionCode&key=apiKey&maxResults=2&pageToken=token&part=snippet,statistics&chart=mostPopular";
            string trendingVideo = YoutubeApiStrings.GetTrendingVideo("regionCode", "apiKey", 2, "token");

            Assert.AreEqual(expected, trendingVideo);
        }
        public async Task <ActionResult> FetchYoutubeVideosAsync()
        {
            if (dateTimeRateLimited == DateTime.Now.Date)
            {
                return(BadRequest("rate-limited"));
            }

            int totalVideosAdded = 0;

            foreach (string country in Constants.TRENDING_COUNTRY_CODES)
            {
                // Finds and adds all the trending videos
                List <YoutubeVideo> trendingVideos = new List <YoutubeVideo>();
                string nextPageToken = null;
                do
                {
                    string          url      = YoutubeApiStrings.GetTrendingVideo(country, Constants.YOUTUBE_API_KEY, pageToken: nextPageToken);
                    YoutubeResponse response = await youtubeApi.GetRequest(url);

                    if (response.StatusCode != HttpStatusCode.OK)
                    {
                        dateTimeRateLimited = DateTime.Now.Date;
                        YoutubeError error = parser.ParseYoutubeError(response.Content);
                        return(BadRequest(error));
                    }
                    nextPageToken = parser.ParseTokenAndVideoCount(response.Content, out int videoCount);
                    trendingVideos.AddRange(parser.ParseYoutubeVideosFromTrending(response.Content, videoCount));
                } while (nextPageToken != null);

                await AddYoutubeVideosDatabaseAsync(trendingVideos);
                await SaveChangesDbAsync();

                totalVideosAdded += trendingVideos.Count;

                // Find and adds all videos related to the trending videos
                List <YoutubeVideo> relatedVideos = new List <YoutubeVideo>();
                foreach (YoutubeVideo video in trendingVideos)
                {
                    string          url      = YoutubeApiStrings.GetVideoRelatedToVideoId(video.VideoId, Constants.YOUTUBE_API_KEY, maxResults: 50);
                    YoutubeResponse response = await youtubeApi.GetRequest(url);

                    if (response.StatusCode != HttpStatusCode.OK)
                    {
                        dateTimeRateLimited = DateTime.Now.Date;
                        YoutubeError error = parser.ParseYoutubeError(response.Content);
                        return(BadRequest(error));
                    }
                    relatedVideos.AddRange(parser.ParseYoutubeVideosFromSearch(response.Content, 50));
                }

                await AddYoutubeVideosDatabaseAsync(relatedVideos);
                await SaveChangesDbAsync();

                totalVideosAdded += relatedVideos.Count;
            }

            return(Ok(totalVideosAdded));
        }
        public async Task <ActionResult> GetFillerVideos()
        {
            if (dateTimeRateLimited == DateTime.Now.Date)
            {
                return(BadRequest("rate-limited"));
            }

            int totalVideosAdded = 0;

            // Fetch all videos by search, while not rate limited
            while (dateTimeRateLimited != DateTime.Now.Date)
            {
                string          url      = YoutubeApiStrings.GetVideoBySearch(RandomString(3), Constants.YOUTUBE_API_KEY, maxResults: 50);
                YoutubeResponse response = await youtubeApi.GetRequest(url);

                if (response.StatusCode != HttpStatusCode.OK)
                {
                    dateTimeRateLimited = DateTime.Now.Date;
                    // TODO: Fetch and log this YoutubeError message
                    YoutubeError error = parser.ParseYoutubeError(response.Content);
                    return(BadRequest(error));
                }
                parser.ParseTokenAndVideoCount(response.Content, out int videoCount);
                List <YoutubeVideo> videos = parser.ParseYoutubeVideosFromSearch(response.Content, videoCount);

                for (int i = 0; i < videos.Count; i++)
                {
                    string urlStats  = YoutubeApiStrings.GetVideoStatistics(videos[i].VideoId, Constants.YOUTUBE_API_KEY);
                    long   viewCount = await GetYoutubeVideoViewCountFromApiAsync(urlStats);

                    if (viewCount == -1)
                    {
                        break;
                    }
                    videos[i].ViewCount = viewCount;
                }
                List <YoutubeVideo> validVideos = videos.Where(yt => yt.ViewCount != -1).ToList();

                totalVideosAdded += validVideos.Count;
                await AddYoutubeVideosDatabaseAsync(validVideos);
                await SaveChangesDbAsync();
            }
            return(Ok(totalVideosAdded));
        }
        public void Assert_GetVideoRelatedToVideoId_with_overloads_is_correct()
        {
            string expected = $"https://www.googleapis.com/youtube/v3/search?0={videoId}&key={youtubeApiKey}&maxResults={maxResults}&order={order}&part=snippet&type=video";

            Assert.AreEqual(expected, YoutubeApiStrings.GetVideoRelatedToVideoId(videoId, youtubeApiKey, maxResults, order));
        }
        public void Assert_GetVideoRelatedToVideoId_is_correct()
        {
            string expected = $"https://www.googleapis.com/youtube/v3/search?0={videoId}&key={youtubeApiKey}&maxResults=1&order=relevance&part=snippet&type=video";

            Assert.AreEqual(expected, YoutubeApiStrings.GetVideoRelatedToVideoId(videoId, youtubeApiKey));
        }
        public void Assert_GetVideoBySearch_with_overloads_is_correct()
        {
            string expected = $"https://www.googleapis.com/youtube/v3/search?q={search}&key={youtubeApiKey}&maxResults={maxResults}&order={order}&part=snippet&type=video&relevanceLanguage=en";

            Assert.AreEqual(expected, YoutubeApiStrings.GetVideoBySearch(search, youtubeApiKey, maxResults, order));
        }
        public void Assert_GetVideoStatistics_is_correct()
        {
            string expected = $"https://www.googleapis.com/youtube/v3/videos?&id={videoId}&key={youtubeApiKey}&part=statistics";

            Assert.AreEqual(expected, YoutubeApiStrings.GetVideoStatistics(videoId, youtubeApiKey));
        }