|
|
@ -13,26 +13,23 @@ namespace NzbDrone.Core.Notifications.Xbmc
|
|
|
|
{
|
|
|
|
{
|
|
|
|
public class JsonApiProvider : IApiProvider
|
|
|
|
public class JsonApiProvider : IApiProvider
|
|
|
|
{
|
|
|
|
{
|
|
|
|
private readonly IHttpProvider _httpProvider;
|
|
|
|
private readonly IXbmcJsonApiProxy _proxy;
|
|
|
|
private readonly Logger _logger;
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
|
|
|
|
|
|
public JsonApiProvider(IHttpProvider httpProvider, Logger logger)
|
|
|
|
public JsonApiProvider(IXbmcJsonApiProxy proxy, Logger logger)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_httpProvider = httpProvider;
|
|
|
|
_proxy = proxy;
|
|
|
|
_logger = logger;
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Notify(XbmcSettings settings, string title, string message)
|
|
|
|
public bool CanHandle(XbmcVersion version)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var parameters = new JObject(
|
|
|
|
return version >= new XbmcVersion(5);
|
|
|
|
new JProperty("title", title),
|
|
|
|
}
|
|
|
|
new JProperty("message", message),
|
|
|
|
|
|
|
|
new JProperty("image", "https://raw.github.com/NzbDrone/NzbDrone/develop/Logo/64.png"),
|
|
|
|
|
|
|
|
new JProperty("displaytime", settings.DisplayTime * 1000));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var postJson = BuildJsonRequest("GUI.ShowNotification", parameters);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_httpProvider.PostCommand(settings.Address, settings.Username, settings.Password, postJson.ToString());
|
|
|
|
public void Notify(XbmcSettings settings, string title, string message)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
_proxy.Notify(settings, title, message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Update(XbmcSettings settings, Series series)
|
|
|
|
public void Update(XbmcSettings settings, Series series)
|
|
|
@ -40,7 +37,7 @@ namespace NzbDrone.Core.Notifications.Xbmc
|
|
|
|
if (!settings.AlwaysUpdate)
|
|
|
|
if (!settings.AlwaysUpdate)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_logger.Debug("Determining if there are any active players on XBMC host: {0}", settings.Address);
|
|
|
|
_logger.Debug("Determining if there are any active players on XBMC host: {0}", settings.Address);
|
|
|
|
var activePlayers = GetActivePlayers(settings);
|
|
|
|
var activePlayers = _proxy.GetActivePlayers(settings);
|
|
|
|
|
|
|
|
|
|
|
|
if (activePlayers.Any(a => a.Type.Equals("video")))
|
|
|
|
if (activePlayers.Any(a => a.Type.Equals("video")))
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -54,64 +51,17 @@ namespace NzbDrone.Core.Notifications.Xbmc
|
|
|
|
|
|
|
|
|
|
|
|
public void Clean(XbmcSettings settings)
|
|
|
|
public void Clean(XbmcSettings settings)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var postJson = BuildJsonRequest("VideoLibrary.Clean");
|
|
|
|
_proxy.CleanLibrary(settings);
|
|
|
|
|
|
|
|
|
|
|
|
_httpProvider.PostCommand(settings.Address, settings.Username, settings.Password, postJson.ToString());
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public List<ActivePlayer> GetActivePlayers(XbmcSettings settings)
|
|
|
|
public List<ActivePlayer> GetActivePlayers(XbmcSettings settings)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
try
|
|
|
|
return _proxy.GetActivePlayers(settings);
|
|
|
|
{
|
|
|
|
|
|
|
|
var postJson = new JObject();
|
|
|
|
|
|
|
|
postJson.Add(new JProperty("jsonrpc", "2.0"));
|
|
|
|
|
|
|
|
postJson.Add(new JProperty("method", "Player.GetActivePlayers"));
|
|
|
|
|
|
|
|
postJson.Add(new JProperty("id", 10));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var response = _httpProvider.PostCommand(settings.Address, settings.Username, settings.Password, postJson.ToString());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (CheckForError(response))
|
|
|
|
|
|
|
|
return new List<ActivePlayer>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var result = Json.Deserialize<ActivePlayersEdenResult>(response);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return result.Result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
_logger.DebugException(ex.Message, ex);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return new List<ActivePlayer>();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public bool CheckForError(string response)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
_logger.Debug("Looking for error in response: {0}", response);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (String.IsNullOrWhiteSpace(response))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
_logger.Debug("Invalid response from XBMC, the response is not valid JSON");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (response.StartsWith("{\"error\""))
|
|
|
|
public String GetSeriesPath(XbmcSettings settings, Series series)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var error = Json.Deserialize<ErrorResult>(response);
|
|
|
|
var allSeries = _proxy.GetSeries(settings);
|
|
|
|
var code = error.Error["code"];
|
|
|
|
|
|
|
|
var message = error.Error["message"];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_logger.Debug("XBMC Json Error. Code = {0}, Message: {1}", code, message);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public string GetSeriesPath(XbmcSettings settings, Series series)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var allSeries = GetSeries(settings);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!allSeries.Any())
|
|
|
|
if (!allSeries.Any())
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -126,11 +76,6 @@ namespace NzbDrone.Core.Notifications.Xbmc
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public bool CanHandle(XbmcVersion version)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return version >= new XbmcVersion(5);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void UpdateLibrary(XbmcSettings settings, Series series)
|
|
|
|
private void UpdateLibrary(XbmcSettings settings, Series series)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
try
|
|
|
|
try
|
|
|
@ -142,27 +87,17 @@ namespace NzbDrone.Core.Notifications.Xbmc
|
|
|
|
if (seriesPath != null)
|
|
|
|
if (seriesPath != null)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_logger.Debug("Updating series {0} (Path: {1}) on XBMC host: {2}", series, seriesPath, settings.Address);
|
|
|
|
_logger.Debug("Updating series {0} (Path: {1}) on XBMC host: {2}", series, seriesPath, settings.Address);
|
|
|
|
|
|
|
|
|
|
|
|
var parameters = new JObject(new JObject(new JProperty("directory", seriesPath)));
|
|
|
|
|
|
|
|
postJson = BuildJsonRequest("VideoLibrary.Scan", parameters);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_logger.Debug("Series {0} doesn't exist on XBMC host: {1}, Updating Entire Library", series,
|
|
|
|
_logger.Debug("Series {0} doesn't exist on XBMC host: {1}, Updating Entire Library", series,
|
|
|
|
settings.Address);
|
|
|
|
settings.Address);
|
|
|
|
|
|
|
|
|
|
|
|
postJson = BuildJsonRequest("VideoLibrary.Scan");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var response = _httpProvider.PostCommand(settings.Address, settings.Username, settings.Password, postJson.ToString());
|
|
|
|
var response = _proxy.UpdateLibrary(settings, null);
|
|
|
|
|
|
|
|
|
|
|
|
if (CheckForError(response)) return;
|
|
|
|
if (!response.Equals("OK", StringComparison.InvariantCultureIgnoreCase))
|
|
|
|
|
|
|
|
|
|
|
|
_logger.Debug(" from response");
|
|
|
|
|
|
|
|
var result = Json.Deserialize<XbmcJsonResult<String>>(response);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!result.Result.Equals("OK", StringComparison.InvariantCultureIgnoreCase))
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_logger.Debug("Failed to update library for: {0}", settings.Address);
|
|
|
|
_logger.Debug("Failed to update library for: {0}", settings.Address);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -173,51 +108,5 @@ namespace NzbDrone.Core.Notifications.Xbmc
|
|
|
|
_logger.DebugException(ex.Message, ex);
|
|
|
|
_logger.DebugException(ex.Message, ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private List<TvShow> GetSeries(XbmcSettings settings)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var properties = new JObject { new JProperty("properties", new[] { "file", "imdbnumber" }) };
|
|
|
|
|
|
|
|
var postJson = BuildJsonRequest("VideoLibrary.GetTvShows", properties);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var response = _httpProvider.PostCommand(settings.Address, settings.Username, settings.Password, postJson.ToString());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (CheckForError(response))
|
|
|
|
|
|
|
|
return new List<TvShow>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var result = Json.Deserialize<TvShowResponse>(response);
|
|
|
|
|
|
|
|
var shows = result.Result.TvShows;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return shows;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
_logger.DebugException(ex.Message, ex);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return new List<TvShow>();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private JObject BuildJsonRequest(string method)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return BuildJsonRequest(method, null);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private JObject BuildJsonRequest(string method, JObject parameters)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var postJson = new JObject();
|
|
|
|
|
|
|
|
postJson.Add(new JProperty("jsonrpc", "2.0"));
|
|
|
|
|
|
|
|
postJson.Add(new JProperty("method", method));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (parameters != null)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
postJson.Add(new JProperty("params", parameters));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
postJson.Add(new JProperty("id", 2));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return postJson;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|