private List <GUITrailerListItem> SearchMovieTrailers(MediaItem searchItem) { string searchTerm = string.Empty; var listItems = new List <GUITrailerListItem>(); if (!string.IsNullOrEmpty(searchItem.TMDb)) { searchTerm = searchItem.TMDb; } else if (!string.IsNullOrEmpty((searchItem.IMDb ?? string.Empty).Trim())) { var externalId = PluginSettings.IMDbIds.FirstOrDefault(t => t.ExternalId == searchItem.IMDb); if (externalId == null) { // use the find method to lookup by external source id FileLog.Debug("Searching themoviedb.org for all objects with IMDb ID '{0}'", searchItem.IMDb); var findResults = TMDbAPI.TMDbFind(searchItem.IMDb, TMDbAPI.ExternalSource.imdb_id); if (findResults == null || findResults.Movies == null || findResults.Movies.Count == 0) { FileLog.Warning("Not enough information to search for trailers from themoviedb.org, no matches found using IMDb ID"); return(listItems); } // there should only be one searchTerm = findResults.Movies.First().Id.ToString(); // cache id so we can use it again PluginSettings.IMDbIds.Add(new PluginSettings.ExternalID { ExternalId = searchItem.IMDb, TmdbId = findResults.Movies.First().Id }); } else { FileLog.Debug("Found cached TMDb ID '{0}' for IMDb External ID: '{1}'", externalId.TmdbId.ToString(), searchItem.IMDb); searchTerm = externalId.TmdbId.ToString(); } } else if (!string.IsNullOrEmpty(searchItem.Title)) { // we do the best we can with out any proper movie id's FileLog.Debug("Searching themoviedb.org for movie: Title: '{0}', Year: '{1}'", searchItem.Title, searchItem.Year); var searchResults = TMDbAPI.SearchMovies(searchItem.Title, language: "en", year: searchItem.Year <= 1900 ? null : searchItem.Year.ToString()); if (searchResults == null || searchResults.TotalResults == 0) { FileLog.Warning("No movies found, skipping search from the themoviedb.org."); return(listItems); } else { foreach (var movie in searchResults.Results) { FileLog.Debug("Found movie: Title: '{0}', Original Title: '{1}', Release Date: '{2}', TMDb: '{3}', Popularity: '{4}'", movie.Title, movie.OriginalTitle, movie.ReleaseDate, movie.Id, movie.Popularity); } } // get the movie id of the first result, this would be the most likely match (based on popularity) // we can think about providing a menu of choices as well based on demand searchTerm = searchResults.Results.First().Id.ToString(); searchItem.TMDb = searchTerm; } else { FileLog.Warning("Not enough information to search for trailers from themoviedb.org, require IMDb ID, TMDb ID or Title+Year."); return(listItems); } FileLog.Debug("Searching for movie trailers using search term '{0}' from themoviedb.org...", searchTerm); var trailers = GetMovieTrailersFromCache(searchTerm); if (trailers == null || trailers.Results == null) { FileLog.Error("Error getting movie trailers from themoviedb.org."); return(listItems); } foreach (var trailer in trailers.Results) { var listItem = new GUITrailerListItem(); string itemName = string.IsNullOrEmpty(trailer.Type) || trailer.Name.ToLowerInvariant().Contains(trailer.Type.ToLowerInvariant()) ? trailer.Name : string.Format("{0} - {1}", trailer.Name, trailer.Type); itemName = string.IsNullOrEmpty(trailer.Size) || itemName.ToLowerInvariant().Contains(trailer.Size.ToLowerInvariant()) ? itemName : string.Format("{0} ({1})", itemName, trailer.Size); if (PluginSettings.PreferredLanguage != "en") { itemName = string.Format("{0} [{1}]", itemName, trailer.LanguageCode); } listItem.Label = itemName; listItem.Label2 = Localisation.Translation.Online; listItem.URL = WebUtils.GetYouTubeURL(trailer.Key); listItem.TVTag = trailer; listItem.IsOnlineItem = true; listItem.CurrentMedia = searchItem; listItems.Add(listItem); } FileLog.Info("Found {0} movie trailer(s) from themoviedb.org", trailers.Results.Count.ToString()); return(listItems); }
public static string GetMovieSearchTerm(string imdbid, string tmdbid, string title, string year) { string searchTerm = null; if (!string.IsNullOrEmpty(tmdbid)) { searchTerm = tmdbid; } else if (!string.IsNullOrEmpty((imdbid ?? string.Empty).Trim())) { var externalId = PluginSettings.IMDbIds.FirstOrDefault(t => t.ExternalId == imdbid); if (externalId == null) { // use the find method to lookup by external source id FileLog.Debug("Searching themoviedb.org for all objects with IMDb ID '{0}'", imdbid); var findResults = TMDbAPI.TMDbFind(imdbid, TMDbAPI.ExternalSource.imdb_id); if (findResults == null || findResults.Movies == null || findResults.Movies.Count == 0) { FileLog.Warning("Not enough information to search for trailers from themoviedb.org, no matches found using IMDb ID"); return(searchTerm); } // there should only be one searchTerm = findResults.Movies.First().Id.ToString(); // cache id so we can use it again PluginSettings.IMDbIds.Add(new PluginSettings.ExternalID { ExternalId = imdbid, TmdbId = findResults.Movies.First().Id }); } else { FileLog.Debug("Found cached TMDb ID '{0}' for IMDb External ID: '{1}'", externalId.TmdbId.ToString(), imdbid); searchTerm = externalId.TmdbId.ToString(); } } else if (!string.IsNullOrEmpty(title)) { // we do the best we can with out any proper movie id's FileLog.Debug("Searching themoviedb.org for movie: Title: '{0}', Year: '{1}'", title, year); int iYear = 0; int.TryParse(year, out iYear); var searchResults = TMDbAPI.SearchMovies(title, language: "en", year: iYear <= 1900 ? null : year); if (searchResults == null || searchResults.TotalResults == 0) { FileLog.Warning("No movies found, skipping search from the themoviedb.org."); return(searchTerm); } else { foreach (var movie in searchResults.Results) { FileLog.Debug("Found movie: Title: '{0}', Original Title: '{1}', Release Date: '{2}', TMDb: '{3}', Popularity: '{4}'", movie.Title, movie.OriginalTitle, movie.ReleaseDate, movie.Id, movie.Popularity); } } // get the movie id of the first result, this would be the most likely match (based on popularity) // we can think about providing a menu of choices as well based on demand searchTerm = searchResults.Results.First().Id.ToString(); } else { FileLog.Warning("Not enough information to search for trailers from themoviedb.org, require IMDb ID, TMDb ID or Title+Year."); } return(searchTerm); }