public static Task<TvShowUpdate> GetUpdate(int id, DateTime? lastUpdate = null, Language language = Language.English) { return Task.Run(() => { var client = new TMDbClient(ApiKey); var episodes = new List<TvShowEpisodeInfo>(); var updateDate = DateTime.Now; var show = client.GetTvShow(id, language: GetLanguage(language)); if (show == null) return null; foreach (var seasonTmp in show.Seasons) { if (seasonTmp.SeasonNumber == 0) continue; var season = client.GetTvSeason(show.Id, seasonTmp.SeasonNumber); if (season == null) continue; foreach (var episode in season.Episodes) { var ad = episode.AirDate; DateTime? airDate; if (ad.Year < 1896) airDate = null; else airDate = new DateTime(ad.Year, ad.Month, ad.Day, 20, 0, 0, DateTimeKind.Utc); episodes.Add(new TvShowEpisodeInfo(season.SeasonNumber, episode.EpisodeNumber, episode.Name, airDate, episode.Overview)); } } return new TvShowUpdate(updateDate, show.InProduction, show.Status, episodes); }); }
/// <summary> /// Match the series information by Episode name /// </summary> private static bool MatchByEpisodeName(TMDbClient client, VideoTags videoTags, TvShow tvShow) { if (String.IsNullOrWhiteSpace(videoTags.SubTitle)) return false; //Nothing to match here // Cycle through all Seasons and Episodes looking for a match for (int sNo = 0; sNo <= tvShow.NumberOfSeasons; sNo++) { TvSeason season = client.GetTvSeason(tvShow.Id, sNo); if (season == null || season.Episodes == null) continue; for (int eNo = 0; eNo <= season.Episodes.Count; eNo++) { TvEpisode episode = client.GetTvEpisode(tvShow.Id, sNo, eNo); if (episode == null) continue; string episodeName = episode.Name; if (!String.IsNullOrWhiteSpace(episodeName)) { if (String.Compare(videoTags.SubTitle.Trim(), episodeName.Trim(), CultureInfo.InvariantCulture, (CompareOptions.IgnoreSymbols | CompareOptions.IgnoreCase)) == 0) // Compare the episode names (case / special characters / whitespace can change very often) { int episodeNo = episode.EpisodeNumber; int seasonNo = season.SeasonNumber; string episodeOverview = episode.Overview; string overview = season.Overview; string tvdbID = (episode.ExternalIds != null ? (episode.ExternalIds.TvdbId != null ? episode.ExternalIds.TvdbId.ToString() : "") : ""); string imdbID = (episode.ExternalIds != null ? episode.ExternalIds.ImdbId : ""); string tmdbID = (tvShow.Id != 0 ? tvShow.Id.ToString() : ""); string network = (tvShow.Networks != null ? String.Join(";", tvShow.Networks.Select(s => s.Name)) : ""); DateTime premiereDate = GlobalDefs.NO_BROADCAST_TIME; if (tvShow.FirstAirDate != null) premiereDate = (DateTime)tvShow.FirstAirDate; List<string> genres = (tvShow.Genres != null ? tvShow.Genres.Select(s => s.Name).ToList() : new List<string>()); DateTime firstAired = (episode.AirDate != null ? episode.AirDate : GlobalDefs.NO_BROADCAST_TIME); string mediaCredits = (tvShow.Credits != null ? ((tvShow.Credits.Cast != null) ? String.Join(";", tvShow.Credits.Cast.Select(s => s.Name)) : "") : ""); client.GetConfig(); // First we need to get the config VideoMetaData.DownloadBannerFile(videoTags, client.GetImageUrl("original", tvShow.PosterPath).OriginalString); // Get bannerfile if ((episodeNo != 0) && (videoTags.Episode == 0)) videoTags.Episode = episodeNo; if ((seasonNo != 0) && (videoTags.Season == 0)) videoTags.Season = seasonNo; if (!String.IsNullOrWhiteSpace(episodeOverview) && String.IsNullOrWhiteSpace(videoTags.Description)) videoTags.Description = episodeOverview; else if (!String.IsNullOrWhiteSpace(overview) && (String.IsNullOrWhiteSpace(videoTags.Description))) videoTags.Description = overview; if (!String.IsNullOrWhiteSpace(tvdbID) && String.IsNullOrWhiteSpace(videoTags.tvdbId)) videoTags.tvdbId = tvdbID; if (!String.IsNullOrWhiteSpace(imdbID) && String.IsNullOrWhiteSpace(videoTags.imdbId)) videoTags.imdbId = imdbID; if (!String.IsNullOrWhiteSpace(tmdbID) && String.IsNullOrWhiteSpace(videoTags.tmdbId)) videoTags.tmdbId = tmdbID; if (!String.IsNullOrWhiteSpace(network) && String.IsNullOrWhiteSpace(videoTags.Network)) videoTags.Network = network; if (!String.IsNullOrWhiteSpace(mediaCredits) && String.IsNullOrWhiteSpace(videoTags.MediaCredits)) videoTags.MediaCredits = mediaCredits; if (premiereDate > GlobalDefs.NO_BROADCAST_TIME) if ((videoTags.SeriesPremiereDate <= GlobalDefs.NO_BROADCAST_TIME) || (videoTags.SeriesPremiereDate.Date > premiereDate.Date)) // Sometimes the metadata from the video recordings are incorrect and report the recorded date (which is more recent than the release date) then use TVDB dates, TVDB Dates are more reliable than video metadata usually videoTags.SeriesPremiereDate = premiereDate; // TVDB stores time in network (local) timezone if (genres.Count > 0) { if (videoTags.Genres != null) { if (videoTags.Genres.Length == 0) videoTags.Genres = genres.ToArray(); } else videoTags.Genres = genres.ToArray(); } if (firstAired > GlobalDefs.NO_BROADCAST_TIME) if ((videoTags.OriginalBroadcastDateTime <= GlobalDefs.NO_BROADCAST_TIME) || (videoTags.OriginalBroadcastDateTime.Date > firstAired.Date)) // Sometimes the metadata from the video recordings are incorrect and report the recorded date (which is more recent than the release date) then use TVDB dates, TVDB Dates are more reliable than video metadata usually videoTags.OriginalBroadcastDateTime = firstAired; // TVDB stores time in network (local) timezone return true; // Found a match got all the data, we're done here } } } } return false; // nothing matches }