/// <summary> /// Returns a list of shows for watched history sync as show objects with season / episode hierarchy /// </summary> private TraktSyncShowsWatchedEx GetWatchedShowsForSyncEx(List<DBEpisode> localWatchedEpisodes, List<TraktCache.EpisodeWatched> traktEpisodesWatched) { TraktLogger.Info("Finding local episodes to add to trakt.tv watched history"); // prepare new sync object var syncWatchedEpisodes = new TraktSyncShowsWatchedEx(); syncWatchedEpisodes.Shows = new List<TraktSyncShowWatchedEx>(); // filter out any invalid episodes by user var episodes = localWatchedEpisodes.Where(lwe => lwe[DBOnlineEpisode.cEpisodeIndex] != "" && lwe[DBOnlineEpisode.cEpisodeIndex] != "0").ToList(); // create a unique key to lookup and search for faster var onlineEpisodes = traktEpisodesWatched.ToLookup(twe => CreateLookupKey(twe), twe => twe); foreach (var episode in episodes) { string tvdbKey = CreateLookupKey(episode); var traktEpisode = onlineEpisodes[tvdbKey].FirstOrDefault(); // check if not watched on trakt and add it to sync list if (traktEpisode == null) { // check if we already have the show added to our sync object var syncShow = syncWatchedEpisodes.Shows.FirstOrDefault(swe => swe.Ids != null && swe.Ids.Tvdb == episode[DBOnlineEpisode.cSeriesID]); if (syncShow == null) { // get show data from episode var show = Helper.getCorrespondingSeries(episode[DBOnlineEpisode.cSeriesID]); if (show == null) continue; // create new show syncShow = new TraktSyncShowWatchedEx { Ids = new TraktShowId { Tvdb = show[DBSeries.cID], Imdb = BasicHandler.GetProperImdbId(show[DBOnlineSeries.cIMDBID]) }, Title = show[DBOnlineSeries.cOriginalName], Year = show.Year.ToNullableInt32() }; // add a new season collection to show object syncShow.Seasons = new List<TraktSyncShowWatchedEx.Season>(); // add show to the collection syncWatchedEpisodes.Shows.Add(syncShow); } // check if season exists in show sync object var syncSeason = syncShow.Seasons.FirstOrDefault(ss => ss.Number == episode[DBOnlineEpisode.cSeasonIndex]); if (syncSeason == null) { // create new season syncSeason = new TraktSyncShowWatchedEx.Season { Number = episode[DBOnlineEpisode.cSeasonIndex] }; // add a new episode collection to season object syncSeason.Episodes = new List<TraktSyncShowWatchedEx.Season.Episode>(); // add season to the show syncShow.Seasons.Add(syncSeason); } // add episode to season syncSeason.Episodes.Add(new TraktSyncShowWatchedEx.Season.Episode { Number = episode[DBOnlineEpisode.cEpisodeIndex], WatchedAt = GetLastPlayedDate(episode) }); } } return syncWatchedEpisodes; }
private void MarkEpisodesAsWatched(DBSeries show, List<DBEpisode> episodes) { var syncThread = new Thread((o) => { // send show data as well in case tvdb ids are not available on trakt server // TraktSyncEpisodeRated object is good if we could trust trakt having the tvdb ids. // trakt is more likely to have a show tvdb id than a episode tvdb id var showEpisodes = new TraktSyncShowWatchedEx { Title = show[DBOnlineSeries.cOriginalName], Year = show.Year.ToNullableInt32(), Ids = new TraktShowId { Tvdb = show[DBSeries.cID], Imdb = BasicHandler.GetProperImdbId(show[DBOnlineSeries.cIMDBID]) } }; var seasons = new List<TraktSyncShowWatchedEx.Season>(); foreach (var episode in episodes) { if (seasons.Exists(s => s.Number == episode[DBOnlineEpisode.cSeasonIndex])) { // add the episode to the season collection seasons.First(s => s.Number == episode[DBOnlineEpisode.cSeasonIndex]) .Episodes.Add(new TraktSyncShowWatchedEx.Season.Episode { Number = episode[DBOnlineEpisode.cEpisodeIndex], WatchedAt = DateTime.UtcNow.ToISO8601() }); } else { // create season and add episode to it's episode collection seasons.Add(new TraktSyncShowWatchedEx.Season { Number = episode[DBOnlineEpisode.cSeasonIndex], Episodes = new List<TraktSyncShowWatchedEx.Season.Episode> { new TraktSyncShowWatchedEx.Season.Episode { Number = episode[DBOnlineEpisode.cEpisodeIndex], WatchedAt = DateTime.UtcNow.ToISO8601() } } }); } } showEpisodes.Seasons = seasons; var showSync = new TraktSyncShowsWatchedEx { Shows = new List<TraktSyncShowWatchedEx> { showEpisodes } }; var response = TraktAPI.TraktAPI.AddShowsToWatchedHistoryEx(showSync); TraktLogger.LogTraktResponse(response); if (response != null && response.NotFound != null && response.NotFound.Episodes.Count == 0) { // update local cache TraktCache.AddEpisodesToWatchHistory(showEpisodes); } }) { IsBackground = true, Name = "ToggleWatched" }; syncThread.Start(); }
internal static void AddEpisodesToWatchHistory(TraktSyncShowWatchedEx show) { var watchedEpisodes = (_WatchedEpisodes ?? new List<EpisodeWatched>()).ToList(); var episodesToAdd = new List<EpisodeWatched>(); foreach (var season in show.Seasons) { foreach (var episode in season.Episodes) { episodesToAdd.Add(new EpisodeWatched { Number = episode.Number, Season = season.Number, ShowId = show.Ids.Trakt, ShowImdbId = show.Ids.Imdb, ShowTvdbId = show.Ids.Tvdb, ShowTitle = show.Title, ShowYear = show.Year, Plays = 1, WatchedAt = episode.WatchedAt ?? DateTime.UtcNow.ToISO8601() }); } } watchedEpisodes.AddRange(episodesToAdd); _WatchedEpisodes = watchedEpisodes; }