Пример #1
0
        public void ImportRatings()
        {
            importCancelled = false;

            // get show userratings from theTVDb.com first
            UIUtils.UpdateStatus("Getting show ratings from theTVDb.com");

            TVDbShowRatings showRatings = TVDbAPI.GetShowRatings(accountId);

            // if there are no show ratings quit
            if (showRatings == null || showRatings.Shows.Count == 0)
            {
                UIUtils.UpdateStatus("Unable to get list of shows from thetvdb.com, NOTE: episode ratings can not be retreived from theTVDb.com unless the Show has also been rated!", true);
                return;
            }

            #region Import Show Ratings
            if (importCancelled)
            {
                return;
            }
            UIUtils.UpdateStatus("Retrieving existing tv show ratings from trakt.tv");
            var currentUserShowRatings = TraktAPI.GetRatedShows();

            var filteredShows = new TVDbShowRatings();
            filteredShows.Shows.AddRange(showRatings.Shows);

            if (currentUserShowRatings != null)
            {
                UIUtils.UpdateStatus("Found {0} user tv show ratings on trakt.tv", currentUserShowRatings.Count());
                UIUtils.UpdateStatus("Filtering out tvdb show ratings that already exist at trakt.tv");

                // Filter out shows to rate from existing ratings online
                filteredShows.Shows.RemoveAll(s => currentUserShowRatings.Any(c => c.Show.Ids.TvdbId == s.Id));
            }

            UIUtils.UpdateStatus("Importing {0} show ratings to trakt.tv", filteredShows.Shows.Count);

            if (filteredShows.Shows.Count > 0)
            {
                int pageSize = AppSettings.BatchSize;
                int pages    = (int)Math.Ceiling((double)filteredShows.Shows.Count / pageSize);
                for (int i = 0; i < pages; i++)
                {
                    UIUtils.UpdateStatus("Importing page {0}/{1} TVDb rated shows...", i + 1, pages);

                    TraktSyncResponse response = TraktAPI.AddShowsToRatings(GetRateShowsData(filteredShows.Shows.Skip(i * pageSize).Take(pageSize).ToList()));
                    if (response == null)
                    {
                        UIUtils.UpdateStatus("Error importing show ratings to trakt.tv", true);
                        Thread.Sleep(2000);
                    }
                    else if (response.NotFound.Shows.Count > 0)
                    {
                        UIUtils.UpdateStatus("Unable to sync ratings of {0} shows as they're not found on trakt.tv!", response.NotFound.Shows.Count);
                        Thread.Sleep(1000);
                    }
                    if (importCancelled)
                    {
                        return;
                    }
                }
            }
            #endregion

            #region Import Episode Ratings
            int iCounter      = 0;
            var episodesRated = new Dictionary <string, List <TraktEpisodeRating> >();

            // get all existing user ratings from trakt.tv
            UIUtils.UpdateStatus("Retrieving existing episode ratings from trakt.tv");
            var currentUserEpisodeRatings = TraktAPI.GetRatedEpisodes();

            if (currentUserEpisodeRatings != null)
            {
                UIUtils.UpdateStatus("Found {0} user tv episode ratings on trakt.tv", currentUserEpisodeRatings.Count());
            }

            foreach (var show in showRatings.Shows)
            {
                if (importCancelled)
                {
                    return;
                }
                iCounter++;

                UIUtils.UpdateStatus("[{0}/{1}] Getting show info for tvdb series id {2}", iCounter, showRatings.Shows.Count, show.Id);

                // we need to get the episode/season numbers as trakt api requires this
                // tvdb only returns episode ids, so user series info call to this info
                TVDbShow showInfo = TVDbAPI.GetShowInfo(show.Id.ToString());
                if (showInfo == null)
                {
                    UIUtils.UpdateStatus(string.Format("Unable to get show info for tvdb series id: {0}", show.Id), true);
                    Thread.Sleep(2000);
                    continue;
                }
                if (importCancelled)
                {
                    return;
                }
                UIUtils.UpdateStatus("[{0}/{1}] Requesting episode ratings for {2} from theTVDb.com", iCounter, showRatings.Shows.Count, showInfo.Show.Name);

                // get episode ratings for each show in showratings
                TVDbEpisodeRatings episodeRatings = TVDbAPI.GetEpisodeRatings(accountId, show.Id.ToString());
                if (episodeRatings == null)
                {
                    UIUtils.UpdateStatus(string.Format("Unable to get episode ratings for {0} [{1}] from theTVDb.com", showInfo.Show.Name, show.Id), true);
                    Thread.Sleep(2000);
                    continue;
                }
                if (importCancelled)
                {
                    return;
                }

                UIUtils.UpdateStatus("Found {0} episode ratings for {1} on theTVDb.com", episodeRatings.Episodes.Count, showInfo.Show.Name);

                if (currentUserEpisodeRatings != null)
                {
                    UIUtils.UpdateStatus("Filtering out {0} tvdb episode ratings that already exist at trakt.tv", showInfo.Show.Name);

                    // Filter out episodes to rate from existing ratings online, using tvdb episode id's
                    episodeRatings.Episodes.RemoveAll(e => currentUserEpisodeRatings.Any(c => ((c.Episode.Ids.TvdbId == e.Id))));
                }

                UIUtils.UpdateStatus("[{0}/{1}] Importing {2} episode ratings for {3}", iCounter, showRatings.Shows.Count, episodeRatings.Episodes.Count, showInfo.Show.Name);
                if (episodeRatings.Episodes.Count == 0)
                {
                    continue;
                }

                // submit one series at a time
                var episodesToRate = GetRateEpisodeData(episodeRatings);
                var response       = TraktAPI.AddsEpisodesToRatings(episodesToRate);
                if (response == null)
                {
                    UIUtils.UpdateStatus(string.Format("Error importing {0} episode ratings to trakt.tv", showInfo.Show.Name), true);
                    Thread.Sleep(2000);
                    continue;
                }
                else if (response.NotFound.Episodes.Count > 0)
                {
                    UIUtils.UpdateStatus("[{0}/{1}] Unable to sync ratings for {2} episodes of {3} as they're not found on trakt.tv!", iCounter, showRatings.Shows.Count, response.NotFound.Episodes.Count, showInfo.Show.Name);
                    Thread.Sleep(1000);
                }
                episodesRated.Add(showInfo.Show.Name, episodesToRate.Episodes);
            }
            #endregion

            #region Mark As Watched

            if (AppSettings.MarkAsWatched && episodesRated.Any())
            {
                int i = 0;
                foreach (var show in episodesRated)
                {
                    if (importCancelled)
                    {
                        return;
                    }

                    // mark all episodes as watched if rated
                    UIUtils.UpdateStatus("[{0}/{1}] Importing {2} TVDb episodes of {3} as watched to trakt.tv...", ++i, episodesRated.Count, show.Value.Count, show.Key);
                    var watchedEpisodes = GetWatchedEpisodeData(show.Value);
                    var response        = TraktAPI.AddEpisodesToWatchedHistory(watchedEpisodes);
                    if (response == null)
                    {
                        UIUtils.UpdateStatus(string.Format("Failed to send watched status for TVDb '{0}' episodes", show.Key), true);
                        Thread.Sleep(2000);
                    }
                    else if (response.NotFound.Episodes.Count > 0)
                    {
                        UIUtils.UpdateStatus("[{0}/{1}] Unable to sync {2} TVDb episodes of {3} as watched as they're not found on trakt.tv!", i, episodesRated.Count, response.NotFound.Episodes.Count, show.Key);
                        Thread.Sleep(1000);
                    }
                }
            }

            #endregion
        }
