private async Task HandleProgress(TraktShow traktShow, ShowSql bddShow) { // Remove Season blacklist traktShow.Seasons.RemoveAll(s => bddShow.Seasons.Any(b => b.SeasonNumber == s.Season && b.Blacklisted)); if (traktShow.Seasons.Any()) { TraktResponse <TraktNet.Objects.Get.Shows.ITraktShowCollectionProgress> collectionProgress = await Client.Shows.GetShowCollectionProgressAsync(traktShow.Id.ToString(), false, false, false).ConfigureAwait(false); TraktResponse <TraktNet.Objects.Get.Shows.ITraktShowCollectionProgress> collectionProgressRes = collectionProgress; foreach (ITraktSeasonCollectionProgress season in collectionProgressRes.Value.Seasons) { TraktSeason misSeason = traktShow.Seasons.SingleOrDefault(e => e.Season == season.Number); if (misSeason == null) { misSeason = new TraktSeason { Season = season.Number.Value }; traktShow.Seasons.Add(misSeason); } foreach (ITraktEpisodeCollectionProgress episode in season.Episodes) { // Already existing in missing if (misSeason.MissingEpisodes.All(m => m.Episode != episode.Number)) { misSeason.MissingEpisodes.Add(new TraktEpisode { Episode = episode.Number.Value, Collected = episode.Completed.HasValue && episode.Completed.Value }); } } } foreach (TraktSeason showSeason in traktShow.Seasons) { SeasonSql season = GetSeason(bddShow, showSeason.Season); // BlackList Ended series if complete if ((traktShow.Status == TraktShowStatus.Ended || traktShow.Status == TraktShowStatus.Canceled) && !showSeason.MissingEpisodes.Any()) { season.Blacklisted = true; } if (!season.Blacklisted) { // Save missing episode foreach (TraktEpisode missingEpisode in showSeason.MissingEpisodes) { EpisodeSql ep = GetEpisode(season, missingEpisode.Episode); ep.Status = missingEpisode.Collected || missingEpisode.Watched ? EpisodeStatusSql.Collected : EpisodeStatusSql.Missing; } } // Remove watched from missing showSeason.MissingEpisodes.RemoveAll(m => m.Watched); // Remove collected from missing showSeason.MissingEpisodes.RemoveAll(m => m.Collected); } traktShow.Seasons.RemoveAll(s => !s.MissingEpisodes.Any()); } }
public bool RefreshMissingEpisodes(IDatabase database) { SetupClient(database); RefreshHiddenItem(database); // Set all missing to unknown database.ClearMissingEpisodes(); Task <TraktListResponse <ITraktCollectionShow> > collected = Client.Users.GetCollectionShowsAsync("me", new TraktExtendedInfo { Full = true }); Task <TraktListResponse <ITraktWatchedShow> > watched = Client.Users.GetWatchedShowsAsync("me", new TraktExtendedInfo { Full = true }); List <TraktShow> shows = new List <TraktShow>(); watched.Wait(); TraktListResponse <ITraktWatchedShow> watchedRes = watched.Result; foreach (ITraktWatchedShow traktWatchedShow in watchedRes) { int watchedEpisodes = traktWatchedShow.WatchedSeasons.Where(s => s.Number != 0).Sum(season => season.Episodes.Count()); TraktShow show = new TraktShow { Id = traktWatchedShow.Ids.Trakt, Year = traktWatchedShow.Year, SerieName = traktWatchedShow.Title, Watched = watchedEpisodes >= traktWatchedShow.AiredEpisodes, Status = traktWatchedShow.Status, Imdb = traktWatchedShow.Ids.Imdb, Tmdb = traktWatchedShow.Ids.Tmdb, }; foreach (ITraktWatchedShowSeason season in traktWatchedShow.WatchedSeasons) { TraktSeason traktSeason = new TraktSeason { Season = season.Number.Value }; foreach (ITraktWatchedShowEpisode episode in season.Episodes) { traktSeason.MissingEpisodes.Add(new TraktEpisode { Episode = episode.Number.Value, Watched = true }); } show.Seasons.Add(traktSeason); } shows.Add(show); } collected.Wait(); TraktListResponse <ITraktCollectionShow> collectedRes = collected.Result; foreach (ITraktCollectionShow traktCollectionShow in collectedRes) { TraktShow show = shows.SingleOrDefault(s => s.Id == traktCollectionShow.Ids.Trakt); if (show == null) { show = new TraktShow { Id = traktCollectionShow.Ids.Trakt, Year = traktCollectionShow.Year, SerieName = traktCollectionShow.Title, Status = traktCollectionShow.Status, Imdb = traktCollectionShow.Ids.Imdb, Tmdb = traktCollectionShow.Ids.Tmdb, }; shows.Add(show); } foreach (ITraktCollectionShowSeason season in traktCollectionShow.CollectionSeasons) { TraktSeason misSeason = show.Seasons.SingleOrDefault(e => e.Season == season.Number); if (misSeason == null) { misSeason = new TraktSeason { Season = season.Number.Value }; show.Seasons.Add(misSeason); } foreach (ITraktCollectionShowEpisode episode in season.Episodes) { TraktEpisode misEpisode = misSeason.MissingEpisodes.SingleOrDefault(e => e.Episode == episode.Number); if (misEpisode != null) { misEpisode.Collected = true; } else { misSeason.MissingEpisodes.Add(new TraktEpisode { Episode = episode.Number.Value, Collected = true }); } } } } shows.RemoveAll(s => s.Watched); // PrepareDB List <ShowSql> bddShows = database.GetShows(); List <ShowSql> updateShows = new List <ShowSql>(); // Remove Show blacklist shows.RemoveAll(s => bddShows.Any(b => b.Id == s.Id && b.Blacklisted)); List <Task> tasks = new List <Task>(); foreach (TraktShow traktShow in shows) { ShowSql localShow = GetShow(database, traktShow.Id); localShow.Update(traktShow); updateShows.Add(localShow); tasks.Add(HandleProgress(traktShow, localShow)); } Task.WaitAll(tasks.ToArray()); database.AddOrUpdateShows(updateShows); database.ClearUnknownEpisodes(); shows.RemoveAll(s => !s.Seasons.Any()); return(true); }