private static bool GetTraktCollectionInfo(ref List<TraktV2ShowCollectedResult> collected, ref List<TraktV2ShowWatchedResult> watched) { try { if (!ServerSettings.Trakt_IsEnabled || string.IsNullOrEmpty(ServerSettings.Trakt_AuthToken)) return false; // check that we have at least one user nominated for Trakt JMMUserRepository repUsers = new JMMUserRepository(); List<JMMUser> traktUsers = repUsers.GetTraktUsers(); if (traktUsers.Count == 0) return false; int traktCode = TraktStatusCodes.Success; // now get the full users collection from Trakt collected = TraktTVHelper.GetCollectedShows(ref traktCode); if (traktCode != TraktStatusCodes.Success) { logger.Error("Could not get users collection: {0}", traktCode); return false; } // now get all the shows / episodes the user has watched watched = TraktTVHelper.GetWatchedShows(ref traktCode); if (traktCode != TraktStatusCodes.Success) { logger.Error("Could not get users watched history: {0}", traktCode); return false; } return true; } catch (Exception ex) { logger.ErrorException("Error in TraktTVHelper.GetTraktCollectionInfo: " + ex.ToString(), ex); return false; } }
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; }
public static void SyncCollectionToTrakt() { try { if (!ServerSettings.Trakt_IsEnabled || string.IsNullOrEmpty(ServerSettings.Trakt_AuthToken)) return; // check that we have at least one user nominated for Trakt JMMUserRepository repUsers = new JMMUserRepository(); List<JMMUser> traktUsers = repUsers.GetTraktUsers(); if (traktUsers.Count == 0) return; AnimeSeriesRepository repSeries = new AnimeSeriesRepository(); List<AnimeSeries> allSeries = repSeries.GetAll(); // now get the full users collection from Trakt List<TraktV2ShowCollectedResult> collected = new List<TraktV2ShowCollectedResult>(); List<TraktV2ShowWatchedResult> watched = new List<TraktV2ShowWatchedResult>(); if (!GetTraktCollectionInfo(ref collected, ref watched)) return; TraktV2SyncCollectionEpisodesByNumber syncCollectionAdd = new TraktV2SyncCollectionEpisodesByNumber(); TraktV2SyncCollectionEpisodesByNumber syncCollectionRemove = new TraktV2SyncCollectionEpisodesByNumber(); TraktV2SyncWatchedEpisodesByNumber syncHistoryAdd = new TraktV2SyncWatchedEpisodesByNumber(); TraktV2SyncWatchedEpisodesByNumber syncHistoryRemove = new TraktV2SyncWatchedEpisodesByNumber(); #region Local Collection Sync /////////////////////////////////////////////////////////////////////////////////////// // First take a look at our local collection and update on Trakt /////////////////////////////////////////////////////////////////////////////////////// AniDB_AnimeRepository repAnime = new AniDB_AnimeRepository(); int counter = 0; foreach (AnimeSeries series in allSeries) { counter++; logger.Trace("Syncing check - local collection: {0} / {1} - {2}", counter, allSeries.Count, series.GetSeriesName()); AniDB_Anime anime = repAnime.GetByAnimeID(series.AniDB_ID); if (anime == null) continue; //if (anime.AnimeID != 3427) continue; TraktSummaryContainer traktSummary = new TraktSummaryContainer(); traktSummary.Populate(series.AniDB_ID); if (traktSummary.CrossRefTraktV2 == null || traktSummary.CrossRefTraktV2.Count == 0) continue; // get the current watched records for this series on Trakt foreach (AnimeEpisode ep in series.GetAnimeEpisodes()) { if (ep.EpisodeTypeEnum == enEpisodeType.Episode || ep.EpisodeTypeEnum == enEpisodeType.Special) { EpisodeSyncDetails epsync = ReconSyncTraktEpisode(series, ep, traktSummary, traktUsers, collected, watched, false); if (epsync != null) { switch (epsync.SyncType) { case TraktSyncType.CollectionAdd: syncCollectionAdd.AddEpisode(epsync.Slug, epsync.Season, epsync.EpNumber, epsync.EpDate); break; case TraktSyncType.CollectionRemove: syncCollectionRemove.AddEpisode(epsync.Slug, epsync.Season, epsync.EpNumber, epsync.EpDate); break; case TraktSyncType.HistoryAdd: syncHistoryAdd.AddEpisode(epsync.Slug, epsync.Season, epsync.EpNumber, epsync.EpDate); break; case TraktSyncType.HistoryRemove: syncHistoryRemove.AddEpisode(epsync.Slug, epsync.Season, epsync.EpNumber, epsync.EpDate); break; } } } } } #endregion // refresh online info, just in case it was chnaged by the last operations if (!GetTraktCollectionInfo(ref collected, ref watched)) return; #region Online Collection Sync /////////////////////////////////////////////////////////////////////////////////////// // Now look at the collection according to Trakt, and remove it if we don't have it locally /////////////////////////////////////////////////////////////////////////////////////// CrossRef_AniDB_TraktV2Repository repCrossRef = new CrossRef_AniDB_TraktV2Repository(); counter = 0; foreach (TraktV2ShowCollectedResult col in collected) { counter++; logger.Trace("Syncing check - Online collection: {0} / {1} - {2}", counter, collected.Count, col.show.Title); //continue; // check if we have this series locally List<CrossRef_AniDB_TraktV2> xrefs = repCrossRef.GetByTraktID(col.show.ids.slug); if (xrefs.Count > 0) { foreach (CrossRef_AniDB_TraktV2 xref in xrefs) { AnimeSeries locSeries = repSeries.GetByAnimeID(xref.AnimeID); if (locSeries == null) continue; TraktSummaryContainer traktSummary = new TraktSummaryContainer(); traktSummary.Populate(locSeries.AniDB_ID); if (traktSummary.CrossRefTraktV2 == null || traktSummary.CrossRefTraktV2.Count == 0) continue; // if we have this series locSeries, let's sync the whole series foreach (AnimeEpisode ep in locSeries.GetAnimeEpisodes()) { if (ep.EpisodeTypeEnum == enEpisodeType.Episode || ep.EpisodeTypeEnum == enEpisodeType.Special) { EpisodeSyncDetails epsync = ReconSyncTraktEpisode(locSeries, ep, traktSummary, traktUsers, collected, watched, false); if (epsync != null) { switch (epsync.SyncType) { case TraktSyncType.CollectionAdd: syncCollectionAdd.AddEpisode(epsync.Slug, epsync.Season, epsync.EpNumber, epsync.EpDate); break; case TraktSyncType.CollectionRemove: syncCollectionRemove.AddEpisode(epsync.Slug, epsync.Season, epsync.EpNumber, epsync.EpDate); break; case TraktSyncType.HistoryAdd: syncHistoryAdd.AddEpisode(epsync.Slug, epsync.Season, epsync.EpNumber, epsync.EpDate); break; case TraktSyncType.HistoryRemove: syncHistoryRemove.AddEpisode(epsync.Slug, epsync.Season, epsync.EpNumber, epsync.EpDate); break; } } } } } } else { // Actually we can't do this, because the user may have other non Anime series and Movies /* // series doesn't exist locally at all, so let's completely remove it from Trakt foreach (TraktV2CollectedSeason colSeason in col.seasons) { foreach (TraktV2CollectedEpisode colEp in colSeason.episodes) { string msg = string.Format("SYNC ONLINE: Removing from Trakt Collection: Slug: {0} - S:{1} - EP:{2}", col.show.ids.slug, colSeason.number, colEp.number); logger.Trace(msg); SyncEpisodeToTrakt(TraktSyncType.CollectionRemove, col.show.ids.slug, colSeason.number, colEp.number, DateTime.Now, false); } }*/ } } #endregion // refresh online info, just in case it was chnaged by the last operations if (!GetTraktCollectionInfo(ref collected, ref watched)) return; #region Online History (Watched/Unwatched) Sync /////////////////////////////////////////////////////////////////////////////////////// // Now look at the history according to Trakt, and remove it if we don't have it locally /////////////////////////////////////////////////////////////////////////////////////// counter = 0; foreach (TraktV2ShowWatchedResult wtch in watched) { counter++; logger.Trace("Syncing check - Online History: {0} / {1} - {2}", counter, watched.Count, wtch.show.Title); //continue; // check if we have this series locally List<CrossRef_AniDB_TraktV2> xrefs = repCrossRef.GetByTraktID(wtch.show.ids.slug); if (xrefs.Count > 0) { foreach (CrossRef_AniDB_TraktV2 xref in xrefs) { AnimeSeries locSeries = repSeries.GetByAnimeID(xref.AnimeID); if (locSeries == null) continue; TraktSummaryContainer traktSummary = new TraktSummaryContainer(); traktSummary.Populate(locSeries.AniDB_ID); if (traktSummary.CrossRefTraktV2 == null || traktSummary.CrossRefTraktV2.Count == 0) continue; // if we have this series locSeries, let's sync the whole series foreach (AnimeEpisode ep in locSeries.GetAnimeEpisodes()) { if (ep.EpisodeTypeEnum == enEpisodeType.Episode || ep.EpisodeTypeEnum == enEpisodeType.Special) { EpisodeSyncDetails epsync = ReconSyncTraktEpisode(locSeries, ep, traktSummary, traktUsers, collected, watched, false); if (epsync != null) { switch (epsync.SyncType) { case TraktSyncType.CollectionAdd: syncCollectionAdd.AddEpisode(epsync.Slug, epsync.Season, epsync.EpNumber, epsync.EpDate); break; case TraktSyncType.CollectionRemove: syncCollectionRemove.AddEpisode(epsync.Slug, epsync.Season, epsync.EpNumber, epsync.EpDate); break; case TraktSyncType.HistoryAdd: syncHistoryAdd.AddEpisode(epsync.Slug, epsync.Season, epsync.EpNumber, epsync.EpDate); break; case TraktSyncType.HistoryRemove: syncHistoryRemove.AddEpisode(epsync.Slug, epsync.Season, epsync.EpNumber, epsync.EpDate); break; } } } } } } else { // Actually we can't do this, because the user may have other non Anime series and Movies /* // series doesn't exist locally at all, so let's completely remove it from Trakt foreach (TraktV2WatchedSeason wtchSeason in wtch.seasons) { foreach (TraktV2WatchedEpisode wtchEp in wtchSeason.episodes) { string msg = string.Format("SYNC ONLINE: Removing from Trakt History: Slug: {0} - S:{1} - EP:{2}", wtch.show.ids.slug, wtchSeason.number, wtchEp.number); logger.Trace(msg); SyncEpisodeToTrakt(TraktSyncType.HistoryRemove, wtch.show.ids.slug, wtchSeason.number, wtchEp.number, DateTime.Now, false); } }*/ } } #endregion // send the data to Trakt string json = string.Empty; string url = TraktURIs.SyncCollectionAdd; string retData = string.Empty; if (syncCollectionAdd.shows != null && syncCollectionAdd.shows.Count > 0) { json = JSONHelper.Serialize<TraktV2SyncCollectionEpisodesByNumber>(syncCollectionAdd); url = TraktURIs.SyncCollectionAdd; retData = string.Empty; SendData(url, json, "POST", BuildRequestHeaders(), ref retData); } if (syncCollectionRemove.shows != null && syncCollectionRemove.shows.Count > 0) { json = JSONHelper.Serialize<TraktV2SyncCollectionEpisodesByNumber>(syncCollectionRemove); url = TraktURIs.SyncCollectionRemove; retData = string.Empty; SendData(url, json, "POST", BuildRequestHeaders(), ref retData); } if (syncHistoryAdd.shows != null && syncHistoryAdd.shows.Count > 0) { json = JSONHelper.Serialize<TraktV2SyncWatchedEpisodesByNumber>(syncHistoryAdd); url = TraktURIs.SyncHistoryAdd; retData = string.Empty; SendData(url, json, "POST", BuildRequestHeaders(), ref retData); } if (syncHistoryRemove.shows != null && syncHistoryRemove.shows.Count > 0) { json = JSONHelper.Serialize<TraktV2SyncWatchedEpisodesByNumber>(syncHistoryRemove); url = TraktURIs.SyncHistoryRemove; retData = string.Empty; SendData(url, json, "POST", BuildRequestHeaders(), ref retData); } logger.Trace("Test"); } catch (Exception ex) { logger.ErrorException("Error in TraktTVHelper.SyncCollectionToTrakt: " + ex.ToString(), ex); } }
public static void SyncCollectionToTrakt_Series(AnimeSeries series) { try { // check that we have at least one user nominated for Trakt JMMUserRepository repUsers = new JMMUserRepository(); List<JMMUser> traktUsers = repUsers.GetTraktUsers(); if (traktUsers.Count == 0) return; AniDB_AnimeRepository repAnime = new AniDB_AnimeRepository(); AniDB_Anime anime = repAnime.GetByAnimeID(series.AniDB_ID); if (anime == null) return; TraktSummaryContainer traktSummary = new TraktSummaryContainer(); traktSummary.Populate(series.AniDB_ID); if (traktSummary.CrossRefTraktV2 == null || traktSummary.CrossRefTraktV2.Count == 0) return; // now get the full users collection from Trakt List<TraktV2ShowCollectedResult> collected = new List<TraktV2ShowCollectedResult>(); List<TraktV2ShowWatchedResult> watched = new List<TraktV2ShowWatchedResult>(); if (!GetTraktCollectionInfo(ref collected, ref watched)) return; foreach (AnimeEpisode ep in series.GetAnimeEpisodes()) { if (ep.EpisodeTypeEnum == enEpisodeType.Episode || ep.EpisodeTypeEnum == enEpisodeType.Special) { ReconSyncTraktEpisode(series, ep, traktSummary, traktUsers, collected, watched, true); } } } catch (Exception ex) { logger.ErrorException("Error in TraktTVHelper.SyncCollectionToTrakt_Series: " + ex.ToString(), ex); } }
public static void SyncCollectionToTrakt_Series(AnimeSeries series) { try { // check that we have at least one user nominated for Trakt JMMUserRepository repUsers = new JMMUserRepository(); List<JMMUser> traktUsers = repUsers.GetTraktUsers(); if (traktUsers.Count == 0) return; string url = string.Format(Constants.TraktTvURLs.URLPostShowEpisodeLibrary, Constants.TraktTvURLs.APIKey); string urlSeen = string.Format(Constants.TraktTvURLs.URLPostShowEpisodeSeen, Constants.TraktTvURLs.APIKey); int retEpNum = 0, retSeason = 0; CrossRef_AniDB_Trakt xref = series.CrossRefTrakt; if (xref == null) return; Trakt_ShowRepository repShows = new Trakt_ShowRepository(); Trakt_Show show = repShows.GetByTraktID(xref.TraktID); if (show == null) return; if (!show.TvDB_ID.HasValue) return; Dictionary<int, int> dictTraktSeasons = null; Dictionary<int, Trakt_Episode> dictTraktEpisodes = null; Dictionary<int, Trakt_Episode> dictTraktSpecials = null; GetDictTraktEpisodesAndSeasons(show, ref dictTraktEpisodes, ref dictTraktSpecials, ref dictTraktSeasons); TraktTVPost_ShowEpisodeLibrary postLibrary = new TraktTVPost_ShowEpisodeLibrary(); postLibrary.episodes = new List<TraktTVSeasonEpisode>(); postLibrary.SetCredentials(); postLibrary.imdb_id = ""; postLibrary.title = show.Title; postLibrary.year = show.Year; postLibrary.tvdb_id = show.TvDB_ID.Value.ToString(); TraktTVPost_ShowEpisodeSeen postSeen = new TraktTVPost_ShowEpisodeSeen(); postSeen.episodes = new List<TraktTVSeasonEpisode>(); postSeen.SetCredentials(); postSeen.imdb_id = ""; postSeen.title = show.Title; postSeen.year = show.Year; postSeen.tvdb_id = show.TvDB_ID.Value.ToString(); foreach (AnimeEpisode ep in series.GetAnimeEpisodes()) { if (ep.GetVideoLocals().Count > 0) { retEpNum = -1; retSeason = -1; GetTraktEpisodeNumber(ep, series, show, xref.TraktSeasonNumber, ref retEpNum, ref retSeason, dictTraktEpisodes, dictTraktSpecials, dictTraktSeasons); if (retEpNum < 0) continue; TraktTVSeasonEpisode traktEp = new TraktTVSeasonEpisode(); traktEp.episode = retEpNum.ToString(); traktEp.season = retSeason.ToString(); postLibrary.episodes.Add(traktEp); AnimeEpisode_User userRecord = null; foreach (JMMUser juser in traktUsers) { userRecord = ep.GetUserRecord(juser.JMMUserID); if (userRecord != null) break; } if (userRecord != null) postSeen.episodes.Add(traktEp); } } if (postLibrary.episodes.Count > 0) { logger.Info("PostShowEpisodeLibrary: {0}/{1}/{2} eps", show.Title, show.TraktID, postLibrary.episodes.Count); string json = JSONHelper.Serialize<TraktTVPost_ShowEpisodeLibrary>(postLibrary); string jsonResponse = SendData(url, json); logger.Info("PostShowEpisodeLibrary RESPONSE: {0}", jsonResponse); } if (postSeen.episodes.Count > 0) { logger.Info("PostShowEpisodeSeen: {0}/{1}/{2} eps", show.Title, show.TraktID, postSeen.episodes.Count); string json = JSONHelper.Serialize<TraktTVPost_ShowEpisodeSeen>(postSeen); string jsonResponse = SendData(urlSeen, json); logger.Info("PostShowEpisodeSeen RESPONSE: {0}", jsonResponse); } } catch (Exception ex) { logger.ErrorException("Error in TraktTVHelper.SyncCollectionToTrakt_Series: " + ex.ToString(), ex); } }