Пример #2
0
        public void ImportRatings()
        {
            ImportCancelled = false;

            // get show userratings from theTVDb.com first
            UIUtils.UpdateStatus("Getting show ratings from theTVDb.com.");

            TVDbShowRatings showRatings = TVDbAPI.GetShowRatings(AccountId);

            // if there are no show ratings quit
            if (showRatings == null || showRatings.Shows.Count == 0)
            {
                UIUtils.UpdateStatus("Unable to get list of shows from thetvdb.com.", true);
                return;
            }

            #region Import Show Ratings
            if (ImportCancelled)
            {
                return;
            }
            UIUtils.UpdateStatus(string.Format("Importing {0} show ratings to trakt.tv.", showRatings.Shows.Count));

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

            #region Import Episode Ratings
            int iCounter = 0;
            List <TraktEpisode> episodesRated = new List <TraktEpisode>();

            foreach (var show in showRatings.Shows)
            {
                if (ImportCancelled)
                {
                    return;
                }
                iCounter++;

                UIUtils.UpdateStatus(string.Format("[{0}/{1}] Getting show info for series id {2}", iCounter, showRatings.Shows.Count, show.Id));

                // we need to get the episode/season numbers as trakt api requires this
                // tvdb only returns episode ids, so user series info call to this info
                TVDbShow showInfo = TVDbAPI.GetShowInfo(show.Id.ToString());
                if (showInfo == null)
                {
                    UIUtils.UpdateStatus(string.Format("Unable to show info for series id: {0}", show.Id), true);
                    Thread.Sleep(2000);
                    continue;
                }
                if (ImportCancelled)
                {
                    return;
                }
                UIUtils.UpdateStatus(string.Format("[{0}/{1}] Requesting episode ratings for {2}", iCounter, showRatings.Shows.Count, showInfo.Show.Name));

                // get episode ratings for each show in showratings
                TVDbEpisodeRatings episodeRatings = TVDbAPI.GetEpisodeRatings(AccountId, show.Id.ToString());
                if (episodeRatings == null)
                {
                    UIUtils.UpdateStatus(string.Format("Unable to get episode ratings for {0}", showInfo.Show.Name), true);
                    Thread.Sleep(2000);
                    continue;
                }
                if (ImportCancelled)
                {
                    return;
                }
                UIUtils.UpdateStatus(string.Format("[{0}/{1}] Importing {2} episode ratings for {3}", iCounter, showRatings.Shows.Count, episodeRatings.Episodes.Count, showInfo.Show.Name));
                if (episodeRatings.Episodes.Count == 0)
                {
                    continue;
                }

                // submit one series at a time
                var episodesToRate = GetRateEpisodeData(episodeRatings, showInfo);
                response = TraktAPI.TraktAPI.RateEpisodes(episodesToRate);
                if (response == null || response.Status != "success")
                {
                    UIUtils.UpdateStatus(string.Format("Error importing {0} episode ratings to trakt.tv.", showInfo.Show.Name), true);
                    Thread.Sleep(2000);
                    continue;
                }
                episodesRated.AddRange(episodesToRate.Episodes);
            }
            #endregion

            #region Mark As Watched

            if (AppSettings.MarkAsWatched && episodesRated.Count() > 0)
            {
                // mark all episodes as watched if rated
                UIUtils.UpdateStatus(string.Format("Importing {0} TVDb Episodes as Watched...", episodesRated.Count()));
                var watchedEpisodes = GetWatchedEpisodeData(episodesRated);
                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 TVDb '{0}' episodes.", showSyncData.Title), true);
                        Thread.Sleep(2000);
                        continue;
                    }
                }
            }

            #endregion

            return;
        }
