Exemplo n.º 1
0
        /// <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
            if (syncData == null || string.IsNullOrEmpty(syncData.SeriesID))
            {
                return(null);
            }

            // serialize data to JSON and send to server
            string response = TraktWeb.Transmit(string.Format(TraktURIs.SyncEpisodeLibrary, mode.ToString()), syncData.ToJSON());

            // return success or failure
            return(response.FromJSON <TraktResponse>());
        }
Exemplo n.º 2
0
        public bool SyncMovies()
        {
            try
            {
                TestStatus = "[Trakt.SyncMovies]";
                Guid[] types            = { MediaAspect.ASPECT_ID, MovieAspect.ASPECT_ID };
                var    contentDirectory = ServiceRegistration.Get <IServerConnectionManager>().ContentDirectory;
                if (contentDirectory == null)
                {
                    TestStatus = "[Trakt.MediaLibraryNotConnected]";
                    return(false);
                }
                var            movies   = contentDirectory.Search(new MediaItemQuery(types, null, null), true);
                TraktMovieSync syncData = new TraktMovieSync {
                    UserName = Username, Password = Password, MovieList = new List <TraktMovieSync.Movie>()
                };
                // First send all movies to Trakt that we have so they appear in library
                foreach (var movie in movies)
                {
                    syncData.MovieList.Add(ToMovie(movie));
                }

                TraktSyncModes traktSyncMode = TraktSyncModes.library;
                var            response      = TraktAPI.SyncMovieLibrary(syncData, traktSyncMode);
                ServiceRegistration.Get <ILogger>().Info("Trakt.tv: Movies '{0}': {1} inserted, {2} existing, {3} skipped movies.", traktSyncMode, response.Inserted, SafeCount(response.AlreadyExistMovies), SafeCount(response.SkippedMovies));

                syncData.MovieList.Clear();
                // Then send only the watched movies as "seen"
                foreach (var movie in movies.Where(IsWatched))
                {
                    syncData.MovieList.Add(ToMovie(movie));
                }

                traktSyncMode = TraktSyncModes.seen;
                response      = TraktAPI.SyncMovieLibrary(syncData, traktSyncMode);
                ServiceRegistration.Get <ILogger>().Info("Trakt.tv: Movies '{0}': {1} inserted, {2} existing, {3} skipped movies.", traktSyncMode, response.Inserted, SafeCount(response.AlreadyExistMovies), SafeCount(response.SkippedMovies));
                return(true);
            }
            catch (Exception ex)
            {
                ServiceRegistration.Get <ILogger>().Error("Trakt.tv: Exception while synchronizing media library.", ex);
            }
            return(false);
        }
