diff --git a/src/NzbDrone.Core/Indexers/PassThePopcorn/PassThePopcornParser.cs b/src/NzbDrone.Core/Indexers/PassThePopcorn/PassThePopcornParser.cs index 0ad64c5eb..1b935a787 100644 --- a/src/NzbDrone.Core/Indexers/PassThePopcorn/PassThePopcornParser.cs +++ b/src/NzbDrone.Core/Indexers/PassThePopcorn/PassThePopcornParser.cs @@ -1,18 +1,19 @@ using System.Collections.Generic; using System.Net; using Newtonsoft.Json; -using Newtonsoft.Json.Linq; using NzbDrone.Common.Http; using NzbDrone.Core.Indexers.Exceptions; using NzbDrone.Core.Parser.Model; -using System; using System.Linq; +using NzbDrone.Common.Cache; +using NzbDrone.Common.Extensions; namespace NzbDrone.Core.Indexers.PassThePopcorn { public class PassThePopcornParser : IParseIndexerResponse { private readonly PassThePopcornSettings _settings; + public ICached> AuthCookieCache { get; set; } public PassThePopcornParser(PassThePopcornSettings settings) { @@ -25,21 +26,30 @@ namespace NzbDrone.Core.Indexers.PassThePopcorn if (indexerResponse.HttpResponse.StatusCode != HttpStatusCode.OK) { - throw new IndexerException(indexerResponse, - "Unexpected response status {0} code from API request", - indexerResponse.HttpResponse.StatusCode); + // Remove cookie cache + AuthCookieCache.Remove(_settings.BaseUrl.Trim().TrimEnd('/')); + + throw new IndexerException(indexerResponse, $"Unexpected response status {indexerResponse.HttpResponse.StatusCode} code from API request"); } - var jsonResponse = JsonConvert.DeserializeObject(indexerResponse.Content); + if (indexerResponse.HttpResponse.Headers.ContentType != HttpAccept.Json.Value) + { + // Remove cookie cache + AuthCookieCache.Remove(_settings.BaseUrl.Trim().TrimEnd('/')); + + throw new IndexerException(indexerResponse, $"Unexpected response header {indexerResponse.HttpResponse.Headers.ContentType} from API request, expected {HttpAccept.Json.Value}"); + } - var responseData = jsonResponse.Movies; - if (responseData == null) + var jsonResponse = JsonConvert.DeserializeObject(indexerResponse.Content); + if (jsonResponse.TotalResults == "0" || + jsonResponse.TotalResults.IsNullOrWhiteSpace() || + jsonResponse.Movies == null) { - throw new IndexerException(indexerResponse, - "Indexer API call response missing result data"); + throw new IndexerException(indexerResponse, "No results were found"); } - foreach (var result in responseData) + + foreach (var result in jsonResponse.Movies) { foreach (var torrent in result.Torrents) { @@ -155,10 +165,5 @@ namespace NzbDrone.Core.Indexers.PassThePopcorn return url.FullUri; } - - //public static bool IsPropertyExist(dynamic torrents, string name) - //{ - // return torrents.GetType().GetProperty(name) != null; - //} } } diff --git a/src/NzbDrone.Core/Indexers/PassThePopcorn/PassThePopcornRequestGenerator.cs b/src/NzbDrone.Core/Indexers/PassThePopcorn/PassThePopcornRequestGenerator.cs index 9127860eb..919ed0a96 100644 --- a/src/NzbDrone.Core/Indexers/PassThePopcorn/PassThePopcornRequestGenerator.cs +++ b/src/NzbDrone.Core/Indexers/PassThePopcorn/PassThePopcornRequestGenerator.cs @@ -99,7 +99,7 @@ namespace NzbDrone.Core.Indexers.PassThePopcorn Logger.Debug("PassThePopcorn authentication succeeded."); cookies = response.GetCookies(); - AuthCookieCache.Set(authKey, cookies); + AuthCookieCache.Set(authKey, cookies, new TimeSpan(7, 0, 0, 0, 0)); // re-auth every 7 days requestBuilder.SetCookies(cookies); } else