Пример #3
0
        /// <summary>
        /// TODO: Remove episode lookup from theTVDb.com and search from trakt.tv instead now that API methods exist
        /// </summary>
        public static IMDbEpisode GetIMDbEpisodeFromTVDb(Dictionary <string, string> episode)
        {
            try
            {
                string tvEpisodeName = GetEpisodeName(episode[IMDbFieldMapping.cTitle]);
                string tvShowName    = GetShowName(episode[IMDbFieldMapping.cTitle]);
                string tvShowYear    = episode[IMDbFieldMapping.cYear];
                string tvShowImdbId  = episode[IMDbFieldMapping.cIMDbID];

                // search for the show
                UIUtils.UpdateStatus("Searching for tv show {0} on thetvdb.com", tvShowName);
                var searchResults = TVDbAPI.SearchShow(tvShowName);
                if (searchResults == null)
                {
                    UIUtils.UpdateStatus(string.Format("Failed to search for tv show {0} from thetvdb.com", tvShowName), true);
                    Thread.Sleep(2000);
                    return(null);
                }

                // get the first match that contains the same 'year'
                // only if we're using a csv export file
                var tvdbShowSearchResult = new TVDbShowSearch.Series();
                if (episode[IMDbFieldMapping.cProvider].IsCSVExport())
                {
                    tvdbShowSearchResult = searchResults.Shows.Find(s => s.FirstAired != null && s.FirstAired.Contains(tvShowYear));
                    if (tvdbShowSearchResult == null)
                    {
                        UIUtils.UpdateStatus(string.Format("Failed to search for tv show {0} ({1}) from thetvdb.com", tvShowName, tvShowYear), true);
                        Thread.Sleep(2000);
                        return(null);
                    }
                }
                else
                {
                    // the website populates the 'year' with the episode year (not show)
                    // so we can't use that for a show match.
                    // However, the website populates the IMDb using the IMDb ID of the show (not episode).
                    tvdbShowSearchResult = searchResults.Shows.Find(s => s.ImdbId != null && s.ImdbId == tvShowImdbId);
                    if (tvdbShowSearchResult == null)
                    {
                        UIUtils.UpdateStatus(string.Format("Failed to search for tv show {0} (imdb_id:{1}) from thetvdb.com", tvShowName, tvShowImdbId ?? "<empty>"), true);
                        Thread.Sleep(2000);
                        return(null);
                    }
                }

                // get the show info for the given show
                UIUtils.UpdateStatus(string.Format("Getting tv show info for {0} [tvdb_id:{1}] on thetvdb.com", tvShowName, tvdbShowSearchResult.Id));
                var tvdbShowInfo = TVDbAPI.GetShowInfo(tvdbShowSearchResult.Id.ToString());
                if (tvdbShowInfo == null)
                {
                    UIUtils.UpdateStatus(string.Format("Failed to get show info for tv show {0} [tvdb_id:{1}] from thetvdb.com", tvShowName, tvdbShowSearchResult.Id), true);
                    Thread.Sleep(2000);
                    return(null);
                }

                // we now have a list of episodes from thetvdb.com, we can use the IMDb Episode Title to lookup a tvdb ID
                var tvdbEpisodeInfo = tvdbShowInfo.Episodes.Find(e => e.Name.ToLowerInvariant() == tvEpisodeName.ToLowerInvariant());
                if (tvdbEpisodeInfo == null)
                {
                    // we can also lookup by airDate if using a csv export file
                    if (episode[IMDbFieldMapping.cProvider].IsCSVExport())
                    {
                        string episodeAirDate = null;
                        episode.TryGetValue(IMDbFieldMapping.cReleaseDate, out episodeAirDate);
                        if (!string.IsNullOrEmpty(episodeAirDate))
                        {
                            tvdbEpisodeInfo = tvdbShowInfo.Episodes.Find(e => e.AirDate == episodeAirDate);
                        }

                        // still no luck?
                        if (tvdbEpisodeInfo == null)
                        {
                            UIUtils.UpdateStatus(string.Format("Failed to get episode info for tv show {0} [tvdb_id:{1}] - {2} [AirDate:{3}] from thetvdb.com", tvShowName, tvdbShowSearchResult.Id, tvEpisodeName, episodeAirDate ?? "<empty>"), true);
                            Thread.Sleep(2000);
                            return(null);
                        }
                    }
                    else
                    {
                        if (tvdbEpisodeInfo == null)
                        {
                            UIUtils.UpdateStatus(string.Format("Failed to get episode info for tv show {0} [tvdb_id:{1}] - {2} from thetvdb.com", tvShowName, tvdbShowSearchResult.Id, tvEpisodeName), true);
                            Thread.Sleep(2000);
                            return(null);
                        }
                    }
                }

                // Note: Web Parsing does not use the IMDb ID for the episode, only the show.
                //       we're also not setting the created date from the webrequest.
                var imdbEpisode = new IMDbEpisode
                {
                    EpisodeName   = tvEpisodeName,
                    EpisodeNumber = tvdbEpisodeInfo.EpisodeNumber,
                    ImdbId        = episode[IMDbFieldMapping.cProvider].IsCSVExport() ? episode[IMDbFieldMapping.cIMDbID] : null,
                    SeasonNumber  = tvdbEpisodeInfo.SeasonNumber,
                    ShowName      = tvShowName,
                    TvdbId        = tvdbEpisodeInfo.Id
                };

                // we will convert this to the correct date format later
                if (episode.ContainsKey(IMDbFieldMapping.cCreated))
                {
                    imdbEpisode.Created = episode[IMDbFieldMapping.cCreated];
                }
                if (episode.ContainsKey(IMDbFieldMapping.cAdded))
                {
                    imdbEpisode.Created = episode[IMDbFieldMapping.cAdded];
                }

                if (episode.ContainsKey(IMDbFieldMapping.cRating))
                {
                    imdbEpisode.Rating = string.IsNullOrEmpty(episode[IMDbFieldMapping.cRating]) ? 0 : int.Parse(episode[IMDbFieldMapping.cRating]);
                }

                // return the episode
                return(imdbEpisode);
            }
            catch (Exception e)
            {
                UIUtils.UpdateStatus(string.Format("Failed to get episode info for '{0}' from thetvdb.com, Reason: '{1}'", episode[IMDbFieldMapping.cTitle], e.Message), true);
                Thread.Sleep(2000);
                return(null);
            }
        }