From 571730ddec5152e8a3935dae1ba0664c3c140707 Mon Sep 17 00:00:00 2001 From: Devin Buhl Date: Wed, 8 Mar 2017 19:00:00 -0500 Subject: [PATCH] Onedr0p/3 8 17 (#1087) * Move ToUrlSlug to Parser, fix issue with trakt lists that have non-alphanumeric characters * Move "Add movies" link in nav to the first link * String interpolation! * Add Limit to Trakt List Settings to limit the amount of movies returned, 50 by default * Updates to FailedDownload * Update DownloadService and CompleteDownloadService --- .../Migration/118_update_movie_slug.cs | 29 ++------------ .../Download/CompletedDownloadService.cs | 3 -- src/NzbDrone.Core/Download/DownloadService.cs | 8 ++-- .../Download/FailedDownloadService.cs | 4 +- .../RedownloadFailedDownloadService.cs | 38 ++----------------- .../DownloadMonitoringService.cs | 2 +- .../MetadataSource/SkyHook/SkyHookProxy.cs | 32 +--------------- .../NetImport/Trakt/TraktRequestGenerator.cs | 29 +++++++------- .../NetImport/Trakt/TraktSettings.cs | 9 +++++ src/NzbDrone.Core/Parser/Parser.cs | 25 ++++++++++++ src/UI/Navbar/NavbarLayoutTemplate.hbs | 3 +- 11 files changed, 67 insertions(+), 115 deletions(-) diff --git a/src/NzbDrone.Core/Datastore/Migration/118_update_movie_slug.cs b/src/NzbDrone.Core/Datastore/Migration/118_update_movie_slug.cs index f7026ae5d..c0eb85b42 100644 --- a/src/NzbDrone.Core/Datastore/Migration/118_update_movie_slug.cs +++ b/src/NzbDrone.Core/Datastore/Migration/118_update_movie_slug.cs @@ -19,7 +19,7 @@ namespace NzbDrone.Core.Datastore.Migration using (IDbCommand getSeriesCmd = conn.CreateCommand()) { getSeriesCmd.Transaction = tran; - getSeriesCmd.CommandText = @"SELECT Id, Title, Year FROM Movies"; + getSeriesCmd.CommandText = @"SELECT Id, Title, Year, TmdbId FROM Movies"; using (IDataReader seriesReader = getSeriesCmd.ExecuteReader()) { while (seriesReader.Read()) @@ -27,8 +27,9 @@ namespace NzbDrone.Core.Datastore.Migration var id = seriesReader.GetInt32(0); var title = seriesReader.GetString(1); var year = seriesReader.GetInt32(2); + var tmdbId = seriesReader.GetInt32(3); - var titleSlug = ToUrlSlug(title + "-" + year); + var titleSlug = Parser.Parser.ToUrlSlug(title + "-" + tmdbId); using (IDbCommand updateCmd = conn.CreateCommand()) { @@ -43,29 +44,5 @@ namespace NzbDrone.Core.Datastore.Migration } } } - - public static string ToUrlSlug(string value) - { - //First to lower case - value = value.ToLowerInvariant(); - - //Remove all accents - var bytes = Encoding.GetEncoding("Cyrillic").GetBytes(value); - value = Encoding.ASCII.GetString(bytes); - - //Replace spaces - value = Regex.Replace(value, @"\s", "-", RegexOptions.Compiled); - - //Remove invalid chars - value = Regex.Replace(value, @"[^a-z0-9\s-_]", "", RegexOptions.Compiled); - - //Trim dashes from end - value = value.Trim('-', '_'); - - //Replace double occurences of - or _ - value = Regex.Replace(value, @"([-_]){2,}", "$1", RegexOptions.Compiled); - - return value; - } } } \ No newline at end of file diff --git a/src/NzbDrone.Core/Download/CompletedDownloadService.cs b/src/NzbDrone.Core/Download/CompletedDownloadService.cs index 955816c64..6e9695ec3 100644 --- a/src/NzbDrone.Core/Download/CompletedDownloadService.cs +++ b/src/NzbDrone.Core/Download/CompletedDownloadService.cs @@ -30,14 +30,12 @@ namespace NzbDrone.Core.Download private readonly IParsingService _parsingService; private readonly IMovieService _movieService; private readonly Logger _logger; - private readonly ISeriesService _seriesService; public CompletedDownloadService(IConfigService configService, IEventAggregator eventAggregator, IHistoryService historyService, IDownloadedMovieImportService downloadedMovieImportService, IParsingService parsingService, - ISeriesService seriesService, IMovieService movieService, Logger logger) { @@ -48,7 +46,6 @@ namespace NzbDrone.Core.Download _parsingService = parsingService; _movieService = movieService; _logger = logger; - _seriesService = seriesService; } public void Process(TrackedDownload trackedDownload, bool ignoreWarnings = false) diff --git a/src/NzbDrone.Core/Download/DownloadService.cs b/src/NzbDrone.Core/Download/DownloadService.cs index b7c4b205a..c8068c044 100644 --- a/src/NzbDrone.Core/Download/DownloadService.cs +++ b/src/NzbDrone.Core/Download/DownloadService.cs @@ -137,16 +137,16 @@ namespace NzbDrone.Core.Download throw; } - var episodeGrabbedEvent = new MovieGrabbedEvent(remoteMovie); - episodeGrabbedEvent.DownloadClient = downloadClient.GetType().Name; + var movieGrabbedEvent = new MovieGrabbedEvent(remoteMovie); + movieGrabbedEvent.DownloadClient = downloadClient.GetType().Name; if (!string.IsNullOrWhiteSpace(downloadClientId)) { - episodeGrabbedEvent.DownloadId = downloadClientId; + movieGrabbedEvent.DownloadId = downloadClientId; } _logger.ProgressInfo("Report sent to {0}. {1}", downloadClient.Definition.Name, downloadTitle); - _eventAggregator.PublishEvent(episodeGrabbedEvent); + _eventAggregator.PublishEvent(movieGrabbedEvent); } } } \ No newline at end of file diff --git a/src/NzbDrone.Core/Download/FailedDownloadService.cs b/src/NzbDrone.Core/Download/FailedDownloadService.cs index 4168e0451..45e83439e 100644 --- a/src/NzbDrone.Core/Download/FailedDownloadService.cs +++ b/src/NzbDrone.Core/Download/FailedDownloadService.cs @@ -87,9 +87,9 @@ namespace NzbDrone.Core.Download var downloadFailedEvent = new DownloadFailedEvent { - SeriesId = historyItem.SeriesId, + SeriesId = 0, MovieId = historyItem.MovieId, - EpisodeIds = historyItems.Select(h => h.EpisodeId).ToList(), + EpisodeIds = null, Quality = historyItem.Quality, SourceTitle = historyItem.SourceTitle, DownloadClient = historyItem.Data.GetValueOrDefault(History.History.DOWNLOAD_CLIENT), diff --git a/src/NzbDrone.Core/Download/RedownloadFailedDownloadService.cs b/src/NzbDrone.Core/Download/RedownloadFailedDownloadService.cs index f49f3772e..2784d35c0 100644 --- a/src/NzbDrone.Core/Download/RedownloadFailedDownloadService.cs +++ b/src/NzbDrone.Core/Download/RedownloadFailedDownloadService.cs @@ -12,17 +12,17 @@ namespace NzbDrone.Core.Download public class RedownloadFailedDownloadService : IHandleAsync { private readonly IConfigService _configService; - private readonly IEpisodeService _episodeService; + private readonly IMovieService _movieService; private readonly IManageCommandQueue _commandQueueManager; private readonly Logger _logger; public RedownloadFailedDownloadService(IConfigService configService, - IEpisodeService episodeService, + IMovieService movieService, IManageCommandQueue commandQueueManager, Logger logger) { _configService = configService; - _episodeService = episodeService; + _movieService = movieService; _commandQueueManager = commandQueueManager; _logger = logger; } @@ -38,40 +38,8 @@ namespace NzbDrone.Core.Download if (message.MovieId != 0) { _logger.Debug("Failed download contains a movie, searching again."); - _commandQueueManager.Push(new MoviesSearchCommand { MovieIds = new List { message.MovieId } }); - - return; - } - - if (message.EpisodeIds.Count == 1) - { - _logger.Debug("Failed download only contains one episode, searching again"); - - _commandQueueManager.Push(new EpisodeSearchCommand(message.EpisodeIds)); - - return; } - - var seasonNumber = _episodeService.GetEpisode(message.EpisodeIds.First()).SeasonNumber; - var episodesInSeason = _episodeService.GetEpisodesBySeason(message.SeriesId, seasonNumber); - - if (message.EpisodeIds.Count == episodesInSeason.Count) - { - _logger.Debug("Failed download was entire season, searching again"); - - _commandQueueManager.Push(new SeasonSearchCommand - { - SeriesId = message.SeriesId, - SeasonNumber = seasonNumber - }); - - return; - } - - _logger.Debug("Failed download contains multiple episodes, probably a double episode, searching again"); - - _commandQueueManager.Push(new EpisodeSearchCommand(message.EpisodeIds)); } } } diff --git a/src/NzbDrone.Core/Download/TrackedDownloads/DownloadMonitoringService.cs b/src/NzbDrone.Core/Download/TrackedDownloads/DownloadMonitoringService.cs index cf9124d19..7a88f1713 100644 --- a/src/NzbDrone.Core/Download/TrackedDownloads/DownloadMonitoringService.cs +++ b/src/NzbDrone.Core/Download/TrackedDownloads/DownloadMonitoringService.cs @@ -135,7 +135,7 @@ namespace NzbDrone.Core.Download.TrackedDownloads } catch (Exception e) { - _logger.Error(e, "Couldn't process tracked download " + downloadItem.Title); + _logger.Error(e, $"Couldn't process tracked download {downloadItem.Title}"); } return trackedDownloads; diff --git a/src/NzbDrone.Core/MetadataSource/SkyHook/SkyHookProxy.cs b/src/NzbDrone.Core/MetadataSource/SkyHook/SkyHookProxy.cs index 8973ec8d3..72a6c0a54 100644 --- a/src/NzbDrone.Core/MetadataSource/SkyHook/SkyHookProxy.cs +++ b/src/NzbDrone.Core/MetadataSource/SkyHook/SkyHookProxy.cs @@ -9,11 +9,7 @@ using NzbDrone.Common.Http; using NzbDrone.Core.Exceptions; using NzbDrone.Core.MediaCover; using NzbDrone.Core.MetadataSource.SkyHook.Resource; -using NzbDrone.Core.MetadataSource; using NzbDrone.Core.Tv; -using Newtonsoft.Json; -using System.Text.RegularExpressions; -using System.Text; using System.Threading; using NzbDrone.Core.Parser; using NzbDrone.Core.Profiles; @@ -133,7 +129,7 @@ namespace NzbDrone.Core.MetadataSource.SkyHook movie.TmdbId = TmdbId; movie.ImdbId = resource.imdb_id; movie.Title = resource.title; - movie.TitleSlug = ToUrlSlug(resource.title); + movie.TitleSlug = Parser.Parser.ToUrlSlug(resource.title); movie.CleanTitle = Parser.Parser.CleanSeriesTitle(resource.title); movie.SortTitle = Parser.Parser.NormalizeTitle(resource.title); movie.Overview = resource.overview; @@ -475,7 +471,7 @@ namespace NzbDrone.Core.MetadataSource.SkyHook { imdbMovie.SortTitle = Parser.Parser.NormalizeTitle(result.title); imdbMovie.Title = result.title; - imdbMovie.TitleSlug = ToUrlSlug(result.title); + imdbMovie.TitleSlug = Parser.Parser.ToUrlSlug(result.title); if (result.release_date.IsNotNullOrWhiteSpace()) { @@ -662,30 +658,6 @@ namespace NzbDrone.Core.MetadataSource.SkyHook } } - public static string ToUrlSlug(string value) - { - //First to lower case - value = value.ToLowerInvariant(); - - //Remove all accents - var bytes = Encoding.GetEncoding("ISO-8859-8").GetBytes(value); - value = Encoding.ASCII.GetString(bytes); - - //Replace spaces - value = Regex.Replace(value, @"\s", "-", RegexOptions.Compiled); - - //Remove invalid chars - value = Regex.Replace(value, @"[^a-z0-9\s-_]", "", RegexOptions.Compiled); - - //Trim dashes from end - value = value.Trim('-', '_'); - - //Replace double occurences of - or _ - value = Regex.Replace(value, @"([-_]){2,}", "$1", RegexOptions.Compiled); - - return value; - } - public Movie MapMovieToTmdbMovie(Movie movie) { Movie newMovie = movie; diff --git a/src/NzbDrone.Core/NetImport/Trakt/TraktRequestGenerator.cs b/src/NzbDrone.Core/NetImport/Trakt/TraktRequestGenerator.cs index 3ee8c6014..5013d1f7f 100644 --- a/src/NzbDrone.Core/NetImport/Trakt/TraktRequestGenerator.cs +++ b/src/NzbDrone.Core/NetImport/Trakt/TraktRequestGenerator.cs @@ -1,6 +1,8 @@ using NzbDrone.Common.Http; using System; using System.Collections.Generic; +using System.Text.RegularExpressions; +using NzbDrone.Common.Extensions; using NzbDrone.Common.Serializer; using NzbDrone.Core.Configuration; @@ -87,42 +89,43 @@ namespace NzbDrone.Core.NetImport.Trakt { var link = Settings.Link.Trim(); - var filters = $"?years={Settings.Years}&genres={Settings.Genres.ToLower()}&ratings={Settings.Rating}&certifications={Settings.Ceritification.ToLower()}"; + var filtersAndLimit = $"?years={Settings.Years}&genres={Settings.Genres.ToLower()}&ratings={Settings.Rating}&certifications={Settings.Ceritification.ToLower()}&limit={Settings.Limit}"; switch (Settings.ListType) { case (int)TraktListType.UserCustomList: - link = link + $"/users/{Settings.Username.Trim()}/lists/{Settings.Listname.Trim()}/items/movies"; + var listName = Parser.Parser.ToUrlSlug(Settings.Listname.Trim()); + link = link + $"/users/{Settings.Username.Trim()}/lists/{listName}/items/movies?limit={Settings.Limit}"; break; case (int)TraktListType.UserWatchList: - link = link + $"/users/{Settings.Username.Trim()}/watchlist/movies"; + link = link + $"/users/{Settings.Username.Trim()}/watchlist/movies?limit={Settings.Limit}"; break; case (int)TraktListType.UserWatchedList: - link = link + $"/users/{Settings.Username.Trim()}/watched/movies"; + link = link + $"/users/{Settings.Username.Trim()}/watched/movies?limit={Settings.Limit}"; break; case (int)TraktListType.Trending: - link = link + "/movies/trending" + filters; + link = link + "/movies/trending" + filtersAndLimit; break; case (int)TraktListType.Popular: - link = link + "/movies/popular" + filters; + link = link + "/movies/popular" + filtersAndLimit; break; case (int)TraktListType.Anticipated: - link = link + "/movies/anticipated" + filters; + link = link + "/movies/anticipated" + filtersAndLimit; break; case (int)TraktListType.BoxOffice: - link = link + "/movies/boxoffice" + filters; + link = link + "/movies/boxoffice" + filtersAndLimit; break; case (int)TraktListType.TopWatchedByWeek: - link = link + "/movies/watched/weekly" + filters; + link = link + "/movies/watched/weekly" + filtersAndLimit; break; case (int)TraktListType.TopWatchedByMonth: - link = link + "/movies/watched/monthly" + filters; + link = link + "/movies/watched/monthly" + filtersAndLimit; break; case (int)TraktListType.TopWatchedByYear: - link = link + "/movies/watched/yearly" + filters; + link = link + "/movies/watched/yearly" + filtersAndLimit; break; case (int)TraktListType.TopWatchedByAllTime: - link = link + "/movies/watched/all" + filters; + link = link + "/movies/watched/all" + filtersAndLimit; break; } @@ -131,7 +134,7 @@ namespace NzbDrone.Core.NetImport.Trakt var request = new NetImportRequest($"{link}", HttpAccept.Json); request.HttpRequest.Headers.Add("trakt-api-version", "2"); request.HttpRequest.Headers.Add("trakt-api-key", "964f67b126ade0112c4ae1f0aea3a8fb03190f71117bd83af6a0560a99bc52e6"); //aeon - if (_configService.TraktAuthToken != null) + if (_configService.TraktAuthToken.IsNotNullOrWhiteSpace()) { request.HttpRequest.Headers.Add("Authorization", "Bearer " + _configService.TraktAuthToken); } diff --git a/src/NzbDrone.Core/NetImport/Trakt/TraktSettings.cs b/src/NzbDrone.Core/NetImport/Trakt/TraktSettings.cs index cc4d29399..3cfa5ccc0 100644 --- a/src/NzbDrone.Core/NetImport/Trakt/TraktSettings.cs +++ b/src/NzbDrone.Core/NetImport/Trakt/TraktSettings.cs @@ -43,6 +43,11 @@ namespace NzbDrone.Core.NetImport.Trakt .Matches(@"^\d+(\-\d+)?$", RegexOptions.IgnoreCase) .When(c => c.Years.IsNotNullOrWhiteSpace()) .WithMessage("Not a valid year or range of years"); + + // Limit not smaller than 1 and not larger than 100 + RuleFor(c => c.Limit) + .InclusiveBetween(1, 500) + .WithMessage("Must be 1 thru 500"); } } @@ -60,6 +65,7 @@ namespace NzbDrone.Core.NetImport.Trakt Ceritification = "NR,G,PG,PG-13,R,NC-17"; Genres = ""; Years = ""; + Limit = 100; } [FieldDefinition(0, Label = "Trakt API URL", HelpText = "Link to to Trakt API URL, do not change unless you know what you are doing.")] @@ -86,6 +92,9 @@ namespace NzbDrone.Core.NetImport.Trakt [FieldDefinition(7, Label = "Years", HelpText = "Filter movies by year or year range")] public string Years { get; set; } + [FieldDefinition(8, Label = "Limit", HelpText = "Limit the number of movies to get")] + public int Limit { get; set; } + public NzbDroneValidationResult Validate() { diff --git a/src/NzbDrone.Core/Parser/Parser.cs b/src/NzbDrone.Core/Parser/Parser.cs index d56d241a6..84e3b3190 100644 --- a/src/NzbDrone.Core/Parser/Parser.cs +++ b/src/NzbDrone.Core/Parser/Parser.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Text; using System.Text.RegularExpressions; using NLog; using NzbDrone.Common.Extensions; @@ -608,6 +609,30 @@ namespace NzbDrone.Core.Parser return null; } + public static string ToUrlSlug(string value) + { + //First to lower case + value = value.ToLowerInvariant(); + + //Remove all accents + var bytes = Encoding.GetEncoding("ISO-8859-8").GetBytes(value); + value = Encoding.ASCII.GetString(bytes); + + //Replace spaces + value = Regex.Replace(value, @"\s", "-", RegexOptions.Compiled); + + //Remove invalid chars + value = Regex.Replace(value, @"[^a-z0-9\s-_]", "", RegexOptions.Compiled); + + //Trim dashes from end + value = value.Trim('-', '_'); + + //Replace double occurences of - or _ + value = Regex.Replace(value, @"([-_]){2,}", "$1", RegexOptions.Compiled); + + return value; + } + public static string ParseSeriesName(string title) { Logger.Debug("Parsing string '{0}'", title); diff --git a/src/UI/Navbar/NavbarLayoutTemplate.hbs b/src/UI/Navbar/NavbarLayoutTemplate.hbs index cb2237964..873887929 100644 --- a/src/UI/Navbar/NavbarLayoutTemplate.hbs +++ b/src/UI/Navbar/NavbarLayoutTemplate.hbs @@ -19,8 +19,9 @@