2022-12-08 03:54:12 +00:00
|
|
|
using System;
|
2013-06-12 06:45:25 +00:00
|
|
|
using System.Linq;
|
2014-07-04 08:09:48 +00:00
|
|
|
using FluentValidation.Results;
|
2013-06-12 06:45:25 +00:00
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Core.Tv;
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Notifications.Xbmc
|
|
|
|
{
|
|
|
|
public interface IXbmcService
|
|
|
|
{
|
|
|
|
void Notify(XbmcSettings settings, string title, string message);
|
|
|
|
void Update(XbmcSettings settings, Series series);
|
|
|
|
void Clean(XbmcSettings settings);
|
2015-04-25 16:02:17 +00:00
|
|
|
ValidationFailure Test(XbmcSettings settings, string message);
|
2013-06-12 06:45:25 +00:00
|
|
|
}
|
|
|
|
|
2014-07-04 08:09:48 +00:00
|
|
|
public class XbmcService : IXbmcService
|
2013-06-12 06:45:25 +00:00
|
|
|
{
|
2014-08-01 06:30:30 +00:00
|
|
|
private readonly IXbmcJsonApiProxy _proxy;
|
2014-07-04 08:09:48 +00:00
|
|
|
private readonly Logger _logger;
|
2013-06-12 06:45:25 +00:00
|
|
|
|
2014-08-01 06:51:34 +00:00
|
|
|
public XbmcService(IXbmcJsonApiProxy proxy,
|
|
|
|
Logger logger)
|
2013-06-12 06:45:25 +00:00
|
|
|
{
|
2014-08-01 06:30:30 +00:00
|
|
|
_proxy = proxy;
|
2014-07-04 08:09:48 +00:00
|
|
|
_logger = logger;
|
2013-06-12 06:45:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Notify(XbmcSettings settings, string title, string message)
|
|
|
|
{
|
2021-07-29 03:13:06 +00:00
|
|
|
_proxy.Notify(settings, title, message);
|
2013-06-12 06:45:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Update(XbmcSettings settings, Series series)
|
|
|
|
{
|
2023-02-20 18:48:49 +00:00
|
|
|
if (CheckIfVideoPlayerOpen(settings))
|
2021-07-29 03:13:06 +00:00
|
|
|
{
|
2023-02-20 18:48:49 +00:00
|
|
|
_logger.Debug("Video is currently playing, skipping library update");
|
2021-07-29 03:13:06 +00:00
|
|
|
|
2023-02-20 18:48:49 +00:00
|
|
|
return;
|
2021-07-29 03:13:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UpdateLibrary(settings, series);
|
2013-06-12 06:45:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Clean(XbmcSettings settings)
|
|
|
|
{
|
2023-02-20 18:48:49 +00:00
|
|
|
if (CheckIfVideoPlayerOpen(settings))
|
|
|
|
{
|
|
|
|
_logger.Debug("Video is currently playing, skipping library clean");
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-29 03:13:06 +00:00
|
|
|
_proxy.CleanLibrary(settings);
|
2013-06-12 06:45:25 +00:00
|
|
|
}
|
|
|
|
|
2021-07-29 03:13:06 +00:00
|
|
|
public string GetSeriesPath(XbmcSettings settings, Series series)
|
2013-06-12 06:45:25 +00:00
|
|
|
{
|
2021-07-29 03:13:06 +00:00
|
|
|
var allSeries = _proxy.GetSeries(settings);
|
|
|
|
|
|
|
|
if (!allSeries.Any())
|
2013-06-12 06:45:25 +00:00
|
|
|
{
|
2023-02-20 18:48:49 +00:00
|
|
|
_logger.Debug("No TV shows returned from Kodi");
|
2021-07-29 03:13:06 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
var matchingSeries = allSeries.FirstOrDefault(s =>
|
|
|
|
{
|
2023-05-23 10:48:37 +00:00
|
|
|
int.TryParse(s.ImdbNumber, out var tvdbId);
|
2021-07-29 03:13:06 +00:00
|
|
|
|
|
|
|
return tvdbId == series.TvdbId || s.Label == series.Title;
|
|
|
|
});
|
2013-06-12 06:45:25 +00:00
|
|
|
|
2021-08-03 04:43:28 +00:00
|
|
|
if (matchingSeries != null)
|
|
|
|
{
|
|
|
|
return matchingSeries.File;
|
|
|
|
}
|
2013-06-12 06:45:25 +00:00
|
|
|
|
2021-07-29 03:13:06 +00:00
|
|
|
return null;
|
|
|
|
}
|
2014-08-01 06:51:34 +00:00
|
|
|
|
2021-07-29 03:13:06 +00:00
|
|
|
private void UpdateLibrary(XbmcSettings settings, Series series)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2022-12-08 03:54:12 +00:00
|
|
|
var seriesPath = GetSeriesPath(settings, series);
|
2021-07-29 03:13:06 +00:00
|
|
|
|
2022-12-08 03:54:12 +00:00
|
|
|
if (seriesPath != null)
|
2015-11-15 06:34:56 +00:00
|
|
|
{
|
2023-02-20 18:48:49 +00:00
|
|
|
_logger.Debug("Updating series {0} (Path: {1}) on Kodi host: {2}", series, seriesPath, settings.Address);
|
2015-11-15 06:34:56 +00:00
|
|
|
}
|
2021-07-29 03:13:06 +00:00
|
|
|
else
|
2015-11-15 06:34:56 +00:00
|
|
|
{
|
2023-02-20 18:48:49 +00:00
|
|
|
_logger.Debug("Series {0} doesn't exist on Kodi host: {1}, Updating Entire Library", series, settings.Address);
|
2015-11-15 06:34:56 +00:00
|
|
|
}
|
2013-06-12 06:45:25 +00:00
|
|
|
|
2022-12-08 03:54:12 +00:00
|
|
|
var response = _proxy.UpdateLibrary(settings, seriesPath);
|
2013-06-12 06:45:25 +00:00
|
|
|
|
2021-07-29 03:13:06 +00:00
|
|
|
if (!response.Equals("OK", StringComparison.InvariantCultureIgnoreCase))
|
|
|
|
{
|
|
|
|
_logger.Debug("Failed to update library for: {0}", settings.Address);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
2013-06-12 06:45:25 +00:00
|
|
|
{
|
2021-07-29 03:13:06 +00:00
|
|
|
_logger.Debug(ex, ex.Message);
|
2013-06-12 06:45:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-20 18:48:49 +00:00
|
|
|
private bool CheckIfVideoPlayerOpen(XbmcSettings settings)
|
|
|
|
{
|
|
|
|
if (settings.AlwaysUpdate)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_logger.Debug("Determining if there are any active players on Kodi host: {0}", settings.Address);
|
|
|
|
var activePlayers = _proxy.GetActivePlayers(settings);
|
|
|
|
|
|
|
|
return activePlayers.Any(a => a.Type.Equals("video"));
|
|
|
|
}
|
|
|
|
|
2015-04-25 16:02:17 +00:00
|
|
|
public ValidationFailure Test(XbmcSettings settings, string message)
|
2013-06-12 06:45:25 +00:00
|
|
|
{
|
2014-07-04 08:09:48 +00:00
|
|
|
try
|
|
|
|
{
|
2015-04-25 16:02:17 +00:00
|
|
|
Notify(settings, "Test Notification", message);
|
2014-07-04 08:09:48 +00:00
|
|
|
}
|
|
|
|
catch (Exception ex)
|
2013-06-13 02:55:11 +00:00
|
|
|
{
|
2017-01-05 23:32:17 +00:00
|
|
|
_logger.Error(ex, "Unable to send test message");
|
2014-07-04 08:09:48 +00:00
|
|
|
return new ValidationFailure("Host", "Unable to send test message");
|
2013-06-13 02:55:11 +00:00
|
|
|
}
|
|
|
|
|
2014-07-04 08:09:48 +00:00
|
|
|
return null;
|
2013-06-12 06:45:25 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-29 03:13:06 +00:00
|
|
|
}
|