public static IEnumerable<Episode> GetUnWatchedEpisodesFromTrakt()
{
if (UnWatchedEpisodes != null)
return UnWatchedEpisodes;
TraktLogger.Info("Getting current user unwatched episodes from trakt.tv");
// trakt.tv does not provide an unwatched API
// There are plans after initial launch of v2 to have a re-watch API.
// First we need to get the previously cached watched episodes
var previouslyWatched = WatchedEpisodes;
if (previouslyWatched == null)
return new List<Episode>();
// now get the latest watched
var currentWatched = GetWatchedEpisodesFromTrakt();
if (currentWatched == null)
return null;
TraktLogger.Debug("Comparing previous watched episodes against current watched episodes such that unwatched can be determined");
// anything not in the currentwatched that is previously watched
// must be unwatched now.
// Note: we can add to internal cache from external events, so we can't always rely on trakt id for comparisons
var dictCurrWatched = currentWatched.ToLookup(cwe => cwe.ShowTvdbId + "_" + cwe.Season + "_" + cwe.Number);
var unwatchedEpisodes = from pwe in previouslyWatched
where !dictCurrWatched[pwe.ShowTvdbId + "_" + pwe.Season + "_" + pwe.Number].Any()
select new Episode
{
ShowId = pwe.ShowId,
ShowTvdbId = pwe.ShowTvdbId,
ShowImdbId = pwe.ShowImdbId,
ShowTitle = pwe.ShowTitle,
ShowYear = pwe.ShowYear,
Season = pwe.Season,
Number = pwe.Number
};
UnWatchedEpisodes = unwatchedEpisodes ?? new List<Episode>();
return UnWatchedEpisodes;
}