Lidarr/NzbDrone.Core/DataAugmentation/Xem/XemProxy.cs

77 lines
2.2 KiB
C#
Raw Normal View History

2013-09-17 06:14:47 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using NLog;
2013-09-19 01:09:26 +00:00
using NzbDrone.Core.DataAugmentation.Xem.Model;
2013-09-17 06:14:47 +00:00
using NzbDrone.Core.Rest;
using RestSharp;
2013-09-19 01:09:26 +00:00
namespace NzbDrone.Core.DataAugmentation.Xem
2013-09-17 06:14:47 +00:00
{
public interface IXemProxy
{
2013-09-19 01:09:26 +00:00
List<int> GetXemSeriesIds();
2013-09-17 06:14:47 +00:00
List<XemSceneTvdbMapping> GetSceneTvdbMappings(int id);
}
public class XemProxy : IXemProxy
{
private readonly Logger _logger;
private const string XEM_BASE_URL = "http://thexem.de/map/";
public XemProxy(Logger logger)
{
_logger = logger;
}
private static RestRequest BuildRequest(string resource)
{
var req = new RestRequest(resource, Method.GET);
req.AddParameter("origin", "tvdb");
return req;
}
2013-09-19 01:09:26 +00:00
public List<int> GetXemSeriesIds()
2013-09-17 06:14:47 +00:00
{
_logger.Trace("Fetching Series IDs from");
var restClient = new RestClient(XEM_BASE_URL);
var request = BuildRequest("havemap");
2013-09-19 01:09:26 +00:00
var response = restClient.ExecuteAndValidate<XemResult<List<int>>>(request);
2013-09-17 06:14:47 +00:00
CheckForFailureResult(response);
return response.Data.ToList();
}
public List<XemSceneTvdbMapping> GetSceneTvdbMappings(int id)
{
_logger.Trace("Fetching Mappings for: {0}", id);
var url = String.Format("{0}all?id={1}&origin=tvdb", XEM_BASE_URL, id);
var restClient = new RestClient(XEM_BASE_URL);
var request = BuildRequest("all");
request.AddParameter("id", id);
var response = restClient.ExecuteAndValidate<XemResult<List<XemSceneTvdbMapping>>>(request);
CheckForFailureResult(response);
return response.Data;
}
private static void CheckForFailureResult<T>(XemResult<T> response)
{
if (response.Result.Equals("failure", StringComparison.InvariantCultureIgnoreCase) &&
!response.Message.Contains("no show with the tvdb_id"))
{
throw new Exception("Error response received from Xem: " + response.Message);
}
}
}
}