From 1b34780b7e49b88ef20d619db94bdfa60b39f135 Mon Sep 17 00:00:00 2001 From: ta264 Date: Mon, 14 Oct 2019 21:21:01 +0100 Subject: [PATCH] Fixed: Remove obsolete XBMC HTTP notification API --- .../Xbmc/{Json => }/GetMoviePathFixture.cs | 4 +- .../Xbmc/{Json => }/UpdateMovieFixture.cs | 4 +- src/NzbDrone.Core.Test/XbmcVersionTests.cs | 89 ------------ .../Notifications/Xbmc/IApiProvider.cs | 13 -- .../Xbmc/InvalidXbmcVersionException.cs | 15 -- .../Notifications/Xbmc/JsonApiProvider.cs | 125 ----------------- .../Xbmc/Model/ActivePlayersDharmaResult.cs | 11 -- ...rsEdenResult.cs => ActivePlayersResult.cs} | 4 +- .../Notifications/Xbmc/Model/VersionResult.cs | 11 -- .../Notifications/Xbmc/Model/XbmcVersion.cs | 125 ----------------- .../Notifications/Xbmc/XbmcJsonApiProxy.cs | 2 +- .../Notifications/Xbmc/XbmcService.cs | 129 +++++++++--------- 12 files changed, 72 insertions(+), 460 deletions(-) rename src/NzbDrone.Core.Test/NotificationTests/Xbmc/{Json => }/GetMoviePathFixture.cs (95%) rename src/NzbDrone.Core.Test/NotificationTests/Xbmc/{Json => }/UpdateMovieFixture.cs (94%) delete mode 100644 src/NzbDrone.Core.Test/XbmcVersionTests.cs delete mode 100644 src/NzbDrone.Core/Notifications/Xbmc/IApiProvider.cs delete mode 100644 src/NzbDrone.Core/Notifications/Xbmc/InvalidXbmcVersionException.cs delete mode 100644 src/NzbDrone.Core/Notifications/Xbmc/JsonApiProvider.cs delete mode 100644 src/NzbDrone.Core/Notifications/Xbmc/Model/ActivePlayersDharmaResult.cs rename src/NzbDrone.Core/Notifications/Xbmc/Model/{ActivePlayersEdenResult.cs => ActivePlayersResult.cs} (85%) delete mode 100644 src/NzbDrone.Core/Notifications/Xbmc/Model/VersionResult.cs delete mode 100644 src/NzbDrone.Core/Notifications/Xbmc/Model/XbmcVersion.cs diff --git a/src/NzbDrone.Core.Test/NotificationTests/Xbmc/Json/GetMoviePathFixture.cs b/src/NzbDrone.Core.Test/NotificationTests/Xbmc/GetMoviePathFixture.cs similarity index 95% rename from src/NzbDrone.Core.Test/NotificationTests/Xbmc/Json/GetMoviePathFixture.cs rename to src/NzbDrone.Core.Test/NotificationTests/Xbmc/GetMoviePathFixture.cs index d72e2c8f8..03f44d66c 100644 --- a/src/NzbDrone.Core.Test/NotificationTests/Xbmc/Json/GetMoviePathFixture.cs +++ b/src/NzbDrone.Core.Test/NotificationTests/Xbmc/GetMoviePathFixture.cs @@ -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 + public class GetMoviePathFixture : CoreTest { private const string IMDB_ID = "tt67890"; private XbmcSettings _settings; diff --git a/src/NzbDrone.Core.Test/NotificationTests/Xbmc/Json/UpdateMovieFixture.cs b/src/NzbDrone.Core.Test/NotificationTests/Xbmc/UpdateMovieFixture.cs similarity index 94% rename from src/NzbDrone.Core.Test/NotificationTests/Xbmc/Json/UpdateMovieFixture.cs rename to src/NzbDrone.Core.Test/NotificationTests/Xbmc/UpdateMovieFixture.cs index 9bc85faa0..c7751f041 100644 --- a/src/NzbDrone.Core.Test/NotificationTests/Xbmc/Json/UpdateMovieFixture.cs +++ b/src/NzbDrone.Core.Test/NotificationTests/Xbmc/UpdateMovieFixture.cs @@ -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 + public class UpdateMovieFixture : CoreTest { private const string IMDB_ID = "tt67890"; private XbmcSettings _settings; diff --git a/src/NzbDrone.Core.Test/XbmcVersionTests.cs b/src/NzbDrone.Core.Test/XbmcVersionTests.cs deleted file mode 100644 index 211bef487..000000000 --- a/src/NzbDrone.Core.Test/XbmcVersionTests.cs +++ /dev/null @@ -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(); - } - } -} diff --git a/src/NzbDrone.Core/Notifications/Xbmc/IApiProvider.cs b/src/NzbDrone.Core/Notifications/Xbmc/IApiProvider.cs deleted file mode 100644 index 37fb9b5ff..000000000 --- a/src/NzbDrone.Core/Notifications/Xbmc/IApiProvider.cs +++ /dev/null @@ -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); - } -} diff --git a/src/NzbDrone.Core/Notifications/Xbmc/InvalidXbmcVersionException.cs b/src/NzbDrone.Core/Notifications/Xbmc/InvalidXbmcVersionException.cs deleted file mode 100644 index 0a0a46da0..000000000 --- a/src/NzbDrone.Core/Notifications/Xbmc/InvalidXbmcVersionException.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; - -namespace NzbDrone.Core.Notifications.Xbmc -{ - public class InvalidXbmcVersionException : Exception - { - public InvalidXbmcVersionException() - { - } - - public InvalidXbmcVersionException(string message) : base(message) - { - } - } -} diff --git a/src/NzbDrone.Core/Notifications/Xbmc/JsonApiProvider.cs b/src/NzbDrone.Core/Notifications/Xbmc/JsonApiProvider.cs deleted file mode 100644 index a491efa64..000000000 --- a/src/NzbDrone.Core/Notifications/Xbmc/JsonApiProvider.cs +++ /dev/null @@ -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 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); - } - } - } -} diff --git a/src/NzbDrone.Core/Notifications/Xbmc/Model/ActivePlayersDharmaResult.cs b/src/NzbDrone.Core/Notifications/Xbmc/Model/ActivePlayersDharmaResult.cs deleted file mode 100644 index 49d942cc2..000000000 --- a/src/NzbDrone.Core/Notifications/Xbmc/Model/ActivePlayersDharmaResult.cs +++ /dev/null @@ -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 Result { get; set; } - } -} diff --git a/src/NzbDrone.Core/Notifications/Xbmc/Model/ActivePlayersEdenResult.cs b/src/NzbDrone.Core/Notifications/Xbmc/Model/ActivePlayersResult.cs similarity index 85% rename from src/NzbDrone.Core/Notifications/Xbmc/Model/ActivePlayersEdenResult.cs rename to src/NzbDrone.Core/Notifications/Xbmc/Model/ActivePlayersResult.cs index 2f3cc61d8..6868ae48b 100644 --- a/src/NzbDrone.Core/Notifications/Xbmc/Model/ActivePlayersEdenResult.cs +++ b/src/NzbDrone.Core/Notifications/Xbmc/Model/ActivePlayersResult.cs @@ -2,10 +2,10 @@ namespace NzbDrone.Core.Notifications.Xbmc.Model { - public class ActivePlayersEdenResult + public class ActivePlayersResult { public string Id { get; set; } public string JsonRpc { get; set; } public List Result { get; set; } } -} \ No newline at end of file +} diff --git a/src/NzbDrone.Core/Notifications/Xbmc/Model/VersionResult.cs b/src/NzbDrone.Core/Notifications/Xbmc/Model/VersionResult.cs deleted file mode 100644 index 01ad8c8e7..000000000 --- a/src/NzbDrone.Core/Notifications/Xbmc/Model/VersionResult.cs +++ /dev/null @@ -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 Result { get; set; } - } -} diff --git a/src/NzbDrone.Core/Notifications/Xbmc/Model/XbmcVersion.cs b/src/NzbDrone.Core/Notifications/Xbmc/Model/XbmcVersion.cs deleted file mode 100644 index c4fc4358e..000000000 --- a/src/NzbDrone.Core/Notifications/Xbmc/Model/XbmcVersion.cs +++ /dev/null @@ -1,125 +0,0 @@ -using System; - -namespace NzbDrone.Core.Notifications.Xbmc.Model -{ - public class XbmcVersion : IComparable - { - 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); - } -} diff --git a/src/NzbDrone.Core/Notifications/Xbmc/XbmcJsonApiProxy.cs b/src/NzbDrone.Core/Notifications/Xbmc/XbmcJsonApiProxy.cs index ca1ec0b0a..e85b5b4e1 100644 --- a/src/NzbDrone.Core/Notifications/Xbmc/XbmcJsonApiProxy.cs +++ b/src/NzbDrone.Core/Notifications/Xbmc/XbmcJsonApiProxy.cs @@ -76,7 +76,7 @@ public List GetActivePlayers(XbmcSettings settings) var response = ProcessRequest(request, settings, "Player.GetActivePlayers"); - return Json.Deserialize(response).Result; + return Json.Deserialize(response).Result; } public List GetMovies(XbmcSettings settings) diff --git a/src/NzbDrone.Core/Notifications/Xbmc/XbmcService.cs b/src/NzbDrone.Core/Notifications/Xbmc/XbmcService.cs index 002ef1eef..9f96a68c7 100644 --- a/src/NzbDrone.Core/Notifications/Xbmc/XbmcService.cs +++ b/src/NzbDrone.Core/Notifications/Xbmc/XbmcService.cs @@ -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 _apiProviders; private readonly Logger _logger; - private readonly ICached _xbmcVersionCache; - public XbmcService(IXbmcJsonApiProxy proxy, - IEnumerable apiProviders, - ICacheManager cacheManager, Logger logger) { _proxy = proxy; - _apiProviders = apiProviders; _logger = logger; - - _xbmcVersionCache = cacheManager.GetCache(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>(response); - - var versionObject = result.Result.Property("version"); - - if (versionObject.Value.Type == JTokenType.Integer) - { - return new XbmcVersion((int)versionObject.Value); - } - - if (versionObject.Value.Type == JTokenType.Object) - { - return Json.Deserialize(versionObject.Value.ToString()); - } - - throw new InvalidCastException("Unknown Version structure!: " + versionObject); - }, TimeSpan.FromHours(12)); - } - - private IApiProvider GetApiProvider(XbmcSettings settings) - { - 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("No Movies returned from XBMC"); + return null; } - return apiProvider; + 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); + } } 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) @@ -122,4 +123,4 @@ public ValidationFailure Test(XbmcSettings settings, string message) return null; } } -} \ No newline at end of file +}