using System; using System.Collections.Generic; using System.Linq; using RestSharp; using RestSharp.Deserializers; namespace NzbDrone.Core.Tvdb { public partial class Tvdb { private T ProcessRequest (RestRequest request) where T: new() { return ProcessRequest(BASE_URL, request); } private T ProcessRequest (string url, RestRequest request) where T: new() { var client = new RestClient(url); client.AddHandler("text/xml", new DotNetXmlDeserializer()); if(Timeout.HasValue) client.Timeout = Timeout.Value; #if !WINDOWS_PHONE if(Proxy != null) client.Proxy = Proxy; #endif Error = null; //var resp = client.Execute(request); IRestResponse resp = client.Execute(request); ResponseContent = resp.Content; ResponseHeaders = resp.Headers.ToDictionary(k => k.Name, v => v.Value); if(resp.ResponseStatus == ResponseStatus.Completed) { return resp.Data; // Manual deserialization //TextReader r = new StringReader(resp.Content); //XmlSerializer s = new XmlSerializer(typeof(T)); //return (T)s.Deserialize(r); } else { if(resp.ErrorException != null) throw resp.ErrorException; else Error = resp.ErrorMessage; } return default(T); } public TvdbMirrors GetMirrors() { return ProcessRequest(BuildGetMirrorsRequest()); } /// /// http://www.thetvdb.com/api/Updates.php?type=none /// /// public TvdbServerTime GetServerTime() { return ProcessRequest(BuildGetServerTimeRequest()); } /// /// http://www.thetvdb.com/api/{apikey}/languages.xml /// /// public List GetLanguages() { var root = ProcessRequest(BuildGetLanguagesRequest()); if(root != null) return root.Languages; return null; } /// /// http://www.thetvdb.com/api/GetSeries.php?seriesname={series} /// /// /// public List SearchSeries(string search) { var root = ProcessRequest(BuildGetSearchSeriesRequest(search)); if(root != null) return root.Series; return null; } /// /// http://thetvdb.com/api/{apikey}/series/79349/en.xml /// /// /// /// /// public TvdbSeriesBase GetSeriesBaseRecord(string XMLMirror, int SeriesId, string Language) { if(string.IsNullOrEmpty(Language)) Language = "en"; var root = ProcessRequest(XMLMirror, BuildGetSeriesBaseRecordRequest(SeriesId, Language)); if(root != null) return root.Series; return null; } public TvdbSeriesBase GetSeriesBaseRecord(string XMLMirror, int SeriesId) { return GetSeriesBaseRecord(XMLMirror, SeriesId, null); } /// /// http://thetvdb.com/api/{apikey}/series/79349/all/en.xml /// /// /// /// /// public TvdbSeriesFull GetSeriesFullRecord(string XMLMirror, int SeriesId, string Language) { if(string.IsNullOrEmpty(Language)) Language = "en"; return ProcessRequest(XMLMirror, BuildGetSeriesFullRecordRequest(SeriesId, Language)); } public TvdbSeriesFull GetSeriesFullRecord(string MirrorPath, int SeriesId) { return GetSeriesFullRecord(MirrorPath, SeriesId, null); } /// /// http://thetvdb.com/api/{apikey}/series/79349/banners.xml /// /// /// /// public List GetSeriesBanners(string XMLMirror, int SeriesId) { var root = ProcessRequest(XMLMirror, BuildGetSeriesBannersRequest(SeriesId)); if(root != null) return root.Banners; return null; } /// /// http://thetvdb.com/api/{apikey}/series/79349/actors.xml /// /// /// /// public List GetSeriesActors(string XMLMirror, int SeriesId) { var root = ProcessRequest(XMLMirror, BuildGetSeriesActorsRequest(SeriesId)); if(root != null) return root.Actors; return null; } public TvdbEpisode GetEpisode(string XMLMirror, int EpisodeId, string Language) { if(string.IsNullOrEmpty(Language)) Language = "en"; var root = ProcessRequest(XMLMirror, BuildGetEpisodeRequest(EpisodeId, Language)); if(root != null) return root.Episode; return null; } public TvdbEpisode GetEpisode(string XMLMirror, int EpisodeId) { return GetEpisode(XMLMirror, EpisodeId, null); } public TvdbEpisode GetSeriesEpisode(string XMLMirror, int SeriesId, int SeasonNum, int EpisodeNum, string Language) { if(string.IsNullOrEmpty(Language)) Language = "en"; var root = ProcessRequest(XMLMirror, BuildGetSeriesEpisodeRequest(SeriesId, SeasonNum, EpisodeNum, Language)); if(root != null) return root.Episode; return null; } public TvdbEpisode GetSeriesEpisode(string XMLMirror, int SeriesId, int SeasonNum, int EpisodeNum) { return GetSeriesEpisode(XMLMirror, SeriesId, SeasonNum, EpisodeNum, null); } public TvdbUpdates GetUpdates(string XMLMirror, TvdbUpdatePeriod Period) { return ProcessRequest(XMLMirror, BuildGetUpdatesRequest(Period)); } public TvdbUpdateItems GetUpdatesSince(string XMLMirror, Int64 LastTime) { return ProcessRequest(XMLMirror, BuildGetUpdatesSinceRequest(LastTime)); } } }