diff --git a/PlexRequests.Api.Interfaces/IApiRequest.cs b/PlexRequests.Api.Interfaces/IApiRequest.cs index 5b3e0d556..a7e274752 100644 --- a/PlexRequests.Api.Interfaces/IApiRequest.cs +++ b/PlexRequests.Api.Interfaces/IApiRequest.cs @@ -34,5 +34,6 @@ namespace PlexRequests.Api.Interfaces { T Execute(IRestRequest request, Uri baseUri) where T : new(); T ExecuteXml(IRestRequest request, Uri baseUri) where T : class; + T ExecuteJson(IRestRequest request, Uri baseUri) where T : new(); } } diff --git a/PlexRequests.Api/ApiRequest.cs b/PlexRequests.Api/ApiRequest.cs index e1a67e852..dd1f1c6d0 100644 --- a/PlexRequests.Api/ApiRequest.cs +++ b/PlexRequests.Api/ApiRequest.cs @@ -30,6 +30,9 @@ using System.Text; using System.Xml; using System.Xml.Serialization; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; + using PlexRequests.Api.Interfaces; using PlexRequests.Api.Models; @@ -75,11 +78,29 @@ namespace PlexRequests.Api throw new ApplicationException(message, response.ErrorException); } - var result = Deserialize(response.Content); + var result = DeserializeXml(response.Content); return result; } - public T Deserialize(string input) + public T ExecuteJson(IRestRequest request, Uri baseUri) where T : new() + { + var client = new RestClient { BaseUrl = baseUri }; + + var response = client.Execute(request); + + if (response.ErrorException != null) + { + var message = "Error retrieving response. Check inner details for more info."; + throw new ApplicationException(message, response.ErrorException); + } + + var json = JsonConvert.DeserializeObject(response.Content); + + return json; + + } + + public T DeserializeXml(string input) where T : class { var ser = new XmlSerializer(typeof(T)); diff --git a/PlexRequests.Api/CouchPotatoApi.cs b/PlexRequests.Api/CouchPotatoApi.cs new file mode 100644 index 000000000..aef772c01 --- /dev/null +++ b/PlexRequests.Api/CouchPotatoApi.cs @@ -0,0 +1,62 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: CouchPotatoApi.cs +// Created By: Jamie Rees +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// ************************************************************************/ +#endregion +using System; + +using Newtonsoft.Json.Linq; + +using PlexRequests.Api.Models.Movie; + +using RestSharp; + +namespace PlexRequests.Api +{ + public class CouchPotatoApi + { + public CouchPotatoApi() + { + Api = new ApiRequest(); + } + private ApiRequest Api { get; set; } + + public bool AddMovie(string imdbid, string apiKey, string title, string baseUrl) + { + var request = new RestRequest { Resource = "/api/{apikey}/movie.add?title={title}&identifier={imdbid}" }; + + request.AddUrlSegment("apikey", apiKey); + request.AddUrlSegment("imdbid", imdbid); + request.AddUrlSegment("title", title); + + var obj = Api.ExecuteJson(request, new Uri(baseUrl)); + if (obj.Count > 0) + { + var result = (bool)obj["success"]; + return result; + } + return false; + } + } +} \ No newline at end of file diff --git a/PlexRequests.Api/Models/Movie/CouchPotatoAdd.cs b/PlexRequests.Api/Models/Movie/CouchPotatoAdd.cs new file mode 100644 index 000000000..2e98a415f --- /dev/null +++ b/PlexRequests.Api/Models/Movie/CouchPotatoAdd.cs @@ -0,0 +1,83 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PlexRequests.Api.Models.Movie +{ + + public class CouchPotatoAdd + { + public Movie movie { get; set; } + public bool success { get; set; } + } + public class Rating + { + public List imdb { get; set; } + } + + + +public class Images +{ + public List disc_art { get; set; } + public List poster { get; set; } + public List extra_thumbs { get; set; } + public List poster_original { get; set; } + public List landscape { get; set; } + public string[] actors { get; set; } + public List backdrop_original { get; set; } + public List clear_art { get; set; } + public List logo { get; set; } + public List banner { get; set; } + public List backdrop { get; set; } + public List extra_fanart { get; set; } +} + +public class Info +{ + public Rating rating { get; set; } + public List genres { get; set; } + public int tmdb_id { get; set; } + public string plot { get; set; } + public string tagline { get; set; } + public string original_title { get; set; } + public string[] actor_roles { get; set; } + public bool via_imdb { get; set; } + public string mpaa { get; set; } + public bool via_tmdb { get; set; } + public List directors { get; set; } + public List titles { get; set; } + public string imdb { get; set; } + public int year { get; set; } + public Images images { get; set; } + public List actors { get; set; } + public List writers { get; set; } + public int runtime { get; set; } + public string type { get; set; } + public string released { get; set; } +} + +public class Identifiers +{ + public string imdb { get; set; } +} + +public class Movie +{ + public string status { get; set; } + public Info info { get; set; } + public string _t { get; set; } + public List releases { get; set; } + public string title { get; set; } + public string _rev { get; set; } + public string profile_id { get; set; } + public string _id { get; set; } + public object category_id { get; set; } + public string type { get; set; } + public Identifiers identifiers { get; set; } +} + + +} diff --git a/PlexRequests.Api/PlexRequests.Api.csproj b/PlexRequests.Api/PlexRequests.Api.csproj index 911371a69..4de1b21c1 100644 --- a/PlexRequests.Api/PlexRequests.Api.csproj +++ b/PlexRequests.Api/PlexRequests.Api.csproj @@ -61,6 +61,8 @@ + + diff --git a/PlexRequests.Core/SettingsService.cs b/PlexRequests.Core/SettingsService.cs index c2e9ec413..f9ba1a2a1 100644 --- a/PlexRequests.Core/SettingsService.cs +++ b/PlexRequests.Core/SettingsService.cs @@ -54,52 +54,8 @@ namespace PlexRequests.Core } private ICacheProvider Cache { get; set; } - public void AddRequest(int providerId, RequestType type) + public void AddRequest(int providerId, RequestedModel model) { - - var model = new RequestedModel(); - if (type == RequestType.Movie) - { - var movieApi = new TheMovieDbApi(); - var movieInfo = movieApi.GetMovieInformation(providerId).Result; - - model = new RequestedModel - { - ProviderId = movieInfo.Id, - Type = type, - Overview = movieInfo.Overview, - ImdbId = movieInfo.ImdbId, - PosterPath = "http://image.tmdb.org/t/p/w150/" + movieInfo.PosterPath, - Title = movieInfo.Title, - ReleaseDate = movieInfo.ReleaseDate ?? DateTime.MinValue, - Status = movieInfo.Status, - RequestedDate = DateTime.Now, - Approved = false - }; - } - else - { - var tvApi = new TheTvDbApi(); - var token = GetAuthToken(tvApi); - - var showInfo = tvApi.GetInformation(providerId, token).data; - - DateTime firstAir; - DateTime.TryParse(showInfo.firstAired, out firstAir); - - model = new RequestedModel - { - ProviderId = showInfo.id, - Type = type, - Overview = showInfo.overview, - PosterPath = "http://image.tmdb.org/t/p/w150/" + showInfo.banner, // This is incorrect - Title = showInfo.seriesName, - ReleaseDate = firstAir, - Status = showInfo.status, - RequestedDate = DateTime.Now, - Approved = false - }; - } var db = new DbConfiguration(new SqliteFactory()); var repo = new GenericRepository(db); @@ -122,9 +78,6 @@ namespace PlexRequests.Core repo.Delete(entity); } - private string GetAuthToken(TheTvDbApi api) - { - return Cache.GetOrSet(CacheKeys.TvDbToken, api.Authenticate, 50); - } + } } diff --git a/PlexRequests.UI/Modules/SearchModule.cs b/PlexRequests.UI/Modules/SearchModule.cs index b8c629c99..526e992d4 100644 --- a/PlexRequests.UI/Modules/SearchModule.cs +++ b/PlexRequests.UI/Modules/SearchModule.cs @@ -24,7 +24,7 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // ************************************************************************/ #endregion - +using System; using System.Collections.Generic; using System.Linq; using Nancy; @@ -32,6 +32,7 @@ using Nancy.Responses.Negotiation; using PlexRequests.Api; using PlexRequests.Core; +using PlexRequests.Core.SettingModels; using PlexRequests.Helpers; using PlexRequests.Store; using PlexRequests.UI.Models; @@ -40,8 +41,9 @@ namespace PlexRequests.UI.Modules { public class SearchModule : NancyModule { - public SearchModule(ICacheProvider cache) : base("search") + public SearchModule(ICacheProvider cache, ISettingsService cpSettings) : base("search") { + CpService = cpSettings; MovieApi = new TheMovieDbApi(); TvApi = new TheTvDbApi(); Cache = cache; @@ -60,6 +62,7 @@ namespace PlexRequests.UI.Modules private TheMovieDbApi MovieApi { get; } private TheTvDbApi TvApi { get; } private ICacheProvider Cache { get; } + private ISettingsService CpService { get; set; } private string AuthToken => Cache.GetOrSet(CacheKeys.TvDbToken, TvApi.Authenticate, 50); private Negotiator RequestLoad() @@ -136,9 +139,34 @@ namespace PlexRequests.UI.Modules { return Response.AsJson(new { Result = false, Message = "Movie has already been requested!" }); } - - s.AddRequest(movieId, RequestType.Movie); - return Response.AsJson(new { Result = true }); + var settings = CpService.GetSettings(); + var movieApi = new TheMovieDbApi(); + var movieInfo = movieApi.GetMovieInformation(movieId).Result; + + var model = new RequestedModel + { + ProviderId = movieInfo.Id, + Type = RequestType.Movie, + Overview = movieInfo.Overview, + ImdbId = movieInfo.ImdbId, + PosterPath = "http://image.tmdb.org/t/p/w150/" + movieInfo.PosterPath, + Title = movieInfo.Title, + ReleaseDate = movieInfo.ReleaseDate ?? DateTime.MinValue, + Status = movieInfo.Status, + RequestedDate = DateTime.Now, + Approved = false + }; + + var cp = new CouchPotatoApi(); + + var result = cp.AddMovie(model.ImdbId, settings.ApiKey, model.Title, settings.Ip); + if (result) + { + s.AddRequest(movieId, model); + + return Response.AsJson(new { Result = true }); + } + return Response.AsJson(new { Result = false, Message = "Something went wrong adding the movie to CouchPotato! Please check your settings." }); } /// @@ -155,8 +183,34 @@ namespace PlexRequests.UI.Modules { return Response.AsJson(new { Result = false, Message = "TV Show has already been requested!" }); } - s.AddRequest(showId, RequestType.TvShow); + + var tvApi = new TheTvDbApi(); + var token = GetAuthToken(tvApi); + + var showInfo = tvApi.GetInformation(showId, token).data; + + DateTime firstAir; + DateTime.TryParse(showInfo.firstAired, out firstAir); + + var model = new RequestedModel + { + ProviderId = showInfo.id, + Type = RequestType.TvShow, + Overview = showInfo.overview, + PosterPath = "http://image.tmdb.org/t/p/w150/" + showInfo.banner, // This is incorrect + Title = showInfo.seriesName, + ReleaseDate = firstAir, + Status = showInfo.status, + RequestedDate = DateTime.Now, + Approved = false + }; + + s.AddRequest(showId, model); return Response.AsJson(new {Result = true }); } + private string GetAuthToken(TheTvDbApi api) + { + return Cache.GetOrSet(CacheKeys.TvDbToken, api.Authenticate, 50); + } } } \ No newline at end of file