private static EpisodeSyncDetails ReconSyncTraktEpisode(AnimeSeries ser, AnimeEpisode ep, TraktSummaryContainer traktSummary, List<JMMUser> traktUsers, List<TraktV2ShowCollectedResult> collected, List<TraktV2ShowWatchedResult> watched, bool sendNow) { try { // get the Trakt Show ID for this episode string traktShowID = string.Empty; int season = -1; int epNumber = -1; GetTraktEpisodeIdV2(ep, ref traktShowID, ref season, ref epNumber); if (string.IsNullOrEmpty(traktShowID) || season < 0 || epNumber < 0) return null; // get the current collected records for this series on Trakt TraktV2CollectedEpisode epTraktCol = null; TraktV2ShowCollectedResult col = collected.FirstOrDefault(x => x.show.ids.slug == traktShowID); if (col != null) { TraktV2CollectedSeason sea = col.seasons.FirstOrDefault(x => x.number == season); if (sea != null) { epTraktCol = sea.episodes.FirstOrDefault(x => x.number == epNumber); } } bool onlineCollection = epTraktCol != null; // get the current watched records for this series on Trakt TraktV2WatchedEpisode epTraktWatched = null; TraktV2ShowWatchedResult wtc = watched.FirstOrDefault(x => x.show.ids.slug == traktShowID); if (wtc != null) { TraktV2WatchedSeason sea = wtc.seasons.FirstOrDefault(x => x.number == season); if (sea != null) { epTraktWatched = sea.episodes.FirstOrDefault(x => x.number == epNumber); } } bool onlineWatched = epTraktWatched != null; bool localCollection = false; bool localWatched = false; if (ep.GetVideoLocals().Count > 0) { // let's check if this episode has a user record against it // if it does, it means a user has watched it localCollection = true; AnimeEpisode_User userRecord = null; foreach (JMMUser juser in traktUsers) { userRecord = ep.GetUserRecord(juser.JMMUserID); if (userRecord != null) break; } if (userRecord != null) localWatched = true; } string msg1 = string.Format("Sync Check Status: AniDB: {0} - {1} - {2} - Collection: {3} - Watched: {4}", ser.AniDB_ID, ep.EpisodeTypeEnum, ep.AniDB_EpisodeID, localCollection, localWatched); string msg2 = string.Format("Sync Check Status: Trakt: {0} - S:{1} - EP:{2} - Collection: {3} - Watched: {4}", traktShowID, season, epNumber, onlineCollection, onlineWatched); logger.Trace(msg1); logger.Trace(msg2); // sync the collection status if (localCollection) { // is in the local collection, but not Trakt, so let's ADD it if (!onlineCollection) { string msg = string.Format("SYNC LOCAL: Adding to Trakt Collection: Slug: {0} - S:{1} - EP:{2}", traktShowID, season, epNumber); logger.Trace(msg); DateTime epDate = GetEpisodeDateForSync(ep, TraktSyncType.CollectionAdd); if (sendNow) SyncEpisodeToTrakt(TraktSyncType.CollectionAdd, traktShowID, season, epNumber, epDate, false); else return new EpisodeSyncDetails(TraktSyncType.CollectionAdd, traktShowID, season, epNumber, epDate); } } else { // is in the trakt collection, but not local, so let's REMOVE it if (onlineCollection) { string msg = string.Format("SYNC LOCAL: Removing from Trakt Collection: Slug: {0} - S:{1} - EP:{2}", traktShowID, season, epNumber); logger.Trace(msg); DateTime epDate = GetEpisodeDateForSync(ep, TraktSyncType.CollectionRemove); if (sendNow) SyncEpisodeToTrakt(TraktSyncType.CollectionRemove, traktShowID, season, epNumber, epDate, false); else return new EpisodeSyncDetails(TraktSyncType.CollectionRemove, traktShowID, season, epNumber, epDate); } } // sync the watched status if (localWatched) { // is watched locally, but not Trakt, so let's ADD it if (!onlineWatched) { string msg = string.Format("SYNC LOCAL: Adding to Trakt History: Slug: {0} - S:{1} - EP:{2}", traktShowID, season, epNumber); logger.Trace(msg); DateTime epDate = GetEpisodeDateForSync(ep, TraktSyncType.HistoryAdd); if (sendNow) SyncEpisodeToTrakt(TraktSyncType.HistoryAdd, traktShowID, season, epNumber, epDate, false); else return new EpisodeSyncDetails(TraktSyncType.HistoryAdd, traktShowID, season, epNumber, epDate); } } else { // is watched on trakt, but not locally, so let's REMOVE it if (onlineWatched) { string msg = string.Format("SYNC LOCAL: Removing from Trakt History: Slug: {0} - S:{1} - EP:{2}", traktShowID, season, epNumber); logger.Trace(msg); DateTime epDate = GetEpisodeDateForSync(ep, TraktSyncType.HistoryRemove); if (sendNow) SyncEpisodeToTrakt(TraktSyncType.HistoryRemove, traktShowID, season, epNumber, epDate, false); else return new EpisodeSyncDetails(TraktSyncType.HistoryRemove, traktShowID, season, epNumber, epDate); } } return null; } catch (Exception ex) { logger.ErrorException("Error in TraktTVHelper.SyncTraktEpisode: " + ex.ToString(), ex); return null; } }
private static DateTime GetEpisodeDateForSync(AnimeEpisode ep, TraktSyncType syncType) { DateTime epDate = DateTime.Now; if (syncType == TraktSyncType.CollectionAdd || syncType == TraktSyncType.CollectionRemove) { epDate = DateTime.Now; // not relevant for a remove if (syncType == TraktSyncType.CollectionAdd) { // get the the first file that was added to this episode DateTime? thisDate = null; foreach (VideoLocal vid in ep.GetVideoLocals()) { if (!thisDate.HasValue) thisDate = vid.DateTimeCreated; if (vid.DateTimeCreated < thisDate) thisDate = vid.DateTimeCreated; } if (thisDate.HasValue) epDate = thisDate.Value; } } else { epDate = DateTime.Now; // not relevant for a remove if (syncType == TraktSyncType.HistoryAdd) { // get the latest user record and find the latest date this episode was watched DateTime? thisDate = null; JMMUserRepository repUsers = new JMMUserRepository(); List<JMMUser> traktUsers = repUsers.GetTraktUsers(); if (traktUsers.Count > 0) { AnimeEpisode_User userRecord = null; foreach (JMMUser juser in traktUsers) { userRecord = ep.GetUserRecord(juser.JMMUserID); if (userRecord != null) { if (!thisDate.HasValue && userRecord.WatchedDate.HasValue) thisDate = userRecord.WatchedDate; if (userRecord.WatchedDate.HasValue && thisDate.HasValue && userRecord.WatchedDate > thisDate) thisDate = userRecord.WatchedDate; } } if (thisDate.HasValue) epDate = thisDate.Value; } } } return epDate; }