Firstpass integrating with CouchPotato

pull/13/head
tidusjar 8 years ago
parent 37aa6b938d
commit 73635e06c3

@ -34,5 +34,6 @@ namespace PlexRequests.Api.Interfaces
{
T Execute<T>(IRestRequest request, Uri baseUri) where T : new();
T ExecuteXml<T>(IRestRequest request, Uri baseUri) where T : class;
T ExecuteJson<T>(IRestRequest request, Uri baseUri) where T : new();
}
}

@ -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<T>(response.Content);
var result = DeserializeXml<T>(response.Content);
return result;
}
public T Deserialize<T>(string input)
public T ExecuteJson<T>(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<T>(response.Content);
return json;
}
public T DeserializeXml<T>(string input)
where T : class
{
var ser = new XmlSerializer(typeof(T));

@ -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<JObject>(request, new Uri(baseUrl));
if (obj.Count > 0)
{
var result = (bool)obj["success"];
return result;
}
return false;
}
}
}

@ -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<double> imdb { get; set; }
}
public class Images
{
public List<object> disc_art { get; set; }
public List<string> poster { get; set; }
public List<object> extra_thumbs { get; set; }
public List<string> poster_original { get; set; }
public List<object> landscape { get; set; }
public string[] actors { get; set; }
public List<string> backdrop_original { get; set; }
public List<object> clear_art { get; set; }
public List<object> logo { get; set; }
public List<object> banner { get; set; }
public List<string> backdrop { get; set; }
public List<object> extra_fanart { get; set; }
}
public class Info
{
public Rating rating { get; set; }
public List<string> 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<string> directors { get; set; }
public List<string> titles { get; set; }
public string imdb { get; set; }
public int year { get; set; }
public Images images { get; set; }
public List<string> actors { get; set; }
public List<string> 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<object> 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; }
}
}

@ -61,6 +61,8 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ApiRequest.cs" />
<Compile Include="CouchPotatoApi.cs" />
<Compile Include="Models\Movie\CouchPotatoAdd.cs" />
<Compile Include="Models\PlexAuthentication.cs" />
<Compile Include="Models\PlexError.cs" />
<Compile Include="Models\PlexFriends.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<RequestedModel>(db);
@ -122,9 +78,6 @@ namespace PlexRequests.Core
repo.Delete(entity);
}
private string GetAuthToken(TheTvDbApi api)
{
return Cache.GetOrSet(CacheKeys.TvDbToken, api.Authenticate, 50);
}
}
}

@ -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<CouchPotatoSettings> 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<CouchPotatoSettings> 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." });
}
/// <summary>
@ -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);
}
}
}
Loading…
Cancel
Save