public static IEnumerable <ChangesListItem> GetChangesMovies(this TMDbClient client, CancellationToken cts, UpdateTimeTracker latestUpdateTime) { //We need to ask for updates in blocks of 7 days //We'll keep asking until we get to a date within 7 days of today //(up to a maximum of 52 - if you are this far behind then you may need multiple refreshes) List <ChangesListItem> updatesResponses = new List <ChangesListItem>(); bool moreUpdates = true; int numberOfCallsMade = 0; for (DateTime time = latestUpdateTime.LastSuccessfulServerUpdateDateTime(); time <= DateTime.Now; time = time.AddDays(14) ) { int maxPage = 1; for (int currentPage = 0; currentPage <= maxPage; currentPage++) { if (cts.IsCancellationRequested) { throw new CancelledException(); } SearchContainer <ChangesListItem>?response = client.GetMoviesChangesAsync(page: currentPage, startDate: time, cancellationToken: cts).Result; numberOfCallsMade++; maxPage = response.TotalPages; updatesResponses.AddRange(response.Results); if (numberOfCallsMade > MAX_NUMBER_OF_CALLS) { throw new TooManyCallsException(); } } } latestUpdateTime.RegisterServerUpdate(DateTime.Now.ToUnixTime()); return(updatesResponses); }
public bool GetUpdates(bool showErrorMsgBox, CancellationToken cts, IEnumerable <SeriesSpecifier> ss) { Say("Validating TMDB cache"); MarkPlaceHoldersDirty(ss); try { Say($"Updates list from TMDB since {latestMovieUpdateTime.LastSuccessfulServerUpdateDateTime()}"); long updateFromEpochTime = latestMovieUpdateTime.LastSuccessfulServerUpdateTimecode(); if (updateFromEpochTime == 0) { MarkAllDirty(); latestMovieUpdateTime.RegisterServerUpdate(DateTime.Now.ToUnixTime()); return(true); } List <int> updates = Client.GetChangesMovies(cts, latestMovieUpdateTime).Select(item => item.Id).Distinct().ToList(); Say($"Processing {updates.Count} updates from TMDB. From between {latestMovieUpdateTime.LastSuccessfulServerUpdateDateTime()} and {latestMovieUpdateTime.ProposedServerUpdateDateTime()}"); foreach (int id in updates) { if (!cts.IsCancellationRequested) { if (HasMovie(id)) { CachedMovieInfo?x = GetMovie(id); if (!(x is null)) { LOGGER.Info( $"Identified that show with TMDB Id {id} {x.Name} should be updated."); x.Dirty = true; } } } else { return(true); } } lock (MOVIE_LOCK) { Say($"Identified {Movies.Values.Count(info => info.Dirty && !info.IsSearchResultOnly)} TMDB Movies need updating"); LOGGER.Info(Movies.Values.Where(info => info.Dirty && !info.IsSearchResultOnly).Select(info => info.Name).ToCsv); } return(true); } catch (SourceConnectivityException conex) { LastErrorMessage = conex.Message; return(false); } catch (SourceConsistencyException sce) { LOGGER.Error(sce.Message); LastErrorMessage = sce.Message; return(false); } finally { SayNothing(); } }