示例#1
0
文件: IMDb.cs 项目: ljen/TraktRater
        public void ImportRatings()
        {
            ImportCancelled = false;
            List <Dictionary <string, string> > watchedMovies = new List <Dictionary <string, string> >();

            UIUtils.UpdateStatus("Reading IMDb ratings export...");
            if (!ParseCSVFile(CSVFile))
            {
                UIUtils.UpdateStatus("Failed to parse IMDb ratings file!", true);
                Thread.Sleep(2000);
                return;
            }
            if (ImportCancelled)
            {
                return;
            }

            // IMDb does not return the season and episode number for TV Episodes
            // so we should filter down to TV Shows and Movies only

            #region Movies
            var movies = RateItems.Where(r => r[IMDbFieldMapping.cType].ItemType() == IMDbType.Movie);
            if (movies.Count() > 0)
            {
                UIUtils.UpdateStatus(string.Format("Importing {0} movie ratings to trakt.tv.", movies.Count()));

                TraktRatingsResponse response = TraktAPI.TraktAPI.RateMovies(GetRateMoviesData(movies));
                if (response == null || response.Status != "success")
                {
                    UIUtils.UpdateStatus("Error importing movie ratings to trakt.tv.", true);
                    Thread.Sleep(2000);
                }

                // add to list of movies to mark as watched
                watchedMovies.AddRange(movies);
            }
            if (ImportCancelled)
            {
                return;
            }
            #endregion

            #region TV Shows
            var shows = RateItems.Where(r => r[IMDbFieldMapping.cType].ItemType() == IMDbType.Show);
            if (shows.Count() > 0)
            {
                UIUtils.UpdateStatus(string.Format("Importing {0} show ratings to trakt.tv.", shows.Count()));

                TraktRatingsResponse response = TraktAPI.TraktAPI.RateShows(GetRateShowsData(shows));
                if (response == null || response.Status != "success")
                {
                    UIUtils.UpdateStatus("Error importing show ratings to trakt.tv.", true);
                    Thread.Sleep(2000);
                }
            }
            if (ImportCancelled)
            {
                return;
            }
            #endregion

            #region Episodes
            var episodes = RateItems.Where(r => r[IMDbFieldMapping.cType].ItemType() == IMDbType.Episode);
            TraktRateEpisodes episodesRated = null;
            if (episodes.Count() > 0)
            {
                UIUtils.UpdateStatus(string.Format("Importing {0} episode ratings to trakt.tv.", episodes.Count()));

                episodesRated = GetRateEpisodeData(episodes);

                TraktRatingsResponse response = TraktAPI.TraktAPI.RateEpisodes(episodesRated);
                if (response == null || response.Status != "success")
                {
                    UIUtils.UpdateStatus("Error importing episodes ratings to trakt.tv.", true);
                    Thread.Sleep(2000);
                }
            }
            if (ImportCancelled)
            {
                return;
            }
            #endregion

            #region Mark as Watched

            #region Movies

            if (AppSettings.MarkAsWatched && watchedMovies.Count > 0)
            {
                // mark all movies as watched if rated
                UIUtils.UpdateStatus(string.Format("Importing {0} IMDb Movies as Watched...", watchedMovies.Count));
                TraktMovieSyncResponse watchedMoviesResponse = TraktAPI.TraktAPI.SyncMovieLibrary(GetWatchedMoviesData(watchedMovies), TraktSyncModes.seen);
                if (watchedMoviesResponse == null || watchedMoviesResponse.Status != "success")
                {
                    UIUtils.UpdateStatus("Failed to send watched status for IMDb movies.", true);
                    Thread.Sleep(2000);
                    if (ImportCancelled)
                    {
                        return;
                    }
                }
            }

            #endregion

            #region Episodes

            if (AppSettings.MarkAsWatched && episodesRated.Episodes.Count() > 0)
            {
                // mark all episodes as watched if rated
                UIUtils.UpdateStatus(string.Format("Importing {0} IMDb Episodes as Watched...", episodesRated.Episodes.Count));
                var watchedEpisodes = GetWatchedEpisodeData(episodesRated.Episodes);
                foreach (var showSyncData in watchedEpisodes)
                {
                    if (ImportCancelled)
                    {
                        return;
                    }

                    // send the episodes from each show as watched
                    UIUtils.UpdateStatus(string.Format("Importing {0} episodes of {1} as watched...", showSyncData.EpisodeList.Count(), showSyncData.Title));
                    var watchedEpisodesResponse = TraktAPI.TraktAPI.SyncEpisodeLibrary(showSyncData, TraktSyncModes.seen);
                    if (watchedEpisodesResponse == null || watchedEpisodesResponse.Status != "success")
                    {
                        UIUtils.UpdateStatus(string.Format("Failed to send watched status for IMDb '{0}' episodes.", showSyncData.Title), true);
                        Thread.Sleep(2000);
                        continue;
                    }
                }
            }

            #endregion

            #endregion

            return;
        }
