Example #1
0
        private TraktRateEpisodes GetRateEpisodeData(TVDbEpisodeRatings episodeRatings, TVDbShow showInfo)
        {
            List <TraktEpisode> episodes = new List <TraktEpisode>();

            foreach (var episode in episodeRatings.Episodes)
            {
                // get season/episode info from showInfo object
                var tvdbEpisode = showInfo.Episodes.Find(e => e.Id == episode.Id);
                if (tvdbEpisode == null)
                {
                    continue;
                }

                var traktEpisode = new TraktEpisode
                {
                    Episode = tvdbEpisode.EpisodeNumber,
                    Season  = tvdbEpisode.SeasonNumber,
                    TVDbId  = episodeRatings.Show.Id,
                    Rating  = episode.UserRating,
                    Title   = showInfo.Show.Name
                };
                episodes.Add(traktEpisode);
            }

            TraktRateEpisodes episodeRateData = new TraktRateEpisodes
            {
                Username = AppSettings.TraktUsername,
                Password = AppSettings.TraktPassword,
                Episodes = episodes,
            };

            return(episodeRateData);
        }
Example #2
0
        private TraktRateEpisodes GetRateEpisodeData(IEnumerable <Dictionary <string, string> > episodes)
        {
            var traktEpisodes = new List <TraktEpisode>();

            foreach (var episode in episodes)
            {
                // get the show information
                string showTitle = GetShowName(episode[IMDbFieldMapping.cTitle]);
                if (string.IsNullOrEmpty(showTitle))
                {
                    continue;
                }

                // get slug of show title
                string slug = showTitle.GenerateSlug();
                if (string.IsNullOrEmpty(slug))
                {
                    continue;
                }

                TraktShowSummary showSummary = new TraktShowSummary();

                if (!ShowSummaries.TryGetValue(showTitle, out showSummary))
                {
                    // get from online
                    UIUtils.UpdateStatus(string.Format("Retrieving data for {0}", showTitle));
                    showSummary = TraktAPI.TraktAPI.GetShowSummary(slug);
                    if (showSummary == null || showSummary.Seasons == null || showSummary.Seasons.Count == 0)
                    {
                        UIUtils.UpdateStatus(string.Format("Unable to get info for {0}", showTitle), true);
                        continue;
                    }

                    // store show summary
                    ShowSummaries.Add(showTitle, showSummary);
                }

                var traktEpisode = GetTraktEpisodeRateData(episode, showSummary);
                if (traktEpisode == null)
                {
                    continue;
                }

                traktEpisodes.Add(traktEpisode);
            }

            var episodeRateData = new TraktRateEpisodes
            {
                Username = AppSettings.TraktUsername,
                Password = AppSettings.TraktPassword,
                Episodes = traktEpisodes
            };

            return(episodeRateData);
        }
Example #3
0
        /// <summary>
        /// Rates a list of episodes on trakt
        /// </summary>
        /// <param name="data">The object containing the list of episodes to be rated</param>
        /// <returns>The response from trakt</returns>
        public static TraktRatingsResponse RateEpisodes(TraktRateEpisodes data)
        {
            // check that we have everything we need
            if (data == null || data.Episodes.Count == 0)
            {
                return(null);
            }

            // serialize data to JSON and send to server
            string response = TraktWeb.Transmit(TraktURIs.RateEpisodes, data.ToJSON());

            // return success or failure
            return(response.FromJSON <TraktRatingsResponse>());
        }
Example #4
0
        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;
        }