Exemplo n.º 3
0
        public static List <DBEpisode> GetEpisodesToSync(DBSeries series, TraktSyncModes mode)
        {
            List <DBEpisode> episodes = new List <DBEpisode>();

            SQLCondition conditions = new SQLCondition();

            if (series == null)
            {
                // Get episodes for every series
                conditions.Add(new DBOnlineEpisode(), DBOnlineEpisode.cSeriesID, 0, SQLConditionType.GreaterThan);
            }
            else
            {
                // Get episodes for a single series
                conditions.Add(new DBOnlineEpisode(), DBOnlineEpisode.cSeriesID, series[DBSeries.cID], SQLConditionType.Equal);
            }

            switch (mode)
            {
            case TraktSyncModes.library:
                conditions.Add(new DBOnlineEpisode(), DBOnlineEpisode.cTraktLibrary, 0, SQLConditionType.Equal);
                conditions.Add(new DBOnlineEpisode(), DBOnlineEpisode.cHidden, 0, SQLConditionType.Equal);
                conditions.Add(new DBEpisode(), DBEpisode.cFilename, string.Empty, SQLConditionType.NotEqual);
                episodes = DBEpisode.Get(conditions, false);
                break;

            case TraktSyncModes.seen:
                conditions.Add(new DBOnlineEpisode(), DBOnlineEpisode.cHidden, 0, SQLConditionType.Equal);
                conditions.Add(new DBOnlineEpisode(), DBOnlineEpisode.cWatched, 1, SQLConditionType.Equal);
                conditions.Add(new DBOnlineEpisode(), DBOnlineEpisode.cTraktSeen, 0, SQLConditionType.Equal);
                episodes = DBEpisode.Get(conditions, false);
                break;

            case TraktSyncModes.unseen:
                conditions.Add(new DBOnlineEpisode(), DBOnlineEpisode.cHidden, 0, SQLConditionType.Equal);
                conditions.Add(new DBOnlineEpisode(), DBOnlineEpisode.cTraktSeen, 2, SQLConditionType.Equal);
                episodes = DBEpisode.Get(conditions, false);
                break;
            }

            return(episodes);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Syncronize our collection on trakt
        /// </summary>
        /// <param name="episodes">local tvseries dbepisode list</param>
        /// <param name="mode">trakt sync mode</param>
        private void SyncLibrary(List <FileLocal> episodes, TraktSyncModes mode)
        {
            if (episodes.Count == 0)
            {
                return;
            }

            // get unique series ids
            var uniqueSeries = (from s in episodes where (s.AniDB_File != null && s.AniDB_File.AnimeSeries.TvDB_ID > 0) select s.AniDB_File.AnimeSeries.TvDB_ID).Distinct().ToList();

            if (uniqueSeries.Count == 0)
            {
                TraktLogger.Info("TVDb info not available for series, can not sync '{0}' with trakt.", mode.ToString());
            }

            // go over each series, can only send one series at a time
            foreach (int seriesid in uniqueSeries)
            {
                // There should only be one series
                List <AnimeSeries> series = AnimeSeries.GetSeriesWithSpecificTvDB(seriesid);
                if (series == null)
                {
                    continue;
                }

                TraktLogger.Info("Synchronizing '{0}' episodes for series '{1}'.", mode.ToString(), series[0].ToString());

                // upload to trakt
                TraktEpisodeSync episodeSync = CreateSyncData(series[0], episodes);
                if (episodeSync != null)
                {
                    TraktResponse response = TraktAPI.TraktAPI.SyncEpisodeLibrary(episodeSync, mode);

                    // check for any error and log result
                    TraktAPI.TraktAPI.LogTraktResponse(response);

                    // wait a short period before uploading another series
                    Thread.Sleep(2000);
                }
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Syncronize our collection on trakt
        /// </summary>
        /// <param name="episodes">local tvseries dbepisode list</param>
        /// <param name="mode">trakt sync mode</param>
        private void SyncLibrary(List<DBEpisode> episodes, TraktSyncModes mode)
        {
            // get unique series ids
            var uniqueSeries = (from s in episodes select s[DBEpisode.cSeriesID].ToString()).Distinct().ToList();

            // go over each series, can only send one series at a time
            foreach (string seriesid in uniqueSeries)
            {
                // get series and check if we should ignore it
                DBSeries series = Helper.getCorrespondingSeries(int.Parse(seriesid));
                if (series == null || series[DBOnlineSeries.cTraktIgnore]) continue;

                TraktLogger.Info("Synchronizing '{0}' episodes for series '{1}'.", mode.ToString(), series.ToString());

                // upload to trakt
                TraktResponse response = TraktAPI.TraktAPI.SyncEpisodeLibrary(CreateSyncData(series, episodes), mode);

                // check for any error and log result
                TraktAPI.TraktAPI.LogTraktResponse(response);

                // wait a short period before uploading another series
                Thread.Sleep(2000);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Sends movie sync data to Trakt
        /// </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 TraktMovieSyncResponse SyncMovieLibrary(TraktMovieSync syncData, TraktSyncModes mode)
        {
            // check that we have everything we need
            if (syncData == null || syncData.MovieList.Count == 0)
                return null;

            // serialize data to JSON and send to server
            string response = TraktWeb.Transmit(string.Format(TraktURIs.SyncMovieLibrary, mode.ToString()), syncData.ToJSON());

            // return success or failure
            return response.FromJSON<TraktMovieSyncResponse>();
        }
Exemplo n.º 7
0
        /// <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
            if (syncData == null || string.IsNullOrEmpty(syncData.SeriesID))
                return null;

            // serialize data to JSON and send to server
            string response = TraktWeb.Transmit(string.Format(TraktURIs.SyncEpisodeLibrary, mode.ToString()), syncData.ToJSON());

            // return success or failure
            return response.FromJSON<TraktResponse>();
        }
 /// <summary>
 /// Syncs Movie data in another thread
 /// </summary>
 /// <param name="syncData">Data to sync</param>
 /// <param name="mode">The Syncing mode to use</param>
 private void SyncMovie(TraktMovieSync syncData, TraktSyncModes mode)
 {
     BackgroundWorker moviesync = new BackgroundWorker();
     moviesync.DoWork += new DoWorkEventHandler(moviesync_DoWork);
     moviesync.RunWorkerCompleted += new RunWorkerCompletedEventHandler(moviesync_RunWorkerCompleted);
     moviesync.RunWorkerAsync(new MovieSyncAndMode { SyncData = syncData, Mode = mode });
 }
Exemplo n.º 9
0
        /// <summary>
        /// Syncronize our collection on trakt
        /// </summary>
        /// <param name="episodes">local tvseries dbepisode list</param>
        /// <param name="mode">trakt sync mode</param>
        private void SyncLibrary(List<FileLocal> episodes, TraktSyncModes mode)
        {
            if (episodes.Count == 0) return;

            // get unique series ids
            var uniqueSeries = (from s in episodes where (s.AniDB_File != null && s.AniDB_File.AnimeSeries.TvDB_ID > 0) select s.AniDB_File.AnimeSeries.TvDB_ID).Distinct().ToList();

            if (uniqueSeries.Count == 0)
            {
                TraktLogger.Info("TVDb info not available for series, can not sync '{0}' with trakt.", mode.ToString());
            }

            // go over each series, can only send one series at a time
            foreach (int seriesid in uniqueSeries)
            {
                // There should only be one series
                List<AnimeSeries> series = AnimeSeries.GetSeriesWithSpecificTvDB(seriesid);
                if (series == null) continue;

                TraktLogger.Info("Synchronizing '{0}' episodes for series '{1}'.", mode.ToString(), series[0].ToString());

                // upload to trakt
                TraktEpisodeSync episodeSync = CreateSyncData(series[0], episodes);
                if (episodeSync != null)
                {
                    TraktResponse response = TraktAPI.TraktAPI.SyncEpisodeLibrary(episodeSync, mode);

                    // check for any error and log result
                    TraktAPI.TraktAPI.LogTraktResponse(response);

                    // wait a short period before uploading another series
                    Thread.Sleep(2000);
                }
            }
        }
Exemplo n.º 10
0
        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);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Sends movie sync data to Trakt
        /// </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 TraktMovieSyncResponse SyncMovieLibrary(TraktMovieSync syncData, TraktSyncModes mode)
        {
            // check that we have everything we need
            if (syncData == null || syncData.MovieList.Count == 0)
            {
                return(null);
            }

            // serialize data to JSON and send to server
            string response = TraktWeb.Transmit(string.Format(TraktURIs.SyncMovieLibrary, mode.ToString()), syncData.ToJSON());

            // return success or failure
            return(response.FromJSON <TraktMovieSyncResponse>());
        }
Exemplo n.º 12
0
 public static List <DBEpisode> GetEpisodesToSync(TraktSyncModes mode)
 {
     // Get episodes for every series
     return(GetEpisodesToSync(null, mode));
 }
Exemplo n.º 13
0
        public static void SynchronizeLibrary(List <DBEpisode> episodes, TraktSyncModes mode)
        {
            if (episodes.Count == 0)
            {
                return;
            }

            // get unique series ids
            var uniqueSeriesIDs = (from seriesIDs in episodes
                                   select seriesIDs[DBEpisode.cSeriesID].ToString()).Distinct().ToList();

            // go over each series, can only send one series at a time
            foreach (string seriesID in uniqueSeriesIDs)
            {
                DBSeries series = Helper.getCorrespondingSeries(int.Parse(seriesID));
                if (series == null || series[DBOnlineSeries.cTraktIgnore])
                {
                    continue;
                }

                MPTVSeriesLog.Write("Trakt: Synchronizing '{0}' episodes for series '{1}'.", mode.ToString(), series.ToString());

                TraktSync traktSync = GetTraktSyncObject(series, episodes);

                // upload to trakt
                TraktResponse response = TraktAPI.SyncEpisodeLibrary(traktSync, mode);
                if (response == null)
                {
                    MPTVSeriesLog.Write("Trakt Error: Response from server was unexpected.");
                    continue;
                }

                // check for any error and log result
                CheckTraktErrorAndNotify(response, false);

                if (response.Status == "success")
                {
                    SQLCondition conditions = new SQLCondition();

                    // flag episodes and commit to database
                    switch (mode)
                    {
                    case TraktSyncModes.seen:
                        conditions.Add(new DBOnlineEpisode(), DBOnlineEpisode.cSeriesID, seriesID, SQLConditionType.Equal);
                        conditions.Add(new DBOnlineEpisode(), DBOnlineEpisode.cWatched, 1, SQLConditionType.Equal);
                        conditions.Add(new DBOnlineEpisode(), DBOnlineEpisode.cHidden, 0, SQLConditionType.Equal);
                        conditions.Add(new DBOnlineEpisode(), DBOnlineEpisode.cTraktSeen, 0, SQLConditionType.Equal);

                        // we always flag traktLibrary field as the 'traktSeen' field counts as part of library
                        DBOnlineEpisode.GlobalSet(new DBOnlineEpisode(), DBOnlineEpisode.cTraktLibrary, 1, conditions);
                        DBOnlineEpisode.GlobalSet(new DBOnlineEpisode(), DBOnlineEpisode.cTraktSeen, 1, conditions);
                        break;

                    case TraktSyncModes.library:
                        // we can't do a global set as our conditions are from two different tables
                        // where filename is not empty and traktLibrary = 0
                        foreach (DBEpisode ep in episodes.Where(e => e[DBEpisode.cSeriesID] == seriesID))
                        {
                            ep[DBOnlineEpisode.cTraktLibrary] = 1;
                            ep.Commit();
                        }
                        break;

                    case TraktSyncModes.unseen:
                        conditions.Add(new DBOnlineEpisode(), DBOnlineEpisode.cSeriesID, seriesID, SQLConditionType.Equal);
                        conditions.Add(new DBOnlineEpisode(), DBOnlineEpisode.cHidden, 0, SQLConditionType.Equal);
                        conditions.Add(new DBOnlineEpisode(), DBOnlineEpisode.cTraktSeen, 2, SQLConditionType.Equal);
                        DBOnlineEpisode.GlobalSet(new DBOnlineEpisode(), DBOnlineEpisode.cTraktSeen, 0, conditions);
                        break;

                    case TraktSyncModes.unlibrary:
                        break;
                    }
                }

                // wait a short period before uploading another series
                Thread.Sleep(2000);
            }
        }
Exemplo n.º 14
0
    /// <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>();
    }
Exemplo n.º 15
0
    /// <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>();
    }