Radarr/src/NzbDrone.Core/ImportLists/CouchPotato/CouchPotatoParser.cs

91 lines
3.3 KiB
C#
Raw Normal View History

using System.Collections.Generic;
2017-01-22 00:15:06 +00:00
using System.Linq;
using System.Net;
2019-12-22 22:08:53 +00:00
using Newtonsoft.Json;
2017-01-22 00:15:06 +00:00
using NzbDrone.Common.Extensions;
using NzbDrone.Core.ImportLists.Exceptions;
using NzbDrone.Core.ImportLists.ImportListMovies;
2017-01-22 00:15:06 +00:00
namespace NzbDrone.Core.ImportLists.CouchPotato
2017-01-22 00:15:06 +00:00
{
public class CouchPotatoParser : IParseImportListResponse
2017-01-22 00:15:06 +00:00
{
private ImportListResponse _importListResponse;
2017-01-22 00:15:06 +00:00
public IList<ImportListMovie> ParseResponse(ImportListResponse importListResponse)
2017-01-22 00:15:06 +00:00
{
_importListResponse = importListResponse;
2017-01-22 00:15:06 +00:00
var movies = new List<ImportListMovie>();
2017-01-22 00:15:06 +00:00
if (!PreProcess(_importListResponse))
2017-01-22 00:15:06 +00:00
{
return movies;
}
var jsonResponse = JsonConvert.DeserializeObject<CouchPotatoResponse>(_importListResponse.Content);
2017-01-22 00:15:06 +00:00
// no movies were return
if (jsonResponse.total == 0)
{
return movies;
}
var responseData = jsonResponse.movies;
foreach (var item in responseData)
{
var tmdbid = item.info?.tmdb_id ?? 0;
2017-03-10 00:40:55 +00:00
// Fix weird error reported by Madmanali93
if (item.type != null && item.releases != null)
2017-01-22 00:15:06 +00:00
{
2017-03-10 00:40:55 +00:00
// if there are no releases at all the movie wasn't found on CP, so return movies
if (!item.releases.Any() && item.type == "movie")
2017-01-22 00:15:06 +00:00
{
movies.AddIfNotNull(new ImportListMovie()
2017-01-22 00:15:06 +00:00
{
Title = item.title,
ImdbId = item.info.imdb,
2017-03-10 00:40:55 +00:00
TmdbId = tmdbid
2017-01-22 00:15:06 +00:00
});
}
2017-03-10 00:40:55 +00:00
else
{
// snatched,missing,available,downloaded
// done,seeding
var isCompleted = item.releases.Any(rel => (rel.status == "done" || rel.status == "seeding"));
2017-03-10 00:40:55 +00:00
if (!isCompleted)
{
movies.AddIfNotNull(new ImportListMovie()
2017-03-10 00:40:55 +00:00
{
Title = item.title,
ImdbId = item.info.imdb,
TmdbId = tmdbid
2017-03-10 00:40:55 +00:00
});
}
}
2017-01-22 00:15:06 +00:00
}
}
return movies;
}
protected virtual bool PreProcess(ImportListResponse importListResponse)
2017-01-22 00:15:06 +00:00
{
if (importListResponse.HttpResponse.StatusCode != HttpStatusCode.OK)
2017-01-22 00:15:06 +00:00
{
throw new ImportListException(importListResponse, "List API call resulted in an unexpected StatusCode [{0}]", importListResponse.HttpResponse.StatusCode);
2017-01-22 00:15:06 +00:00
}
if (importListResponse.HttpResponse.Headers.ContentType != null && importListResponse.HttpResponse.Headers.ContentType.Contains("text/json") &&
importListResponse.HttpRequest.Headers.Accept != null && !importListResponse.HttpRequest.Headers.Accept.Contains("text/json"))
2017-01-22 00:15:06 +00:00
{
throw new ImportListException(importListResponse, "List responded with html content. Site is likely blocked or unavailable.");
2017-01-22 00:15:06 +00:00
}
return true;
}
}
}