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 void Assert_ParseYoutubeError_is_correct() { string messageExpected = "The request cannot be completed because you have exceeded your \u003ca href=\"/youtube/v3/getting-started#quota\"\u003equota\u003c/a\u003e."; string jsonData = GetTextFromFile("QuotaReached.json"); YoutubeError youtubeError = parser.ParseYoutubeError(jsonData); Assert.AreEqual(403, youtubeError.StatusCode); Assert.AreEqual("youtube.quota", youtubeError.Domain); Assert.AreEqual("quotaExceeded", youtubeError.Reason); Assert.AreEqual(messageExpected, youtubeError.Message); }
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)); }