diff --git a/src/NzbDrone.Core/NetImport/StevenLu/StevenLuAPI.cs b/src/NzbDrone.Core/NetImport/StevenLu/StevenLuAPI.cs new file mode 100644 index 000000000..e56655278 --- /dev/null +++ b/src/NzbDrone.Core/NetImport/StevenLu/StevenLuAPI.cs @@ -0,0 +1,18 @@ +using System.Collections.Generic; +using System.Windows.Forms; +using System.Xml.Serialization; + +namespace NzbDrone.Core.NetImport.StevenLu +{ + public class StevenLuResponse + { + public Movie[] Movie { get; set; } + } + + public class Movie + { + public string title { get; set; } + public string imdb_id { get; set; } + public string poster_url { get; set; } + } +} diff --git a/src/NzbDrone.Core/NetImport/StevenLu/StevenLuImport.cs b/src/NzbDrone.Core/NetImport/StevenLu/StevenLuImport.cs new file mode 100644 index 000000000..1f3b76039 --- /dev/null +++ b/src/NzbDrone.Core/NetImport/StevenLu/StevenLuImport.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Xml.Serialization; +using FluentValidation.Results; +using NLog; +using NzbDrone.Common.Http; +using NzbDrone.Core.Configuration; +using NzbDrone.Core.Indexers; +using NzbDrone.Core.Indexers.PassThePopcorn; +using NzbDrone.Core.Parser; +using NzbDrone.Core.ThingiProvider; +using NzbDrone.Core.Tv; + +namespace NzbDrone.Core.NetImport.StevenLu +{ + public class StevenLuImport : HttpNetImportBase + { + public override string Name => "Popular movies from StevenLu"; + public override bool Enabled => true; + public override bool EnableAuto => true; + + public StevenLuImport(IHttpClient httpClient, IConfigService configService, IParsingService parsingService, Logger logger) + : base(httpClient, configService, parsingService, logger) + { } + + public override INetImportRequestGenerator GetRequestGenerator() + { + return new StevenLuRequestGenerator() { Settings = Settings }; + } + + public override IParseNetImportResponse GetParser() + { + return new StevenLuParser(Settings); + } + } +} diff --git a/src/NzbDrone.Core/NetImport/StevenLu/StevenLuParser.cs b/src/NzbDrone.Core/NetImport/StevenLu/StevenLuParser.cs new file mode 100644 index 000000000..a73c190aa --- /dev/null +++ b/src/NzbDrone.Core/NetImport/StevenLu/StevenLuParser.cs @@ -0,0 +1,82 @@ +using Newtonsoft.Json; +using NzbDrone.Core.NetImport.Exceptions; +using NzbDrone.Core.Tv; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net; +using System.Text; +using System.Text.RegularExpressions; +using System.Windows.Forms; +using System.Xml; +using System.Xml.Linq; +using NLog; +using NzbDrone.Common.Extensions; +using NzbDrone.Common.Http; +using NzbDrone.Core.Indexers; +using NzbDrone.Core.Indexers.Exceptions; +using NzbDrone.Core.Parser.Model; + +namespace NzbDrone.Core.NetImport.StevenLu +{ + public class StevenLuParser : IParseNetImportResponse + { + private readonly StevenLuSettings _settings; + private NetImportResponse _importResponse; + private readonly Logger _logger; + + public StevenLuParser(StevenLuSettings settings) + { + _settings = settings; + } + + public IList ParseResponse(NetImportResponse importResponse) + { + _importResponse = 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 item in jsonResponse.Movie) + { + movies.AddIfNotNull(new Tv.Movie() + { + Title = item.title, + ImdbId = item.imdb_id + }); + } + + return movies; + } + + protected virtual bool PreProcess(NetImportResponse indexerResponse) + { + if (indexerResponse.HttpResponse.StatusCode != HttpStatusCode.OK) + { + throw new NetImportException(indexerResponse, "Indexer API call resulted in an unexpected StatusCode [{0}]", indexerResponse.HttpResponse.StatusCode); + } + + if (indexerResponse.HttpResponse.Headers.ContentType != null && indexerResponse.HttpResponse.Headers.ContentType.Contains("text/json") && + indexerResponse.HttpRequest.Headers.Accept != null && !indexerResponse.HttpRequest.Headers.Accept.Contains("text/json")) + { + throw new NetImportException(indexerResponse, "Indexer responded with html content. Site is likely blocked or unavailable."); + } + + return true; + } + + } +} diff --git a/src/NzbDrone.Core/NetImport/StevenLu/StevenLuRequestGenerator.cs b/src/NzbDrone.Core/NetImport/StevenLu/StevenLuRequestGenerator.cs new file mode 100644 index 000000000..9e573bcf4 --- /dev/null +++ b/src/NzbDrone.Core/NetImport/StevenLu/StevenLuRequestGenerator.cs @@ -0,0 +1,28 @@ +using NzbDrone.Common.Http; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace NzbDrone.Core.NetImport.StevenLu +{ + public class StevenLuRequestGenerator : INetImportRequestGenerator + { + public StevenLuSettings Settings { get; set; } + + public virtual NetImportPageableRequestChain GetMovies() + { + var pageableRequests = new NetImportPageableRequestChain(); + + pageableRequests.Add(GetMovies(null)); + + return pageableRequests; + } + + private IEnumerable GetMovies(string searchParameters) + { + var request = new NetImportRequest($"{Settings.Link.Trim()}", HttpAccept.Json); + yield return request; + } + } +} diff --git a/src/NzbDrone.Core/NetImport/StevenLu/StevenLuSettings.cs b/src/NzbDrone.Core/NetImport/StevenLu/StevenLuSettings.cs new file mode 100644 index 000000000..a0829c868 --- /dev/null +++ b/src/NzbDrone.Core/NetImport/StevenLu/StevenLuSettings.cs @@ -0,0 +1,22 @@ +using FluentValidation; +using NzbDrone.Core.Annotations; +using NzbDrone.Core.Profiles; +using NzbDrone.Core.ThingiProvider; +using NzbDrone.Core.Validation; + +namespace NzbDrone.Core.NetImport.StevenLu +{ + + public class StevenLuSettings : NetImportBaseSettings + { + public StevenLuSettings() + { + Link = "https://s3.amazonaws.com/popular-movies/movies.json"; + } + + [FieldDefinition(0, Label = "URL", HelpText = "Don't change this unless you know what you are doing.")] + public new string Link { get; set; } + + } + +}