private TMDbTrailers GetMovieTrailersFromCache(string movieId)
        {
            string       key      = string.Format("{0}_{1}_{2}_{3}", movieId, PluginSettings.PreferredLanguage, PluginSettings.FallbackToEnglishLanguage, PluginSettings.AlwaysGetEnglishTrailers);
            TMDbTrailers trailers = null;

            // check if we have a cached request
            TrailerCache.TryGetValue(key, out trailers);

            // check if web request cache has expired and make a new request
            if (trailers == null || LastWebRequest < DateTime.UtcNow.Subtract(new TimeSpan(0, PluginSettings.WebRequestCacheMinutes, 0)))
            {
                // check if we have already cached the request
                trailers = TMDbAPI.GetMovieTrailers(movieId, PluginSettings.PreferredLanguage, PluginSettings.FallbackToEnglishLanguage, PluginSettings.AlwaysGetEnglishTrailers);

                // remove from cache if already exists
                if (TrailerCache.ContainsKey(key))
                {
                    TrailerCache.Remove(key);
                }

                // add to cache
                TrailerCache.Add(key, trailers);
                LastWebRequest = DateTime.UtcNow;
            }

            return(trailers);
        }
Exemple #2
0
        internal static void ProcessAndDownloadTrailers(MovieTrailers cache, MoviePluginSource pluginSource)
        {
            // get a list of movies from the cache that require trailer searches i.e.
            // where trailer count equals zero and update interval is greater than (now - lastupdate)
            // typically this will be for any new movies added to library
            int onlineOps     = 0;
            int saveThreshold = 10;

            var moviesWithoutTrailers = cache.Movies.Where(m => m.Trailers.Count == 0);

            foreach (var movie in moviesWithoutTrailers)
            {
                // check the date last processed is longer than max period for update (default 7 days)
                DateTime lastUpdate = DateTime.MinValue;
                DateTime.TryParse(movie.UpdateTime, out lastUpdate);
                if (DateTime.Now.Subtract(new TimeSpan(PluginSettings.AutoDownloadUpdateInterval, 0, 0, 0)) < lastUpdate)
                {
                    continue;
                }

                // update the last update time
                movie.UpdateTime = DateTime.Now.ToString();

                // get the search term for trailer search
                // if there is no 'id' we will get one from a movie lookup
                FileLog.Info("Searching for trailers from themoviedb.org, Title: '{0}', Year: '{1}', IMDb: '{2}', TMDb: '{3}'", movie.Title, movie.Year, movie.IMDbID ?? "<empty>", movie.TMDbID ?? "<empty>");

                string searchTerm = TMDbTrailerProvider.GetMovieSearchTerm(movie.IMDbID, movie.TMDbID, movie.Title, movie.Year);
                if (searchTerm == null)
                {
                    continue;
                }

                // search for trailers
                var trailers = TMDbAPI.GetMovieTrailers(searchTerm, PluginSettings.PreferredLanguage, PluginSettings.FallbackToEnglishLanguage, PluginSettings.AlwaysGetEnglishTrailers);
                if (trailers == null || trailers.Results == null || trailers.Results.Count == 0)
                {
                    continue;
                }

                // save the list of trailers to the cached movie
                var trailersFound = new List <Trailer>();
                foreach (var trailer in trailers.Results)
                {
                    FileLog.Info("Found Youtube video, Name: '{0}', Quality: '{1}', Source: '{2}', Type: '{3}'", trailer.Name, trailer.Size, trailer.Key, trailer.Type);

                    trailersFound.Add(new Trailer
                    {
                        Name     = trailer.Name,
                        Quality  = trailer.Size,
                        Source   = trailer.Key,
                        Type     = trailer.Type,
                        Language = trailer.LanguageCode,
                        IsValid  = true
                    });

                    onlineOps++;
                }

                // persist trailers found and update time
                movie.Trailers = trailersFound;

                // save cache semi-regularly in case we shutdown/enter stand-by
                if (onlineOps >= saveThreshold)
                {
                    TrailerDownloader.SaveMovieList(pluginSource, cache);
                    saveThreshold += 10;
                }
            }

            // now download trailers that have not been processed yet i.e. don't have a physical file path and are valid
            moviesWithoutTrailers = cache.Movies.Where(m => m.Trailers.Count(t => string.IsNullOrEmpty(t.Path) && t.IsValid) > 0);

            onlineOps     = 0;
            saveThreshold = 10;
            foreach (var movie in moviesWithoutTrailers)
            {
                FileLog.Info("Checking for trailer downloads, Title: {0}, Year: {1}, IMDb: {2}, TMDb: {3}", movie.Title, movie.Year, movie.IMDbID ?? "<empty>", movie.TMDbID ?? "<empty>");

                // only process valid trailers
                foreach (var trailer in movie.Trailers.Where(t => t.IsValid))
                {
                    // if the file already exists skip
                    if (!string.IsNullOrEmpty(trailer.Path))
                    {
                        if (File.Exists(trailer.Path))
                        {
                            FileLog.Info("Skipping trailer '{0}' download for '{1}', file already exists", trailer.Name, trailer.Path);
                            continue;
                        }
                    }

                    // check user wants video
                    if (!CheckAllowedTrailerTypes(trailer))
                    {
                        FileLog.Info("Skipping trailer download for, Name='{0}', Type='{1}', Reason='Unwanted Type'", trailer.Name, trailer.Type);
                        continue;
                    }

                    FileLog.Info("Getting download options for video, Name: {0}, Quality: {1}, Source: {2}, Type: {3}", trailer.Name, trailer.Quality, trailer.Source, trailer.Type);
                    var options = OnlineVideosHandler.GetPlaybackOptionsFromYoutubeUrl(trailer.Source);
                    if (options == null)
                    {
                        FileLog.Warning("Unable to get download options for trailer, marking as invalid");
                        trailer.IsValid = false;
                        continue;
                    }

                    // get the preferred (best) video codec and type
                    var downloadDetails = TrailerDownloader.GetDownloadDetails(movie, trailer, options);
                    if (downloadDetails == null)
                    {
                        FileLog.Warning("Could not find any matching resolution or lower for download, skipping trailer download");
                        continue;
                    }

                    // download trailer
                    trailer.IsValid = WebUtils.DownloadFile(downloadDetails.SourceUrl, downloadDetails.DestinationFilename);
                    trailer.Path    = downloadDetails.DestinationFilename;

                    onlineOps++;
                }

                // save cache semi-regularly in case we shutdown/enter stand-by
                if (onlineOps >= saveThreshold)
                {
                    TrailerDownloader.SaveMovieList(pluginSource, cache);
                    saveThreshold += 10;
                }
            }

            // save cache
            TrailerDownloader.SaveMovieList(pluginSource, cache);
        }