Example #1
0
        public bool SyncMovies()
        {
            ISettingsManager settingsManager = ServiceRegistration.Get <ISettingsManager>();
            TraktSettings    settings        = settingsManager.Load <TraktSettings>();

            #region Get online data from cache

            #region Get unwatched / watched movies from trakt.tv
            IEnumerable <TraktMovieWatched> traktWatchedMovies = null;

            var traktUnWatchedMovies = TraktCache.GetUnWatchedMoviesFromTrakt();
            if (traktUnWatchedMovies == null)
            {
                TraktLogger.Error("Error getting unwatched movies from trakt server, unwatched and watched sync will be skipped");
            }
            else
            {
                TraktLogger.Info("There are {0} unwatched movies since the last sync with trakt.tv", traktUnWatchedMovies.Count());

                traktWatchedMovies = TraktCache.GetWatchedMoviesFromTrakt();
                if (traktWatchedMovies == null)
                {
                    TraktLogger.Error("Error getting watched movies from trakt server, watched sync will be skipped");
                }
                else
                {
                    TraktLogger.Info("There are {0} watched movies in trakt.tv library", traktWatchedMovies.Count().ToString());
                }
            }
            #endregion

            #region Get collected movies from trakt.tv
            var traktCollectedMovies = TraktCache.GetCollectedMoviesFromTrakt();
            if (traktCollectedMovies == null)
            {
                TraktLogger.Error("Error getting collected movies from trakt server");
            }
            else
            {
                TraktLogger.Info("There are {0} collected movies in trakt.tv library", traktCollectedMovies.Count());
            }
            #endregion

            #region Get rated movies from trakt.tv
            //var traktRatedMovies = TraktCache.GetRatedMoviesFromTrakt();
            //if (traktRatedMovies == null)
            //{
            //  TraktLogger.Error("Error getting rated movies from trakt server");
            //}
            //else
            //{
            //  TraktLogger.Info("There are {0} rated movies in trakt.tv library", traktRatedMovies.Count());
            //}
            #endregion

            #region Get watchlisted movies from trakt.tv
            //var traktWatchlistedMovies = TraktCache.GetWatchlistedMoviesFromTrakt();
            //if (traktWatchlistedMovies == null)
            //{
            //  TraktLogger.Error("Error getting watchlisted movies from trakt server");
            //}
            //else
            //{
            //  TraktLogger.Info("There are {0} watchlisted movies in trakt.tv library", traktWatchlistedMovies.Count());
            //}
            #endregion

            #region Get custom lists from trakt.tv
            //var traktCustomLists = TraktCache.GetCustomLists();
            //if (traktCustomLists == null)
            //{
            //  TraktLogger.Error("Error getting custom lists from trakt server");
            //}
            //else
            //{
            //  TraktLogger.Info("There are {0} custom lists in trakt.tv library", traktCustomLists.Count());
            //}
            #endregion
            #endregion

            try
            {
                TestStatus = "[Trakt.SyncMovies]";
                Guid[] types            = { MediaAspect.ASPECT_ID, MovieAspect.ASPECT_ID, VideoAspect.ASPECT_ID, ImporterAspect.ASPECT_ID };
                var    contentDirectory = ServiceRegistration.Get <IServerConnectionManager>().ContentDirectory;
                if (contentDirectory == null)
                {
                    TestStatus = "[Trakt.MediaLibraryNotConnected]";
                    return(false);
                }

                #region Get local database info

                var collectedMovies = contentDirectory.Search(new MediaItemQuery(types, null, null), true);

                TraktLogger.Info("Found {0} movies available to sync in local database", collectedMovies.Count);

                // get the movies that we have watched
                var watchedMovies = collectedMovies.Where(IsWatched).ToList();
                TraktLogger.Info("Found {0} watched movies available to sync in local database", watchedMovies.Count);

                #endregion

                #region Add movies to watched history at trakt.tv
                if (traktWatchedMovies != null)
                {
                    var syncWatchedMovies = new List <TraktSyncMovieWatched>();
                    TraktLogger.Info("Finding movies to add to trakt.tv watched history");

                    syncWatchedMovies = (from movie in watchedMovies
                                         where !traktWatchedMovies.ToList().Exists(c => MovieMatch(movie, c.Movie))
                                         select new TraktSyncMovieWatched
                    {
                        Ids = new TraktMovieId {
                            Imdb = GetMovieImdbId(movie), Tmdb = GetMovieTmdbId(movie)
                        },
                        Title = GetMovieTitle(movie),
                        Year = GetMovieYear(movie),
                        WatchedAt = GetLastPlayedDate(movie),
                    }).ToList();

                    TraktLogger.Info("Adding {0} movies to trakt.tv watched history", syncWatchedMovies.Count);

                    if (syncWatchedMovies.Count > 0)
                    {
                        // update internal cache
                        TraktCache.AddMoviesToWatchHistory(syncWatchedMovies);

                        int pageSize = settings.SyncBatchSize;
                        int pages    = (int)Math.Ceiling((double)syncWatchedMovies.Count / pageSize);
                        for (int i = 0; i < pages; i++)
                        {
                            TraktLogger.Info("Adding movies [{0}/{1}] to trakt.tv watched history", i + 1, pages);

                            var pagedMovies = syncWatchedMovies.Skip(i * pageSize).Take(pageSize).ToList();

                            pagedMovies.ForEach(s => TraktLogger.Info("Adding movie to trakt.tv watched history. Title = '{0}', Year = '{1}', IMDb ID = '{2}', TMDb ID = '{3}', Date Watched = '{4}'",
                                                                      s.Title, s.Year.HasValue ? s.Year.ToString() : "<empty>", s.Ids.Imdb ?? "<empty>", s.Ids.Tmdb.HasValue ? s.Ids.Tmdb.ToString() : "<empty>", s.WatchedAt));

                            // remove title/year such that match against online ID only
                            if (settings.SkipMoviesWithNoIdsOnSync)
                            {
                                pagedMovies.ForEach(m => { m.Title = null; m.Year = null; });
                            }

                            var response = TraktAPI.AddMoviesToWatchedHistory(new TraktSyncMoviesWatched {
                                Movies = pagedMovies
                            });
                            TraktLogger.LogTraktResponse <TraktSyncResponse>(response);

                            // remove movies from cache which didn't succeed
                            if (response != null && response.NotFound != null && response.NotFound.Movies.Count > 0)
                            {
                                TraktCache.RemoveMoviesFromWatchHistory(response.NotFound.Movies);
                            }
                        }
                    }
                }
                #endregion

                #region Add movies to collection at trakt.tv
                if (traktCollectedMovies != null)
                {
                    var syncCollectedMovies = new List <TraktSyncMovieCollected>();
                    TraktLogger.Info("Finding movies to add to trakt.tv collection");

                    syncCollectedMovies = (from movie in collectedMovies
                                           where !traktCollectedMovies.ToList().Exists(c => MovieMatch(movie, c.Movie))
                                           select new TraktSyncMovieCollected
                    {
                        Ids = new TraktMovieId {
                            Imdb = GetMovieImdbId(movie), Tmdb = GetMovieTmdbId(movie)
                        },
                        Title = GetMovieTitle(movie),
                        Year = GetMovieYear(movie),
                        CollectedAt = GetDateAddedToDb(movie),
                        MediaType = GetVideoMediaType(movie),
                        Resolution = GetVideoResolution(movie),
                        AudioCodec = GetVideoAudioCodec(movie),
                        AudioChannels = "",
                        Is3D = false
                    }).ToList();

                    TraktLogger.Info("Adding {0} movies to trakt.tv watched history", syncCollectedMovies.Count);

                    if (syncCollectedMovies.Count > 0)
                    {
                        //update internal cache
                        TraktCache.AddMoviesToCollection(syncCollectedMovies);
                        int pageSize = settings.SyncBatchSize;
                        int pages    = (int)Math.Ceiling((double)syncCollectedMovies.Count / pageSize);
                        for (int i = 0; i < pages; i++)
                        {
                            TraktLogger.Info("Adding movies [{0}/{1}] to trakt.tv collection", i + 1, pages);

                            var pagedMovies = syncCollectedMovies.Skip(i * pageSize).Take(pageSize).ToList();

                            pagedMovies.ForEach(s => TraktLogger.Info("Adding movie to trakt.tv collection. Title = '{0}', Year = '{1}', IMDb ID = '{2}', TMDb ID = '{3}', Date Added = '{4}', MediaType = '{5}', Resolution = '{6}', Audio Codec = '{7}', Audio Channels = '{8}'",
                                                                      s.Title, s.Year.HasValue ? s.Year.ToString() : "<empty>", s.Ids.Imdb ?? "<empty>", s.Ids.Tmdb.HasValue ? s.Ids.Tmdb.ToString() : "<empty>",
                                                                      s.CollectedAt, s.MediaType ?? "<empty>", s.Resolution ?? "<empty>", s.AudioCodec ?? "<empty>", s.AudioChannels ?? "<empty>"));

                            //// remove title/year such that match against online ID only
                            if (settings.SkipMoviesWithNoIdsOnSync)
                            {
                                pagedMovies.ForEach(m =>
                                {
                                    m.Title = null;
                                    m.Year  = null;
                                });
                            }

                            var response = TraktAPI.AddMoviesToCollecton(new TraktSyncMoviesCollected {
                                Movies = pagedMovies
                            });
                            TraktLogger.LogTraktResponse(response);

                            // remove movies from cache which didn't succeed
                            if (response != null && response.NotFound != null && response.NotFound.Movies.Count > 0)
                            {
                                TraktCache.RemoveMoviesFromCollection(response.NotFound.Movies);
                            }
                        }
                    }
                }
                #endregion
                return(true);
            }
            catch (Exception ex)
            {
                ServiceRegistration.Get <ILogger>().Error("Trakt.tv: Exception while synchronizing media library.", ex);
            }
            return(false);
        }