/// <summary> /// Sends episode sync data to Trakt /// </summary> /// <param name="syncData">The sync data to send</param> /// <param name="mode">The sync mode to use</param> public static TraktResponse SyncEpisodeLibrary(TraktEpisodeSync syncData, TraktSyncModes mode) { // check that we have everything we need // server can accept title/year if imdb id is not supplied if (syncData == null || string.IsNullOrEmpty(syncData.SeriesID) && string.IsNullOrEmpty(syncData.Title) && string.IsNullOrEmpty(syncData.Year)) { TraktResponse error = new TraktResponse { Error = "Not enough information to send to server", Status = "failure" }; return error; } // serialize Scrobble object to JSON and send to server string response = Transmit(string.Format(TraktURIs.SyncEpisodeLibrary, mode), syncData.ToJSON()); // return success or failure return response.FromJSON<TraktResponse>(); }
public bool SyncSeries() { try { TestStatus = "[Trakt.SyncSeries]"; Guid[] types = { MediaAspect.ASPECT_ID, SeriesAspect.ASPECT_ID }; MediaItemQuery mediaItemQuery = new MediaItemQuery(types, null, null); var contentDirectory = ServiceRegistration.Get<IServerConnectionManager>().ContentDirectory; if (contentDirectory == null) { TestStatus = "[Trakt.MediaLibraryNotConnected]"; return false; } var episodes = contentDirectory.Search(mediaItemQuery, true); var series = episodes.ToLookup(GetSeriesKey); foreach (var serie in series) { var imdbId = serie.Select(episode => { string value; return MediaItemAspect.TryGetAttribute(episode.Aspects, SeriesAspect.ATTR_IMDB_ID, out value) ? value : null; }).FirstOrDefault(value => !string.IsNullOrWhiteSpace(value)); var tvdbId = serie.Select(episode => { int value; return MediaItemAspect.TryGetAttribute(episode.Aspects, SeriesAspect.ATTR_TVDB_ID, out value) ? value : 0; }).FirstOrDefault(value => value != 0); TraktEpisodeSync syncData = new TraktEpisodeSync { UserName = Username, Password = Password, EpisodeList = new List<TraktEpisodeSync.Episode>(), Title = serie.Key, Year = serie.Min(e => { int year; string seriesTitle; GetSeriesTitleAndYear(e, out seriesTitle, out year); return year; }).ToString() }; if (!string.IsNullOrWhiteSpace(imdbId)) syncData.IMDBID = imdbId; if (tvdbId > 0) syncData.SeriesID = tvdbId.ToString(); HashSet<TraktEpisodeSync.Episode> uniqueEpisodes = new HashSet<TraktEpisodeSync.Episode>(); foreach (var episode in serie) { string seriesTitle; int year = 0; if (!GetSeriesTitle /*AndYear*/(episode, out seriesTitle /*, out year*/)) continue; // First send all movies to Trakt that we have so they appear in library CollectionUtils.AddAll(uniqueEpisodes, ToSeries(episode)); } syncData.EpisodeList = uniqueEpisodes.ToList(); TraktSyncModes traktSyncMode = TraktSyncModes.library; var response = TraktAPI.SyncEpisodeLibrary(syncData, traktSyncMode); ServiceRegistration.Get<ILogger>().Info("Trakt.tv: Series '{0}' '{1}': {2}{3}", syncData.Title, traktSyncMode, response.Message, response.Error); // Then send only the watched movies as "seen" uniqueEpisodes.Clear(); foreach (var seenEpisode in episodes.Where(IsWatched)) CollectionUtils.AddAll(uniqueEpisodes, ToSeries(seenEpisode)); syncData.EpisodeList = uniqueEpisodes.ToList(); traktSyncMode = TraktSyncModes.seen; response = TraktAPI.SyncEpisodeLibrary(syncData, traktSyncMode); ServiceRegistration.Get<ILogger>().Info("Trakt.tv: Series '{0}' '{1}': {2}{3}", syncData.Title, traktSyncMode, response.Message, response.Error); return true; } } catch (Exception ex) { ServiceRegistration.Get<ILogger>().Error("Trakt.tv: Exception while synchronizing media library.", ex); } return false; }
/// <summary> /// Add/Remove episode to/from watchlist /// </summary> /// <param name="syncData">The sync data to send</param> /// <param name="mode">The sync mode to use</param> /// <returns>The response from trakt</returns> public static TraktResponse SyncEpisodeWatchList(TraktEpisodeSync syncData, TraktSyncModes mode) { // check that we have everything we need if (syncData == null || syncData.EpisodeList.Count == 0) { TraktResponse error = new TraktResponse { Error = "Not enough information to send to server", Status = "failure" }; return error; } // serialize Scrobble object to JSON and send to server string response = Transmit(string.Format(TraktURIs.SyncEpisodeWatchList, mode), syncData.ToJSON()); // return success or failure return response.FromJSON<TraktResponse>(); }