2020-04-09 02:52:14 +00:00
|
|
|
using System.Collections.Generic;
|
2014-08-01 06:30:30 +00:00
|
|
|
using NLog;
|
2014-12-02 06:26:25 +00:00
|
|
|
using NzbDrone.Common.Extensions;
|
2020-04-09 02:52:14 +00:00
|
|
|
using NzbDrone.Common.Http;
|
2014-08-01 06:30:30 +00:00
|
|
|
using NzbDrone.Common.Serializer;
|
|
|
|
using NzbDrone.Core.Notifications.Xbmc.Model;
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Notifications.Xbmc
|
|
|
|
{
|
|
|
|
public interface IXbmcJsonApiProxy
|
|
|
|
{
|
2015-10-03 17:45:26 +00:00
|
|
|
string GetJsonVersion(XbmcSettings settings);
|
|
|
|
void Notify(XbmcSettings settings, string title, string message);
|
|
|
|
string UpdateLibrary(XbmcSettings settings, string path);
|
2014-08-01 06:30:30 +00:00
|
|
|
void CleanLibrary(XbmcSettings settings);
|
|
|
|
List<ActivePlayer> GetActivePlayers(XbmcSettings settings);
|
2017-11-10 15:11:09 +00:00
|
|
|
List<XbmcMovie> GetMovies(XbmcSettings settings);
|
2014-08-01 06:30:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public class XbmcJsonApiProxy : IXbmcJsonApiProxy
|
|
|
|
{
|
2020-04-09 02:52:14 +00:00
|
|
|
private readonly IHttpClient _httpClient;
|
2014-08-01 06:30:30 +00:00
|
|
|
private readonly Logger _logger;
|
|
|
|
|
2020-04-09 02:52:14 +00:00
|
|
|
public XbmcJsonApiProxy(IHttpClient httpClient, Logger logger)
|
2014-08-01 06:30:30 +00:00
|
|
|
{
|
2020-04-09 02:52:14 +00:00
|
|
|
_httpClient = httpClient;
|
2014-08-01 06:30:30 +00:00
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
2015-10-03 17:45:26 +00:00
|
|
|
public string GetJsonVersion(XbmcSettings settings)
|
2014-08-01 06:30:30 +00:00
|
|
|
{
|
2020-04-09 02:52:14 +00:00
|
|
|
return ProcessRequest(settings, "JSONRPC.Version");
|
2014-08-01 06:30:30 +00:00
|
|
|
}
|
|
|
|
|
2015-10-03 17:45:26 +00:00
|
|
|
public void Notify(XbmcSettings settings, string title, string message)
|
2014-08-01 06:30:30 +00:00
|
|
|
{
|
2020-04-09 02:52:14 +00:00
|
|
|
ProcessRequest(settings, "GUI.ShowNotification", title, message, "https://raw.github.com/Radarr/Radarr/develop/Logo/64.png", settings.DisplayTime * 1000);
|
2014-08-01 06:30:30 +00:00
|
|
|
}
|
|
|
|
|
2015-10-03 17:45:26 +00:00
|
|
|
public string UpdateLibrary(XbmcSettings settings, string path)
|
2014-08-01 06:30:30 +00:00
|
|
|
{
|
2020-05-22 22:02:21 +00:00
|
|
|
string response;
|
|
|
|
|
|
|
|
if (path == null)
|
|
|
|
{
|
|
|
|
response = ProcessRequest(settings, "VideoLibrary.Scan");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
response = ProcessRequest(settings, "VideoLibrary.Scan", path);
|
|
|
|
}
|
2014-08-01 06:30:30 +00:00
|
|
|
|
2015-10-03 17:45:26 +00:00
|
|
|
return Json.Deserialize<XbmcJsonResult<string>>(response).Result;
|
2014-08-01 06:30:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void CleanLibrary(XbmcSettings settings)
|
|
|
|
{
|
2020-04-09 02:52:14 +00:00
|
|
|
ProcessRequest(settings, "VideoLibrary.Clean");
|
2014-08-01 06:30:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public List<ActivePlayer> GetActivePlayers(XbmcSettings settings)
|
|
|
|
{
|
2020-04-09 02:52:14 +00:00
|
|
|
var response = ProcessRequest(settings, "Player.GetActivePlayers");
|
2014-08-01 06:30:30 +00:00
|
|
|
|
2019-10-14 20:21:01 +00:00
|
|
|
return Json.Deserialize<ActivePlayersResult>(response).Result;
|
2014-08-01 06:30:30 +00:00
|
|
|
}
|
|
|
|
|
2017-11-10 15:11:09 +00:00
|
|
|
public List<XbmcMovie> GetMovies(XbmcSettings settings)
|
2014-08-01 06:30:30 +00:00
|
|
|
{
|
2020-05-14 01:52:08 +00:00
|
|
|
var response = ProcessRequest(settings, "VideoLibrary.GetMovies", new List<string> { "file", "imdbnumber" });
|
2014-08-01 06:30:30 +00:00
|
|
|
|
2017-11-10 15:11:09 +00:00
|
|
|
return Json.Deserialize<MovieResponse>(response).Result.Movies;
|
2014-08-01 06:30:30 +00:00
|
|
|
}
|
|
|
|
|
2020-04-09 02:52:14 +00:00
|
|
|
private string ProcessRequest(XbmcSettings settings, string method, params object[] parameters)
|
2014-08-01 06:30:30 +00:00
|
|
|
{
|
2021-04-24 17:43:26 +00:00
|
|
|
var url = HttpRequestBuilder.BuildBaseUrl(settings.UseSsl, settings.Host, settings.Port, "jsonrpc");
|
2020-04-09 02:52:14 +00:00
|
|
|
var requestBuilder = new JsonRpcRequestBuilder(url, method, parameters);
|
2014-08-01 06:30:30 +00:00
|
|
|
|
2020-04-09 02:52:14 +00:00
|
|
|
requestBuilder.LogResponseContent = true;
|
2014-08-01 06:30:30 +00:00
|
|
|
|
2020-04-09 02:52:14 +00:00
|
|
|
var request = requestBuilder.Build();
|
2014-08-01 06:30:30 +00:00
|
|
|
|
|
|
|
if (!settings.Username.IsNullOrWhiteSpace())
|
|
|
|
{
|
2020-08-06 20:17:45 +00:00
|
|
|
request.Credentials = new BasicNetworkCredential(settings.Username, settings.Password);
|
2014-08-01 06:30:30 +00:00
|
|
|
}
|
|
|
|
|
2020-04-09 02:52:14 +00:00
|
|
|
var response = _httpClient.Execute(request);
|
|
|
|
_logger.Trace("Response: {0}", response.Content);
|
|
|
|
|
|
|
|
CheckForError(response);
|
|
|
|
|
|
|
|
return response.Content;
|
2014-08-01 06:30:30 +00:00
|
|
|
}
|
|
|
|
|
2020-04-09 02:52:14 +00:00
|
|
|
private void CheckForError(HttpResponse response)
|
2014-08-01 06:30:30 +00:00
|
|
|
{
|
2015-10-03 17:45:26 +00:00
|
|
|
if (string.IsNullOrWhiteSpace(response.Content))
|
2014-08-01 06:30:30 +00:00
|
|
|
{
|
|
|
|
throw new XbmcJsonException("Invalid response from XBMC, the response is not valid JSON");
|
|
|
|
}
|
|
|
|
|
2019-04-13 03:25:58 +00:00
|
|
|
_logger.Trace("Looking for error in response, {0}", response.Content);
|
|
|
|
|
2014-08-01 06:30:30 +00:00
|
|
|
if (response.Content.StartsWith("{\"error\""))
|
|
|
|
{
|
|
|
|
var error = Json.Deserialize<ErrorResult>(response.Content);
|
|
|
|
var code = error.Error["code"];
|
|
|
|
var message = error.Error["message"];
|
|
|
|
|
2015-10-03 17:45:26 +00:00
|
|
|
var errorMessage = string.Format("XBMC Json Error. Code = {0}, Message: {1}", code, message);
|
2014-08-01 06:30:30 +00:00
|
|
|
throw new XbmcJsonException(errorMessage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|