1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-04 10:02:40 +01:00

Fixed: Remove obsolete XBMC HTTP notification API

This commit is contained in:
ta264 2019-10-14 21:21:01 +01:00 committed by Qstick
parent 53ffc9867c
commit 1b34780b7e
12 changed files with 72 additions and 460 deletions

View File

@ -8,10 +8,10 @@
using NzbDrone.Core.Test.Framework;
using NzbDrone.Core.Movies;
namespace NzbDrone.Core.Test.NotificationTests.Xbmc.Json
namespace NzbDrone.Core.Test.NotificationTests.Xbmc
{
[TestFixture]
public class GetMoviePathFixture : CoreTest<JsonApiProvider>
public class GetMoviePathFixture : CoreTest<XbmcService>
{
private const string IMDB_ID = "tt67890";
private XbmcSettings _settings;

View File

@ -8,10 +8,10 @@
using NzbDrone.Core.Test.Framework;
using NzbDrone.Core.Movies;
namespace NzbDrone.Core.Test.NotificationTests.Xbmc.Json
namespace NzbDrone.Core.Test.NotificationTests.Xbmc
{
[TestFixture]
public class UpdateMovieFixture : CoreTest<JsonApiProvider>
public class UpdateMovieFixture : CoreTest<XbmcService>
{
private const string IMDB_ID = "tt67890";
private XbmcSettings _settings;

View File

@ -1,89 +0,0 @@
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Core.Notifications.Xbmc.Model;
namespace NzbDrone.Core.Test
{
public class XbmcVersionTests
{
[TestCase(6, 0, 0)]
[TestCase(5, 1, 0)]
[TestCase(5, 0, 1)]
public void Icomparer_greater_test(int major, int minor, int patch)
{
var first = new XbmcVersion(5, 0, 0);
var second = new XbmcVersion(major, minor, patch);
second.Should().BeGreaterThan(first);
}
[TestCase(4, 5, 5)]
[TestCase(5, 4, 5)]
[TestCase(5, 5, 4)]
public void Icomparer_lesser_test(int major, int minor, int patch)
{
var first = new XbmcVersion(5, 5, 5);
var second = new XbmcVersion(major, minor, patch);
second.Should().BeLessThan(first);
}
[Test]
public void equal_operand()
{
var first = new XbmcVersion(5, 0, 0);
var second = new XbmcVersion(5, 0, 0);
(first == second).Should().BeTrue();
(first >= second).Should().BeTrue();
(first <= second).Should().BeTrue();
}
[Test]
public void equal_operand_false()
{
var first = new XbmcVersion(5, 0, 0);
var second = new XbmcVersion(6, 0, 0);
(first == second).Should().BeFalse();
}
[Test]
public void not_equal_operand_false()
{
var first = new XbmcVersion(5, 0, 0);
var second = new XbmcVersion(5, 0, 0);
(first != second).Should().BeFalse();
}
[Test]
public void not_equal_operand_true()
{
var first = new XbmcVersion(5, 0, 0);
var second = new XbmcVersion(6, 0, 0);
(first != second).Should().BeTrue();
}
[Test]
public void greater_operand()
{
var first = new XbmcVersion(5, 0, 0);
var second = new XbmcVersion(6, 0, 0);
(first < second).Should().BeTrue();
(first <= second).Should().BeTrue();
}
[Test]
public void lesser_operand()
{
var first = new XbmcVersion(5, 0, 0);
var second = new XbmcVersion(6, 0, 0);
(second > first).Should().BeTrue();
(second >= first).Should().BeTrue();
}
}
}

View File

@ -1,13 +0,0 @@
using NzbDrone.Core.Notifications.Xbmc.Model;
using NzbDrone.Core.Movies;
namespace NzbDrone.Core.Notifications.Xbmc
{
public interface IApiProvider
{
void Notify(XbmcSettings settings, string title, string message);
void UpdateMovie(XbmcSettings settings, Movie movie);
void Clean(XbmcSettings settings);
bool CanHandle(XbmcVersion version);
}
}

View File

@ -1,15 +0,0 @@
using System;
namespace NzbDrone.Core.Notifications.Xbmc
{
public class InvalidXbmcVersionException : Exception
{
public InvalidXbmcVersionException()
{
}
public InvalidXbmcVersionException(string message) : base(message)
{
}
}
}

View File

@ -1,125 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using NLog;
using NzbDrone.Core.Notifications.Xbmc.Model;
using NzbDrone.Core.Movies;
using NzbDrone.Common.Disk;
using System.IO;
namespace NzbDrone.Core.Notifications.Xbmc
{
public class JsonApiProvider : IApiProvider
{
private readonly IXbmcJsonApiProxy _proxy;
private readonly Logger _logger;
public JsonApiProvider(IXbmcJsonApiProxy proxy, Logger logger)
{
_proxy = proxy;
_logger = logger;
}
public bool CanHandle(XbmcVersion version)
{
return version >= new XbmcVersion(5);
}
public void Notify(XbmcSettings settings, string title, string message)
{
_proxy.Notify(settings, title, message);
}
public void UpdateMovie(XbmcSettings settings, Movie movie)
{
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);
}
public void Clean(XbmcSettings settings)
{
if (!settings.AlwaysUpdate)
{
_logger.Debug("Determining if there are any active players on XBMC host: {0}", settings.Address);
var activePlayers = GetActivePlayers(settings);
if (activePlayers.Any(a => a.Type.Equals("video")))
{
_logger.Debug("Video is currently playing, skipping library cleaning");
return;
}
}
_proxy.CleanLibrary(settings);
}
public List<ActivePlayer> GetActivePlayers(XbmcSettings settings)
{
return _proxy.GetActivePlayers(settings);
}
public string GetMoviePath(XbmcSettings settings, Movie movie)
{
var allMovies = _proxy.GetMovies(settings);
if (!allMovies.Any())
{
_logger.Debug("No Movies returned from XBMC");
return null;
}
var matchingMovies = allMovies.FirstOrDefault(s =>
{
return s.ImdbNumber == movie.ImdbId || s.Label == movie.Title;
});
if (matchingMovies != null) return matchingMovies.File;
return null;
}
private void UpdateMovieLibrary(XbmcSettings settings, Movie movie)
{
try
{
var moviePath = GetMoviePath(settings, movie);
if (moviePath != null)
{
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);
}
else
{
_logger.Debug("Movie {0} doesn't exist on XBMC host: {1}, Updating Entire Library", movie,
settings.Address);
}
var response = _proxy.UpdateLibrary(settings, moviePath);
if (!response.Equals("OK", StringComparison.InvariantCultureIgnoreCase))
{
_logger.Debug("Failed to update library for: {0}", settings.Address);
}
}
catch (Exception ex)
{
_logger.Debug(ex, ex.Message);
}
}
}
}

View File

@ -1,11 +0,0 @@
using System.Collections.Generic;
namespace NzbDrone.Core.Notifications.Xbmc.Model
{
public class ActivePlayersDharmaResult
{
public string Id { get; set; }
public string JsonRpc { get; set; }
public Dictionary<string, bool> Result { get; set; }
}
}

View File

@ -2,7 +2,7 @@
namespace NzbDrone.Core.Notifications.Xbmc.Model
{
public class ActivePlayersEdenResult
public class ActivePlayersResult
{
public string Id { get; set; }
public string JsonRpc { get; set; }

View File

@ -1,11 +0,0 @@
using System.Collections.Generic;
namespace NzbDrone.Core.Notifications.Xbmc.Model
{
public class VersionResult
{
public string Id { get; set; }
public string JsonRpc { get; set; }
public Dictionary<string, int> Result { get; set; }
}
}

View File

@ -1,125 +0,0 @@
using System;
namespace NzbDrone.Core.Notifications.Xbmc.Model
{
public class XbmcVersion : IComparable<XbmcVersion>
{
public XbmcVersion()
{
}
public XbmcVersion(int major)
{
Major = major;
}
public XbmcVersion(int major, int minor, int patch)
{
Major = major;
Minor = minor;
Patch = patch;
}
public int Major { get; set; }
public int Minor { get; set; }
public int Patch { get; set; }
public int CompareTo(XbmcVersion other)
{
if(other.Major > Major)
return -1;
if(other.Major < Major)
return 1;
if (other.Minor > Minor)
return -1;
if (other.Minor < Minor)
return 1;
if (other.Patch > Patch)
return -1;
if (other.Patch < Patch)
return 1;
return 0;
}
public static bool operator !=(XbmcVersion x, XbmcVersion y)
{
return !(x == y);
}
public static bool operator ==(XbmcVersion x, XbmcVersion y)
{
var xObj = (object)x;
var yObj = (object)y;
if (xObj == null || yObj == null)
{
return xObj == yObj;
}
return x.CompareTo(y) == 0;
}
public static bool operator >(XbmcVersion x, XbmcVersion y)
{
return x.CompareTo(y) > 0;
}
public static bool operator <(XbmcVersion x, XbmcVersion y)
{
return x.CompareTo(y) < 0;
}
public static bool operator <=(XbmcVersion x, XbmcVersion y)
{
return x.CompareTo(y) <= 0;
}
public static bool operator >=(XbmcVersion x, XbmcVersion y)
{
return x.CompareTo(y) >= 0;
}
public override string ToString()
{
return string.Format("{0}.{1}.{2}", Major, Minor, Patch);
}
public override int GetHashCode()
{
unchecked // Overflow is fine, just wrap
{
int hash = 17;
hash = hash * 23 + Major.GetHashCode();
hash = hash * 23 + Minor.GetHashCode();
hash = hash * 23 + Patch.GetHashCode();
return hash;
}
}
public bool Equals(XbmcVersion other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return (Equals(other.Major, Major) && Equals(other.Minor, Minor) && Equals(other.Patch, Patch));
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != typeof(XbmcVersion)) return false;
return Equals((XbmcVersion)obj);
}
public static XbmcVersion NONE = new XbmcVersion(0, 0, 0);
public static XbmcVersion DHARMA = new XbmcVersion(2, 0, 0);
public static XbmcVersion EDEN = new XbmcVersion(4, 0, 0);
public static XbmcVersion FRODO = new XbmcVersion(6, 0, 0);
}
}

View File

@ -76,7 +76,7 @@ public List<ActivePlayer> GetActivePlayers(XbmcSettings settings)
var response = ProcessRequest(request, settings, "Player.GetActivePlayers");
return Json.Deserialize<ActivePlayersEdenResult>(response).Result;
return Json.Deserialize<ActivePlayersResult>(response).Result;
}
public List<XbmcMovie> GetMovies(XbmcSettings settings)

View File

@ -1,12 +1,9 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using FluentValidation.Results;
using Newtonsoft.Json.Linq;
using NLog;
using NzbDrone.Common.Cache;
using NzbDrone.Common.Serializer;
using NzbDrone.Core.Notifications.Xbmc.Model;
using NzbDrone.Common.Disk;
using NzbDrone.Core.Movies;
namespace NzbDrone.Core.Notifications.Xbmc
@ -22,95 +19,99 @@ public interface IXbmcService
public class XbmcService : IXbmcService
{
private readonly IXbmcJsonApiProxy _proxy;
private readonly IEnumerable<IApiProvider> _apiProviders;
private readonly Logger _logger;
private readonly ICached<XbmcVersion> _xbmcVersionCache;
public XbmcService(IXbmcJsonApiProxy proxy,
IEnumerable<IApiProvider> apiProviders,
ICacheManager cacheManager,
Logger logger)
{
_proxy = proxy;
_apiProviders = apiProviders;
_logger = logger;
_xbmcVersionCache = cacheManager.GetCache<XbmcVersion>(GetType());
}
public void Notify(XbmcSettings settings, string title, string message)
{
var provider = GetApiProvider(settings);
provider.Notify(settings, title, message);
_proxy.Notify(settings, title, message);
}
public void UpdateMovie(XbmcSettings settings, Movie movie)
{
var provider = GetApiProvider(settings);
provider.UpdateMovie(settings, movie);
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);
}
public void Clean(XbmcSettings settings)
{
var provider = GetApiProvider(settings);
provider.Clean(settings);
_proxy.CleanLibrary(settings);
}
private XbmcVersion GetJsonVersion(XbmcSettings settings)
public string GetMoviePath(XbmcSettings settings, Movie movie)
{
return _xbmcVersionCache.Get(settings.Address, () =>
var allMovies = _proxy.GetMovies(settings);
if (!allMovies.Any())
{
var response = _proxy.GetJsonVersion(settings);
_logger.Debug("Getting version from response: " + response);
var result = Json.Deserialize<XbmcJsonResult<JObject>>(response);
var versionObject = result.Result.Property("version");
if (versionObject.Value.Type == JTokenType.Integer)
{
return new XbmcVersion((int)versionObject.Value);
_logger.Debug("No Movies returned from XBMC");
return null;
}
if (versionObject.Value.Type == JTokenType.Object)
var matchingMovies = allMovies.FirstOrDefault(s =>
{
return Json.Deserialize<XbmcVersion>(versionObject.Value.ToString());
return s.ImdbNumber == movie.ImdbId || s.Label == movie.Title;
});
if (matchingMovies != null) return matchingMovies.File;
return null;
}
throw new InvalidCastException("Unknown Version structure!: " + versionObject);
}, TimeSpan.FromHours(12));
private void UpdateMovieLibrary(XbmcSettings settings, Movie movie)
{
try
{
var moviePath = GetMoviePath(settings, movie);
if (moviePath != null)
{
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);
}
private IApiProvider GetApiProvider(XbmcSettings settings)
else
{
var version = GetJsonVersion(settings);
var apiProvider = _apiProviders.SingleOrDefault(a => a.CanHandle(version));
if (apiProvider == null)
{
var message = string.Format("Invalid API Version: {0} for {1}", version, settings.Address);
throw new InvalidXbmcVersionException(message);
_logger.Debug("Movie {0} doesn't exist on XBMC host: {1}, Updating Entire Library", movie,
settings.Address);
}
return apiProvider;
var response = _proxy.UpdateLibrary(settings, moviePath);
if (!response.Equals("OK", StringComparison.InvariantCultureIgnoreCase))
{
_logger.Debug("Failed to update library for: {0}", settings.Address);
}
}
catch (Exception ex)
{
_logger.Debug(ex, ex.Message);
}
}
public ValidationFailure Test(XbmcSettings settings, string message)
{
_xbmcVersionCache.Clear();
try
{
_logger.Debug("Determining version of Host: {0}", settings.Address);
var version = GetJsonVersion(settings);
_logger.Debug("Version is: {0}", version);
if (version == new XbmcVersion(0))
{
throw new InvalidXbmcVersionException("Version received from XBMC is invalid, please correct your settings.");
}
Notify(settings, "Test Notification", message);
}
catch (Exception ex)