From 50891e5dd7fd6d3b8f66d7a7021ffbda430b8940 Mon Sep 17 00:00:00 2001 From: Devin Buhl Date: Thu, 16 Feb 2017 04:46:35 -0500 Subject: [PATCH] TMDb Filtering Options: Rating, Min Votes, Min Vote Ave, Original Language, TMDb Genre Ids (CSV), (#765) --- .../NetImport/TMDb/TMDbParser.cs | 34 ++++------ .../NetImport/TMDb/TMDbRequestGenerator.cs | 64 ++++++++++++------- .../NetImport/TMDb/TMDbSettings.cs | 38 ++++++----- 3 files changed, 77 insertions(+), 59 deletions(-) diff --git a/src/NzbDrone.Core/NetImport/TMDb/TMDbParser.cs b/src/NzbDrone.Core/NetImport/TMDb/TMDbParser.cs index 2f8946591..1f0589c82 100644 --- a/src/NzbDrone.Core/NetImport/TMDb/TMDbParser.cs +++ b/src/NzbDrone.Core/NetImport/TMDb/TMDbParser.cs @@ -31,7 +31,7 @@ namespace NzbDrone.Core.NetImport.TMDb return movies; } - if (_settings.ListType != (int) TMDbListType.List) + if (_settings.ListType != (int)TMDbListType.List) { var jsonResponse = JsonConvert.DeserializeObject(_importResponse.Content); @@ -43,16 +43,13 @@ namespace NzbDrone.Core.NetImport.TMDb foreach (var movie in jsonResponse.results) { - if (movie.vote_average >= double.Parse(_settings.MinVoteAverage)) + movies.AddIfNotNull(new Tv.Movie() { - movies.AddIfNotNull(new Tv.Movie() - { - Title = movie.title, - TmdbId = movie.id, - ImdbId = null, - Year = DateTime.Parse(movie.release_date).Year - }); - } + Title = movie.title, + TmdbId = movie.id, + ImdbId = null, + Year = DateTime.Parse(movie.release_date).Year + }); } } else @@ -73,16 +70,13 @@ namespace NzbDrone.Core.NetImport.TMDb continue; } - if (movie.vote_average >= double.Parse(_settings.MinVoteAverage)) + movies.AddIfNotNull(new Tv.Movie() { - movies.AddIfNotNull(new Tv.Movie() - { - Title = movie.title, - TmdbId = movie.id, - ImdbId = null, - Year = DateTime.Parse(movie.release_date).Year - }); - } + Title = movie.title, + TmdbId = movie.id, + ImdbId = null, + Year = DateTime.Parse(movie.release_date).Year + }); } } @@ -107,4 +101,4 @@ namespace NzbDrone.Core.NetImport.TMDb } } -} +} \ No newline at end of file diff --git a/src/NzbDrone.Core/NetImport/TMDb/TMDbRequestGenerator.cs b/src/NzbDrone.Core/NetImport/TMDb/TMDbRequestGenerator.cs index ceb491cd9..63f5d4ad4 100644 --- a/src/NzbDrone.Core/NetImport/TMDb/TMDbRequestGenerator.cs +++ b/src/NzbDrone.Core/NetImport/TMDb/TMDbRequestGenerator.cs @@ -2,6 +2,7 @@ using NzbDrone.Common.Http; using System.Collections.Generic; using NLog; +using NzbDrone.Common.Extensions; using NzbDrone.Common.Serializer; using NzbDrone.Core.MetadataSource.SkyHook.Resource; @@ -13,50 +14,67 @@ namespace NzbDrone.Core.NetImport.TMDb public IHttpClient HttpClient { get; set; } public Logger Logger { get; set; } + //public string TMDbApiUrl { get; set; } public int MaxPages { get; set; } public TMDbRequestGenerator() { MaxPages = 3; + // TMDbApiUrl = "https://api.themoviedb.org"; } public virtual NetImportPageableRequestChain GetMovies() { - var searchType = ""; + var minVoteCount = Settings.MinVotes; + var minVoteAverage = Settings.MinVoteAverage; + var ceritification = Settings.Ceritification; + var includeGenreIds = Settings.IncludeGenreIds; + var excludeGenreIds = Settings.ExcludeGenreIds; + var languageCode = (TMDbLanguageCodes)Settings.LanguageCode; + var todaysDate = DateTime.Now.ToString("yyyy-MM-dd"); + var threeMonthsAgo = DateTime.Parse(todaysDate).AddMonths(-3).ToString("yyyy-MM-dd"); + var threeMonthsFromNow = DateTime.Parse(todaysDate).AddMonths(3).ToString("yyyy-MM-dd"); + + if (ceritification.IsNotNullOrWhiteSpace()) + { + ceritification = $"&certification_country=US&certification={ceritification}"; + } + + var tmdbParams = ""; switch (Settings.ListType) { case (int)TMDbListType.List: - searchType = $"/3/list/{Settings.ListId}"; + tmdbParams = $"/3/list/{Settings.ListId}/?api_key=1a7373301961d03f97f853a876dd1212"; break; case (int)TMDbListType.Theaters: - searchType = "/3/movie/now_playing"; + tmdbParams = $"/3/discover/movie?api_key=1a7373301961d03f97f853a876dd1212&primary_release_date.gte={threeMonthsAgo}&primary_release_date.lte={todaysDate}&vote_count.gte={minVoteCount}&vote_average.gte={minVoteAverage}{ceritification}&with_genres={includeGenreIds}&without_genres={excludeGenreIds}&with_original_language={languageCode}"; break; case (int)TMDbListType.Popular: - searchType = "/3/movie/popular"; + tmdbParams = $"/3/discover/movie?api_key=1a7373301961d03f97f853a876dd1212&sort_by=popularity.desc&vote_count.gte={minVoteCount}&vote_average.gte={minVoteAverage}{ceritification}&with_genres={includeGenreIds}&without_genres={excludeGenreIds}&with_original_language={languageCode}"; break; case (int)TMDbListType.Top: - searchType = "/3/movie/top_rated"; + tmdbParams = $"/3/discover/movie?api_key=1a7373301961d03f97f853a876dd1212&sort_by=vote_average.desc&vote_count.gte={minVoteCount}&vote_average.gte={minVoteAverage}{ceritification}&with_genres={includeGenreIds}&without_genres={excludeGenreIds}&with_original_language={languageCode}"; break; case (int)TMDbListType.Upcoming: - searchType = "/3/movie/upcoming"; + tmdbParams = $"/3/discover/movie?api_key=1a7373301961d03f97f853a876dd1212&primary_release_date.gte={todaysDate}&primary_release_date.lte={threeMonthsFromNow}&vote_count.gte={minVoteCount}&vote_average.gte={minVoteAverage}{ceritification}&with_genres={includeGenreIds}&without_genres={excludeGenreIds}&with_original_language={languageCode}"; break; } var pageableRequests = new NetImportPageableRequestChain(); - if (Settings.ListType != (int) TMDbListType.List) + if (Settings.ListType != (int)TMDbListType.List) { // First query to get the total_Pages - var requestBuilder = new HttpRequestBuilder($"{Settings.Link.Trim()}") + var requestBuilder = new HttpRequestBuilder($"{Settings.Link.TrimEnd("/")}") { LogResponseContent = true }; requestBuilder.Method = HttpMethod.GET; - requestBuilder.Resource(searchType); + requestBuilder.Resource(tmdbParams); var request = requestBuilder - .AddQueryParam("api_key", "1a7373301961d03f97f853a876dd1212") + // .AddQueryParam("api_key", "1a7373301961d03f97f853a876dd1212") .Accept(HttpAccept.Json) .Build(); @@ -64,20 +82,18 @@ namespace NzbDrone.Core.NetImport.TMDb var result = Json.Deserialize(response.Content); // @TODO Prolly some error handling to do here - pageableRequests.Add(GetPagedRequests(searchType, result.total_pages)); + pageableRequests.Add(GetPagedRequests(tmdbParams, result.total_pages)); return pageableRequests; } - else - { - pageableRequests.Add(GetPagedRequests(searchType, 0)); - return pageableRequests; - } - } - - private IEnumerable GetPagedRequests(string searchType, int totalPages) + + pageableRequests.Add(GetPagedRequests(tmdbParams, 0)); + return pageableRequests; + } + + private IEnumerable GetPagedRequests(string tmdbParams, int totalPages) { - var baseUrl = $"{Settings.Link.Trim()}{searchType}?api_key=1a7373301961d03f97f853a876dd1212"; - if (Settings.ListType != (int) TMDbListType.List) + var baseUrl = $"{Settings.Link.TrimEnd("/")}{tmdbParams}"; + if (Settings.ListType != (int)TMDbListType.List) { for (var pageNumber = 1; pageNumber <= totalPages; pageNumber++) { @@ -89,16 +105,16 @@ namespace NzbDrone.Core.NetImport.TMDb break; } - Logger.Trace($"Importing TMDb movies from: {baseUrl}&page={pageNumber}"); + Logger.Info($"Importing TMDb movies from: {baseUrl}&page={pageNumber}"); yield return new NetImportRequest($"{baseUrl}&page={pageNumber}", HttpAccept.Json); } } else { - Logger.Trace($"Importing TMDb movies from: {baseUrl}"); + Logger.Info($"Importing TMDb movies from: {baseUrl}"); yield return new NetImportRequest($"{baseUrl}", HttpAccept.Json); } } } -} +} \ No newline at end of file diff --git a/src/NzbDrone.Core/NetImport/TMDb/TMDbSettings.cs b/src/NzbDrone.Core/NetImport/TMDb/TMDbSettings.cs index 845f9f566..dca10555b 100644 --- a/src/NzbDrone.Core/NetImport/TMDb/TMDbSettings.cs +++ b/src/NzbDrone.Core/NetImport/TMDb/TMDbSettings.cs @@ -1,6 +1,4 @@ -using System.Collections.Generic; -using System.Globalization; -using FluentValidation; +using FluentValidation; using NzbDrone.Core.Annotations; using NzbDrone.Core.Validation; @@ -12,7 +10,8 @@ namespace NzbDrone.Core.NetImport.TMDb public TMDbSettingsValidator() { RuleFor(c => c.Link).ValidRootUrl(); - RuleFor(c => double.Parse(c.MinVoteAverage)).ExclusiveBetween(0, 10); + RuleFor(c => double.Parse(c.MinVoteAverage)).InclusiveBetween(0, 10); + RuleFor(c => c.MinVotes).GreaterThan(0); } } @@ -24,7 +23,8 @@ namespace NzbDrone.Core.NetImport.TMDb { Link = "https://api.themoviedb.org"; MinVoteAverage = "5.5"; - // Language = (int) TMDbLanguageCodes.en; + MinVotes = 1000; + LanguageCode = (int)TMDbLanguageCodes.en; } [FieldDefinition(0, Label = "TMDb API URL", HelpText = "Link to to TMDb API URL, do not change unless you know what you are doing.")] @@ -33,14 +33,26 @@ namespace NzbDrone.Core.NetImport.TMDb [FieldDefinition(1, Label = "List Type", Type = FieldType.Select, SelectOptions = typeof(TMDbListType), HelpText = "Type of list your seeking to import from")] public int ListType { get; set; } - //[FieldDefinition(2, Label = "Language", Type = FieldType.Select, SelectOptions = typeof(TMDbLanguageCodes), HelpText = "Filter movies by Language")] - //public int Language { get; set; } + [FieldDefinition(2, Label = "Public List ID", HelpText = "Required for List (Ignores Filtering Options)")] + public string ListId { get; set; } - [FieldDefinition(2, Label = "Minimum Vote Average", HelpText = "Filter movies by rating (0.0-10.0)")] + [FieldDefinition(3, Label = "Minimum Vote Average", HelpText = "Filter movies by votes (0.0-10.0)")] public string MinVoteAverage { get; set; } - [FieldDefinition(3, Label = "Public List ID", HelpText = "Required for List")] - public string ListId { get; set; } + [FieldDefinition(4, Label = "Minimum Number of Votes", HelpText = "Filter movies by number of votes")] + public int MinVotes { get; set; } + + [FieldDefinition(5, Label = "Rating", HelpText = "Filter movies by a rating (NR,G,PG,PG-13,R,NC-17)")] + public string Ceritification { get; set; } + + [FieldDefinition(6, Label = "Include Genre Ids", HelpText = "Filter movies by TMDb Genre Ids (Comma Separated)")] + public string IncludeGenreIds { get; set; } + + [FieldDefinition(7, Label = "Exclude Genre Ids", HelpText = "Filter movies by TMDb Genre Ids (Comma Separated)")] + public string ExcludeGenreIds { get; set; } + + [FieldDefinition(8, Label = "Original Language", Type = FieldType.Select, SelectOptions = typeof(TMDbLanguageCodes), HelpText = "Filter by Language")] + public int LanguageCode { get; set; } public new NzbDroneValidationResult Validate() { @@ -48,8 +60,4 @@ namespace NzbDrone.Core.NetImport.TMDb } } - - - - -} +} \ No newline at end of file