diff --git a/src/NzbDrone.Core/ImportLists/TMDb/Company/TMDbCompanyImport.cs b/src/NzbDrone.Core/ImportLists/TMDb/Company/TMDbCompanyImport.cs new file mode 100644 index 000000000..bd52d5ec9 --- /dev/null +++ b/src/NzbDrone.Core/ImportLists/TMDb/Company/TMDbCompanyImport.cs @@ -0,0 +1,43 @@ +using NLog; +using NzbDrone.Common.Cloud; +using NzbDrone.Common.Http; +using NzbDrone.Core.Configuration; +using NzbDrone.Core.MetadataSource; +using NzbDrone.Core.Parser; + +namespace NzbDrone.Core.ImportLists.TMDb.Company +{ + public class TMDbCompanyImport : TMDbImportListBase + { + public TMDbCompanyImport(IRadarrCloudRequestBuilder requestBuilder, + IHttpClient httpClient, + IImportListStatusService importListStatusService, + IConfigService configService, + IParsingService parsingService, + ISearchForNewMovie searchForNewMovie, + Logger logger) + : base(requestBuilder, httpClient, importListStatusService, configService, parsingService, searchForNewMovie, logger) + { + } + + public override string Name => "TMDb Company"; + public override bool Enabled => true; + public override bool EnableAuto => false; + + public override IParseImportListResponse GetParser() + { + return new TMDbCompanyParser(); + } + + public override IImportListRequestGenerator GetRequestGenerator() + { + return new TMDbCompanyRequestGenerator() + { + RequestBuilder = _requestBuilder, + Settings = Settings, + Logger = _logger, + HttpClient = _httpClient + }; + } + } +} diff --git a/src/NzbDrone.Core/ImportLists/TMDb/Company/TMDbCompanyParser.cs b/src/NzbDrone.Core/ImportLists/TMDb/Company/TMDbCompanyParser.cs new file mode 100644 index 000000000..ce7e6136e --- /dev/null +++ b/src/NzbDrone.Core/ImportLists/TMDb/Company/TMDbCompanyParser.cs @@ -0,0 +1,41 @@ +using System.Collections.Generic; +using Newtonsoft.Json; +using NzbDrone.Common.Extensions; +using NzbDrone.Core.ImportLists.ImportListMovies; + +namespace NzbDrone.Core.ImportLists.TMDb.Company +{ + public class TMDbCompanyParser : TMDbParser + { + public override IList ParseResponse(ImportListResponse importResponse) + { + var movies = new List(); + + if (!PreProcess(importResponse)) + { + return movies; + } + + var jsonResponse = JsonConvert.DeserializeObject(importResponse.Content); + + // no movies were return + if (jsonResponse == null) + { + return movies; + } + + foreach (var movie in jsonResponse.Results) + { + // Movies with no Year Fix + if (string.IsNullOrWhiteSpace(movie.ReleaseDate)) + { + continue; + } + + movies.AddIfNotNull(MapListMovie(movie)); + } + + return movies; + } + } +} diff --git a/src/NzbDrone.Core/ImportLists/TMDb/Company/TMDbCompanyRequestGenerator.cs b/src/NzbDrone.Core/ImportLists/TMDb/Company/TMDbCompanyRequestGenerator.cs new file mode 100644 index 000000000..121ae13cf --- /dev/null +++ b/src/NzbDrone.Core/ImportLists/TMDb/Company/TMDbCompanyRequestGenerator.cs @@ -0,0 +1,57 @@ +using System.Collections.Generic; +using Newtonsoft.Json; +using NLog; +using NzbDrone.Common.Http; + +namespace NzbDrone.Core.ImportLists.TMDb.Company +{ + public class TMDbCompanyRequestGenerator : IImportListRequestGenerator + { + public TMDbCompanySettings Settings { get; set; } + public IHttpClient HttpClient { get; set; } + public IHttpRequestBuilderFactory RequestBuilder { get; set; } + public Logger Logger { get; set; } + public int MaxPages { get; set; } + + public TMDbCompanyRequestGenerator() + { + } + + public virtual ImportListPageableRequestChain GetMovies() + { + var pageableRequests = new ImportListPageableRequestChain(); + + pageableRequests.Add(GetMoviesRequest()); + + return pageableRequests; + } + + private IEnumerable GetMoviesRequest() + { + Logger.Info($"Importing TMDb movies from company: {Settings.CompanyId}"); + + var requestBuilder = RequestBuilder.Create() + .SetSegment("api", "3") + .SetSegment("route", "discover") + .SetSegment("id", $"movie") + .SetSegment("secondaryRoute", ""); + + requestBuilder.AddQueryParam("with_companies", Settings.CompanyId); + + var jsonResponse = JsonConvert.DeserializeObject(HttpClient.Execute(requestBuilder.Build()).Content); + + MaxPages = jsonResponse.TotalPages; + + for (var pageNumber = 1; pageNumber <= MaxPages; pageNumber++) + { + requestBuilder.AddQueryParam("page", pageNumber, true); + + var request = requestBuilder.Build(); + + Logger.Debug($"Importing TMDb movies from: {request.Url}"); + + yield return new ImportListRequest(request); + } + } + } +} diff --git a/src/NzbDrone.Core/ImportLists/TMDb/Company/TMDbCompanySettings.cs b/src/NzbDrone.Core/ImportLists/TMDb/Company/TMDbCompanySettings.cs new file mode 100644 index 000000000..d9b4d0ba6 --- /dev/null +++ b/src/NzbDrone.Core/ImportLists/TMDb/Company/TMDbCompanySettings.cs @@ -0,0 +1,28 @@ +using System.Text.RegularExpressions; +using FluentValidation; +using NzbDrone.Core.Annotations; + +namespace NzbDrone.Core.ImportLists.TMDb.Company +{ + public class TMDbCompanySettingsValidator : TMDbSettingsBaseValidator + { + public TMDbCompanySettingsValidator() + : base() + { + RuleFor(c => c.CompanyId).Matches(@"^[1-9][0-9]*$", RegexOptions.IgnoreCase); + } + } + + public class TMDbCompanySettings : TMDbSettingsBase + { + protected override AbstractValidator Validator => new TMDbCompanySettingsValidator(); + + public TMDbCompanySettings() + { + CompanyId = ""; + } + + [FieldDefinition(1, Label = "Company Id", Type = FieldType.Textbox, HelpText = "TMDb Id of Company to Follow")] + public string CompanyId { get; set; } + } +}