/// <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); } }
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 { Created = episode[IMDbFieldMapping.cProvider].IsCSVExport() ? episode[IMDbFieldMapping.cCreated] : null, EpisodeName = tvEpisodeName, EpisodeNumber = tvdbEpisodeInfo.EpisodeNumber, ImdbId = episode[IMDbFieldMapping.cProvider].IsCSVExport() ? episode[IMDbFieldMapping.cIMDbID] : null, SeasonNumber = tvdbEpisodeInfo.SeasonNumber, ShowName = tvShowName, TvdbId = tvdbEpisodeInfo.Id }; 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; } }