Exemple #1
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;
        }
Exemple #2
0
        private TraktEpisode GetTraktEpisodeRateData(Dictionary<string,string> episode, TraktShowSummary showSummary)
        {
            if (showSummary == null || showSummary.Seasons == null || showSummary.Seasons.Count == 0)
                return null;

            string episodeTitle = GetEpisodeName(episode[IMDbFieldMapping.cTitle]);

            // find episode title in list of episodes from show summary
            if (!string.IsNullOrEmpty(episodeTitle))
            {
                TraktShowSummary.TraktSeason.TraktEpisode match = null;
                foreach (var season in showSummary.Seasons)
                {
                    if (match != null) continue;
                    match = season.Episodes.FirstOrDefault(e => string.Equals(e.Title, episodeTitle, StringComparison.InvariantCultureIgnoreCase));
                }

                if (match != null)
                {
                    return new TraktEpisode
                                {
                                    Episode = match.Episode,
                                    Season = match.Season,
                                    TVDbId = showSummary.TVDbId,
                                    Title = showSummary.Title,
                                    Year = showSummary.Year,
                                    Rating = int.Parse(episode[IMDbFieldMapping.cRating])
                                };
                }
            }

            // we can also lookup by airDate
            string episodeAirDate = episode[IMDbFieldMapping.cReleaseDate];

            if (!string.IsNullOrEmpty(episodeAirDate))
            {
                // get epoch date
                long dateTimeEpoch = 0;
                try
                {
                    var splitDate = episodeAirDate.Split('-');
                    // parse date and add 8hours for PST
                    DateTime dateTime = new DateTime(int.Parse(splitDate[0]), int.Parse(splitDate[1]), int.Parse(splitDate[2])).AddHours(8);
                    dateTimeEpoch = dateTime.ToEpoch();
                }
                catch
                {
                    UIUtils.UpdateStatus(string.Format("Unable to get info for {0}", episode[IMDbFieldMapping.cTitle]), true);
                    return null;
                }

                TraktShowSummary.TraktSeason.TraktEpisode match = null;
                foreach (var season in showSummary.Seasons)
                {
                    if (match != null) continue;
                    match = season.Episodes.FirstOrDefault(e => e.FirstAired == dateTimeEpoch);
                }

                if (match != null)
                {
                    return new TraktEpisode
                    {
                        Episode = match.Episode,
                        Season = match.Season,
                        TVDbId = match.TVDbId,
                        Title = showSummary.Title,
                        Year = showSummary.Year,
                        Rating = int.Parse(episode[IMDbFieldMapping.cRating])
                    };
                }
            }

            UIUtils.UpdateStatus(string.Format("Unable to get info for {0}", episode[IMDbFieldMapping.cTitle]), true);
            return null;
        }
Exemple #3
0
        public static TraktEpisodes GetEpisodeData(IEnumerable<Dictionary<string, string>> episodes, bool ratings=true)
        {
            var traktEpisodes = new List<TraktEpisode>();

            foreach (var episode in episodes)
            {
                if (ratings && string.IsNullOrEmpty(episode[IMDbFieldMapping.cRating]))
                    continue;

                // 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));

                    // Note: IMDb ID will be the TV Show IMDb if syncing from Web, otherwise it uses the Episode IMDb ID
                    // The Summary method will not work with the Episode IMDb so fallback to Title slug.

                    // Check the field count to determine if its a Web/CSV episode
                    if (episode[IMDbFieldMapping.cProvider] == "web")
                        slug = episode[IMDbFieldMapping.cIMDbID];

                    showSummary = TraktAPI.TraktAPI.GetShowSummary(slug);
                    if (showSummary == null || showSummary.Seasons == null || showSummary.Seasons.Count == 0)
                    {
                        // trakt may not have the IMDb for the show, try using title slug
                        if (!slug.StartsWith("tt"))
                        {
                            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 = GetTraktEpisodeData(episode, showSummary, ratings);
                if (traktEpisode == null) continue;

                traktEpisodes.Add(traktEpisode);
            }

            var episodeData = new TraktEpisodes
            {
                Username = AppSettings.TraktUsername,
                Password = AppSettings.TraktPassword,
                Episodes = traktEpisodes
            };

            return episodeData;
        }