Radarr/NzbDrone.Core/Providers/XemCommunicationProvider.cs

68 lines
2.2 KiB
C#
Raw Normal View History

2012-10-17 05:00:28 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using NLog;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NzbDrone.Common;
using NzbDrone.Core.Model.Xem;
namespace NzbDrone.Core.Providers
{
2013-08-21 01:32:49 +00:00
public interface IXemCommunicationProvider
{
List<Int32> GetXemSeriesIds(string origin = "tvdb");
List<XemSceneTvdbMapping> GetSceneTvdbMappings(int id);
void CheckForFailureResult(string response);
}
public class XemCommunicationProvider : IXemCommunicationProvider
2012-10-17 05:00:28 +00:00
{
2013-04-10 23:41:45 +00:00
private readonly IHttpProvider _httpProvider;
2012-10-17 05:00:28 +00:00
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
private const string XEM_BASE_URL = "http://thexem.de/map/";
2013-04-10 23:41:45 +00:00
public XemCommunicationProvider(IHttpProvider httpProvider)
2012-10-17 05:00:28 +00:00
{
_httpProvider = httpProvider;
}
2013-08-21 01:32:49 +00:00
public List<Int32> GetXemSeriesIds(string origin = "tvdb")
2012-10-17 05:00:28 +00:00
{
_logger.Trace("Fetching Series IDs from: {0}", origin);
var url = String.Format("{0}havemap?origin={1}", XEM_BASE_URL, origin);
2012-10-17 05:00:28 +00:00
var response =_httpProvider.DownloadString(url);
CheckForFailureResult(response);
var result = JsonConvert.DeserializeObject<XemResult<List<Int32>>>(response);
2012-10-17 05:00:28 +00:00
return result.Data.ToList();
2012-10-17 05:00:28 +00:00
}
2013-08-21 01:32:49 +00:00
public List<XemSceneTvdbMapping> GetSceneTvdbMappings(int id)
2012-10-17 05:00:28 +00:00
{
_logger.Trace("Fetching Mappings for: {0}", id);
var url = String.Format("{0}all?id={1}&origin=tvdb", XEM_BASE_URL, id);
var response = _httpProvider.DownloadString(url);
CheckForFailureResult(response);
var result = JsonConvert.DeserializeObject<List<XemSceneTvdbMapping>>(JObject.Parse(response).SelectToken("data").ToString());
return result;
}
2013-08-21 01:32:49 +00:00
public void CheckForFailureResult(string response)
2012-10-17 05:00:28 +00:00
{
var result = JsonConvert.DeserializeObject<XemResult<dynamic>>(response);
if (result != null && result.Result.Equals("failure", StringComparison.InvariantCultureIgnoreCase))
throw new Exception("Error response received from Xem: " + result.Message);
}
}
}