2013-06-12 06:45:25 +00:00
|
|
|
|
using System;
|
2019-10-14 20:21:01 +00:00
|
|
|
|
using System.IO;
|
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;
|
2019-10-14 20:21:01 +00:00
|
|
|
|
using NzbDrone.Common.Disk;
|
2018-03-14 20:41:36 +00:00
|
|
|
|
using NzbDrone.Core.Movies;
|
2013-06-12 06:45:25 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Notifications.Xbmc
|
|
|
|
|
{
|
|
|
|
|
public interface IXbmcService
|
|
|
|
|
{
|
|
|
|
|
void Notify(XbmcSettings settings, string title, string message);
|
2017-01-27 18:01:18 +00:00
|
|
|
|
void UpdateMovie(XbmcSettings settings, Movie movie);
|
2013-06-12 06:45:25 +00:00
|
|
|
|
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)
|
|
|
|
|
{
|
2019-10-14 20:21:01 +00:00
|
|
|
|
_proxy.Notify(settings, title, message);
|
2013-06-12 06:45:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-27 18:01:18 +00:00
|
|
|
|
public void UpdateMovie(XbmcSettings settings, Movie movie)
|
|
|
|
|
{
|
2019-10-14 20:21:01 +00:00
|
|
|
|
if (!settings.AlwaysUpdate)
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("Determining if there are any active players on XBMC host: {0}", settings.Address);
|
|
|
|
|
var activePlayers = _proxy.GetActivePlayers(settings);
|
|
|
|
|
|
|
|
|
|
if (activePlayers.Any(a => a.Type.Equals("video")))
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("Video is currently playing, skipping library update");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UpdateMovieLibrary(settings, movie);
|
2017-01-27 18:01:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-06-12 06:45:25 +00:00
|
|
|
|
public void Clean(XbmcSettings settings)
|
|
|
|
|
{
|
2019-10-14 20:21:01 +00:00
|
|
|
|
_proxy.CleanLibrary(settings);
|
2013-06-12 06:45:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-14 20:21:01 +00:00
|
|
|
|
public string GetMoviePath(XbmcSettings settings, Movie movie)
|
2013-06-12 06:45:25 +00:00
|
|
|
|
{
|
2019-10-14 20:21:01 +00:00
|
|
|
|
var allMovies = _proxy.GetMovies(settings);
|
|
|
|
|
|
|
|
|
|
if (!allMovies.Any())
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("No Movies returned from XBMC");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var matchingMovies = allMovies.FirstOrDefault(s =>
|
2013-06-12 06:45:25 +00:00
|
|
|
|
{
|
2019-10-14 20:21:01 +00:00
|
|
|
|
return s.ImdbNumber == movie.ImdbId || s.Label == movie.Title;
|
2013-06-12 06:45:25 +00:00
|
|
|
|
|
2019-10-14 20:21:01 +00:00
|
|
|
|
});
|
2013-06-12 06:45:25 +00:00
|
|
|
|
|
2019-10-14 20:21:01 +00:00
|
|
|
|
if (matchingMovies != null) return matchingMovies.File;
|
2014-08-01 06:51:34 +00:00
|
|
|
|
|
2019-10-14 20:21:01 +00:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateMovieLibrary(XbmcSettings settings, Movie movie)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var moviePath = GetMoviePath(settings, movie);
|
|
|
|
|
|
|
|
|
|
if (moviePath != null)
|
2015-11-15 06:34:56 +00:00
|
|
|
|
{
|
2019-10-14 20:21:01 +00:00
|
|
|
|
moviePath = new OsPath(moviePath).Directory.FullPath.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
|
|
|
|
|
_logger.Debug("Updating movie {0} (Path: {1}) on XBMC host: {2}", movie, moviePath, settings.Address);
|
2015-11-15 06:34:56 +00:00
|
|
|
|
}
|
2013-06-12 06:45:25 +00:00
|
|
|
|
|
2019-10-14 20:21:01 +00:00
|
|
|
|
else
|
2015-11-15 06:34:56 +00:00
|
|
|
|
{
|
2019-10-14 20:21:01 +00:00
|
|
|
|
_logger.Debug("Movie {0} doesn't exist on XBMC host: {1}, Updating Entire Library", movie,
|
|
|
|
|
settings.Address);
|
2015-11-15 06:34:56 +00:00
|
|
|
|
}
|
2013-06-12 06:45:25 +00:00
|
|
|
|
|
2019-10-14 20:21:01 +00:00
|
|
|
|
var response = _proxy.UpdateLibrary(settings, moviePath);
|
2013-06-12 06:45:25 +00:00
|
|
|
|
|
2019-10-14 20:21:01 +00:00
|
|
|
|
if (!response.Equals("OK", StringComparison.InvariantCultureIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("Failed to update library for: {0}", settings.Address);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-06-12 06:45:25 +00:00
|
|
|
|
|
2019-10-14 20:21:01 +00:00
|
|
|
|
catch (Exception ex)
|
2013-06-12 06:45:25 +00:00
|
|
|
|
{
|
2019-10-14 20:21:01 +00:00
|
|
|
|
_logger.Debug(ex, ex.Message);
|
2013-06-12 06:45:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
{
|
2019-04-13 03:25:58 +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
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-14 20:21:01 +00:00
|
|
|
|
}
|