private TraktRateShows GetRateShowsData(TVDbShowRatings showRatings) { List<TraktShow> shows = new List<TraktShow>(); shows.AddRange(from show in showRatings.Shows select new TraktShow { TVDbId = show.Id, Rating = show.UserRating }); TraktRateShows showRateData = new TraktRateShows { Username = AppSettings.TraktUsername, Password = AppSettings.TraktPassword, Shows = shows }; return showRateData; }
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("Retreiving existing tv show ratings from trakt.tv."); var currentUserShowRatings = TraktAPI.TraktAPI.GetUserRatedShows(AppSettings.TraktUsername); var filteredShows = new TVDbShowRatings(); filteredShows.Shows.AddRange(showRatings.Shows); if (currentUserShowRatings != null) { UIUtils.UpdateStatus(string.Format("Found {0} user tv show ratings on trakt.tv", currentUserShowRatings.Count())); // Filter out shows to rate from existing ratings online filteredShows.Shows.RemoveAll(s => currentUserShowRatings.Any(c => c.TVDBID == s.Id.ToString())); } UIUtils.UpdateStatus(string.Format("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(string.Format("Importing page {0}/{1} TVDb rated shows...", i + 1, pages)); TraktRatingsResponse response = TraktAPI.TraktAPI.RateShows(GetRateShowsData(filteredShows.Shows.Skip(i * pageSize).Take(pageSize).ToList())); if (response == null || response.Status != "success") { UIUtils.UpdateStatus("Error importing show ratings to trakt.tv.", true); Thread.Sleep(2000); } if (ImportCancelled) return; } } #endregion #region Import Episode Ratings int iCounter = 0; List<TraktEpisode> episodesRated = new List<TraktEpisode>(); // get all existing user ratings from trakt.tv UIUtils.UpdateStatus("Retreiving existing episode ratings from trakt.tv."); var currentUserEpisodeRatings = TraktAPI.TraktAPI.GetUserRatedEpisodes(AppSettings.TraktUsername); 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; if (currentUserEpisodeRatings != null) { UIUtils.UpdateStatus(string.Format("Found {0} user tv episode ratings on trakt.tv", currentUserEpisodeRatings.Count())); // Filter out shows to rate from existing ratings online episodeRatings.Episodes.RemoveAll(e => currentUserEpisodeRatings.Any(c => ((c.EpisodeDetails.TVDBID == e.Id.ToString())))); } 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); var 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; }