示例#2
0
        public void ImportRatings()
        {
            ImportCancelled = false;
            List <TMDbMovie> watchedMovies = new List <TMDbMovie>();

            #region Session Id
            // check if we have a session id
            // note: request token if new is only valid for 60mins
            if (string.IsNullOrEmpty(AppSettings.TMDbSessionId))
            {
                UIUtils.UpdateStatus("Getting TMDb Authentication Session Id...");
                var sessionResponse = TMDbAPI.RequestSessionId(AppSettings.TMDbRequestToken);
                if (sessionResponse == null || !sessionResponse.Success)
                {
                    UIUtils.UpdateStatus("Unable to get TMDb Authentication Session Id.", true);
                    Thread.Sleep(2000);
                    return;
                }
                AppSettings.TMDbSessionId = sessionResponse.SessionId;
            }
            if (ImportCancelled)
            {
                return;
            }
            #endregion

            #region Account Information
            UIUtils.UpdateStatus("Getting TMDb Account Id...");
            var accountInfo = TMDbAPI.GetAccountId(AppSettings.TMDbSessionId);
            if (accountInfo == null)
            {
                UIUtils.UpdateStatus("Unable to get TMDb Account Id.", true);
                Thread.Sleep(2000);
                return;
            }
            if (ImportCancelled)
            {
                return;
            }
            #endregion

            #region Get Rated Movies
            UIUtils.UpdateStatus("Getting first batch of TMDb Rated Movies..");
            var ratings = TMDbAPI.GetRatedMovies(accountInfo.Id.ToString(), AppSettings.TMDbSessionId, 1);
            if (ImportCancelled)
            {
                return;
            }
            #endregion

            #region Import Movie Ratings
            if (ratings.TotalResults == 0)
            {
                return;
            }
            UIUtils.UpdateStatus(string.Format("[{0}/{1}] Importing {2} TMDb Movie Ratings...", ratings.Page, ratings.TotalPages, ratings.Movies.Count));
            var response = TraktAPI.TraktAPI.RateMovies(GetRateMoviesData(ratings.Movies));
            if (response == null || response.Status != "success")
            {
                UIUtils.UpdateStatus("Failed to send ratings for TMDb movies.", true);
                Thread.Sleep(2000);
                if (ImportCancelled)
                {
                    return;
                }
            }

            // add to list of movies to mark as watched
            watchedMovies.AddRange(ratings.Movies);

            // get each page of movies
            for (int i = 2; i <= ratings.TotalPages; i++)
            {
                UIUtils.UpdateStatus(string.Format("[{0}/{1}] Getting next batch of TMDb Rated Movies...", ratings.Page, ratings.TotalPages));
                ratings = TMDbAPI.GetRatedMovies(accountInfo.Id.ToString(), AppSettings.TMDbSessionId, i);
                if (ImportCancelled)
                {
                    return;
                }

                UIUtils.UpdateStatus(string.Format("[{0}/{1}] Importing {2} TMDb Movie Ratings...", ratings.Page, ratings.TotalPages, ratings.Movies.Count));
                response = TraktAPI.TraktAPI.RateMovies(GetRateMoviesData(ratings.Movies));
                if (response == null || response.Status != "success")
                {
                    UIUtils.UpdateStatus("Failed to send ratings for TMDb movies.", true);
                    Thread.Sleep(2000);
                    if (ImportCancelled)
                    {
                        return;
                    }
                }

                // add to list of movies to mark as watched
                watchedMovies.AddRange(ratings.Movies);
            }
            #endregion

            #region Mark As Watched
            if (AppSettings.MarkAsWatched && watchedMovies.Count > 0)
            {
                // mark all movies as watched if rated
                UIUtils.UpdateStatus(string.Format("Importing {0} TMDb Movies as Watched...", watchedMovies.Count));
                TraktMovieSyncResponse watchedResponse = TraktAPI.TraktAPI.SyncMovieLibrary(GetWatchedMoviesData(watchedMovies), TraktSyncModes.seen);
                if (watchedResponse == null || watchedResponse.Status != "success")
                {
                    UIUtils.UpdateStatus("Failed to send watched status for TMDb movies.", true);
                    Thread.Sleep(2000);
                    if (ImportCancelled)
                    {
                        return;
                    }
                }
            }
            #endregion

            return;
        }