From 3dce7846725a4d5941e9e7ef48af2809fc7c6388 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Wed, 4 Jan 2012 16:41:42 -0800 Subject: [PATCH] XbmcProvider uses Json.net for reading/writing JSON. Updated for v3 of XBMC JSON API. --- .../ProviderTests/XbmcProviderTest.cs | 16 ++--- NzbDrone.Core/Model/Xbmc/Command.cs | 18 ------ NzbDrone.Core/Model/Xbmc/Params.cs | 11 ---- NzbDrone.Core/Model/Xbmc/TvShowResponse.cs | 13 ++++ NzbDrone.Core/Model/Xbmc/TvShowResult.cs | 5 +- NzbDrone.Core/NzbDrone.Core.csproj | 5 +- NzbDrone.Core/Providers/XbmcProvider.cs | 61 +++++++++++-------- 7 files changed, 61 insertions(+), 68 deletions(-) delete mode 100644 NzbDrone.Core/Model/Xbmc/Command.cs delete mode 100644 NzbDrone.Core/Model/Xbmc/Params.cs create mode 100644 NzbDrone.Core/Model/Xbmc/TvShowResponse.cs diff --git a/NzbDrone.Core.Test/ProviderTests/XbmcProviderTest.cs b/NzbDrone.Core.Test/ProviderTests/XbmcProviderTest.cs index ea9c049ec..7cc4769bb 100644 --- a/NzbDrone.Core.Test/ProviderTests/XbmcProviderTest.cs +++ b/NzbDrone.Core.Test/ProviderTests/XbmcProviderTest.cs @@ -498,7 +498,7 @@ namespace NzbDrone.Core.Test.ProviderTests var host = "localhost:8080"; var username = "xbmc"; var password = "xbmc"; - var serializedQuery = "{\"jsonrpc\":\"2.0\",\"method\":\"VideoLibrary.GetTvShows\",\"params\":{\"fields\":[\"file\",\"imdbnumber\"]},\"id\":10}"; + var expectedJson = "{\"jsonrpc\":\"2.0\",\"method\":\"VideoLibrary.GetTvShows\",\"params\":{\"properties\":[\"file\",\"imdbnumber\"]},\"id\":10}"; var tvshows = "{\"id\":10,\"jsonrpc\":\"2.0\",\"result\":{\"limits\":{\"end\":5,\"start\":0,\"total\":5},\"tvshows\":[{\"file\":\"smb://HOMESERVER/TV/7th Heaven/\",\"imdbnumber\":\"73928\",\"label\":\"7th Heaven\",\"tvshowid\":3},{\"file\":\"smb://HOMESERVER/TV/8 Simple Rules/\",\"imdbnumber\":\"78461\",\"label\":\"8 Simple Rules\",\"tvshowid\":4},{\"file\":\"smb://HOMESERVER/TV/24-7 Penguins-Capitals- Road to the NHL Winter Classic/\",\"imdbnumber\":\"213041\",\"label\":\"24/7 Penguins/Capitals: Road to the NHL Winter Classic\",\"tvshowid\":1},{\"file\":\"smb://HOMESERVER/TV/30 Rock/\",\"imdbnumber\":\"79488\",\"label\":\"30 Rock\",\"tvshowid\":2},{\"file\":\"smb://HOMESERVER/TV/90210/\",\"imdbnumber\":\"82716\",\"label\":\"90210\",\"tvshowid\":5}]}}"; var fakeSeries = Builder.CreateNew() @@ -507,17 +507,17 @@ namespace NzbDrone.Core.Test.ProviderTests .Build(); var fakeHttp = Mocker.GetMock(); - fakeHttp.Setup(s => s.PostCommand(host, username, password, serializedQuery)) + fakeHttp.Setup(s => s.PostCommand(host, username, password, It.Is(e => e.Replace(" ", "").Replace("\r\n", "").Replace("\t", "") == expectedJson.Replace(" ", "")))) .Returns(tvshows); var fakeEventClient = Mocker.GetMock(); fakeEventClient.Setup(s => s.SendAction("localhost", ActionType.ExecBuiltin, "ExecBuiltIn(UpdateLibrary(video,smb://HOMESERVER/TV/30 Rock/))")); //Act - Mocker.Resolve().UpdateWithJson(fakeSeries, host, username, password); + var result = Mocker.Resolve().UpdateWithJson(fakeSeries, host, username, password); //Assert - Mocker.VerifyAllMocks(); + result.Should().BeTrue(); } [Test] @@ -529,7 +529,7 @@ namespace NzbDrone.Core.Test.ProviderTests var host = "localhost:8080"; var username = "xbmc"; var password = "xbmc"; - var serializedQuery = "{\"jsonrpc\":\"2.0\",\"method\":\"VideoLibrary.GetTvShows\",\"params\":{\"fields\":[\"file\",\"imdbnumber\"]},\"id\":10}"; + var expectedJson = "{\"jsonrpc\":\"2.0\",\"method\":\"VideoLibrary.GetTvShows\",\"params\":{\"properties\":[\"file\",\"imdbnumber\"]},\"id\":10}"; var tvshows = "{\"id\":10,\"jsonrpc\":\"2.0\",\"result\":{\"limits\":{\"end\":5,\"start\":0,\"total\":5},\"tvshows\":[{\"file\":\"smb://HOMESERVER/TV/7th Heaven/\",\"imdbnumber\":\"73928\",\"label\":\"7th Heaven\",\"tvshowid\":3},{\"file\":\"smb://HOMESERVER/TV/8 Simple Rules/\",\"imdbnumber\":\"78461\",\"label\":\"8 Simple Rules\",\"tvshowid\":4},{\"file\":\"smb://HOMESERVER/TV/24-7 Penguins-Capitals- Road to the NHL Winter Classic/\",\"imdbnumber\":\"213041\",\"label\":\"24/7 Penguins/Capitals: Road to the NHL Winter Classic\",\"tvshowid\":1},{\"file\":\"smb://HOMESERVER/TV/90210/\",\"imdbnumber\":\"82716\",\"label\":\"90210\",\"tvshowid\":5}]}}"; var fakeSeries = Builder.CreateNew() @@ -538,17 +538,17 @@ namespace NzbDrone.Core.Test.ProviderTests .Build(); var fakeHttp = Mocker.GetMock(); - fakeHttp.Setup(s => s.PostCommand(host, username, password, serializedQuery)) + fakeHttp.Setup(s => s.PostCommand(host, username, password, It.Is(e => e.Replace(" ", "").Replace("\r\n", "").Replace("\t", "") == expectedJson.Replace(" ", "")))) .Returns(tvshows); var fakeEventClient = Mocker.GetMock(); fakeEventClient.Setup(s => s.SendAction("localhost", ActionType.ExecBuiltin, "ExecBuiltIn(UpdateLibrary(video))")); //Act - Mocker.Resolve().UpdateWithJson(fakeSeries, host, username, password); + var result = Mocker.Resolve().UpdateWithJson(fakeSeries, host, username, password); //Assert - Mocker.VerifyAllMocks(); + result.Should().BeTrue(); } } } \ No newline at end of file diff --git a/NzbDrone.Core/Model/Xbmc/Command.cs b/NzbDrone.Core/Model/Xbmc/Command.cs deleted file mode 100644 index c4c5a2d7b..000000000 --- a/NzbDrone.Core/Model/Xbmc/Command.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace NzbDrone.Core.Model.Xbmc -{ - public class Command - { - public string jsonrpc - { - get { return "2.0"; } - } - - public string method { get; set; } - public Params @params { get; set; } - public long id { get; set; } - } -} diff --git a/NzbDrone.Core/Model/Xbmc/Params.cs b/NzbDrone.Core/Model/Xbmc/Params.cs deleted file mode 100644 index c8d220c43..000000000 --- a/NzbDrone.Core/Model/Xbmc/Params.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace NzbDrone.Core.Model.Xbmc -{ - public class Params - { - public string[] fields { get; set; } - } -} diff --git a/NzbDrone.Core/Model/Xbmc/TvShowResponse.cs b/NzbDrone.Core/Model/Xbmc/TvShowResponse.cs new file mode 100644 index 000000000..9d31ff166 --- /dev/null +++ b/NzbDrone.Core/Model/Xbmc/TvShowResponse.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace NzbDrone.Core.Model.Xbmc +{ + public class TvShowResponse + { + public string Id { get; set; } + public string JsonRpc { get; set; } + public TvShowResult Result { get; set; } + } +} diff --git a/NzbDrone.Core/Model/Xbmc/TvShowResult.cs b/NzbDrone.Core/Model/Xbmc/TvShowResult.cs index 0a3ea3b3e..9cdc306f2 100644 --- a/NzbDrone.Core/Model/Xbmc/TvShowResult.cs +++ b/NzbDrone.Core/Model/Xbmc/TvShowResult.cs @@ -6,8 +6,7 @@ namespace NzbDrone.Core.Model.Xbmc { public class TvShowResult { - public string Id { get; set; } - public string JsonRpc { get; set; } - public Dictionary> Result { get; set; } + public Dictionary Limits { get; set; } + public List TvShows; } } diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index 2368aec7c..dac41c9ff 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -234,6 +234,7 @@ + @@ -267,9 +268,7 @@ - - - + diff --git a/NzbDrone.Core/Providers/XbmcProvider.cs b/NzbDrone.Core/Providers/XbmcProvider.cs index a32199431..5dbc9e8d7 100644 --- a/NzbDrone.Core/Providers/XbmcProvider.cs +++ b/NzbDrone.Core/Providers/XbmcProvider.cs @@ -4,6 +4,8 @@ using System.IO; using System.Linq; using System.Web.Script.Serialization; using System.Xml.Linq; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using Ninject; using NLog; using NzbDrone.Core.Model.Xbmc; @@ -56,8 +58,6 @@ namespace NzbDrone.Core.Providers Logger.Trace("Determining version of XBMC Host: {0}", host); var version = GetJsonVersion(host, username, password); - Logger.Trace("No video playing, proceeding with library update"); - //If Dharma if (version == 2) { @@ -97,6 +97,10 @@ namespace NzbDrone.Core.Providers UpdateWithJson(series, password, host, username); } + + //Log Version zero if check failed + else + Logger.Trace("Unknown version: [{0}], skipping.", version); } } @@ -232,16 +236,18 @@ namespace NzbDrone.Core.Providers try { - var command = new Command { id = 10, method = "JSONRPC.Version" }; - var serializer = new JavaScriptSerializer(); - var serialized = serializer.Serialize(command); - var response = _httpProvider.PostCommand(host, username, password, serialized); + var postJson = new JObject(); + postJson.Add(new JProperty("jsonrpc", "2.0")); + postJson.Add(new JProperty("method", "JSONRPC.Version")); + postJson.Add(new JProperty("id", 10)); + + var response = _httpProvider.PostCommand(host, username, password, postJson.ToString()); if (CheckForJsonError(response)) return version; Logger.Trace("Getting version from response"); - var result = serializer.Deserialize(response); + var result = JsonConvert.DeserializeObject(response); result.Result.TryGetValue("version", out version); } @@ -257,15 +263,17 @@ namespace NzbDrone.Core.Providers { try { - var command = new Command { id = 10, method = "Player.GetActivePlayers" }; - var serializer = new JavaScriptSerializer(); - var serialized = serializer.Serialize(command); - var response = _httpProvider.PostCommand(host, username, password, serialized); + 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(host, username, password, postJson.ToString()); if (CheckForJsonError(response)) return null; - var result = serializer.Deserialize(response); + var result = JsonConvert.DeserializeObject(response); return result.Result; } @@ -282,15 +290,17 @@ namespace NzbDrone.Core.Providers { try { - var command = new Command { id = 10, method = "Player.GetActivePlayers" }; - var serializer = new JavaScriptSerializer(); - var serialized = serializer.Serialize(command); - var response = _httpProvider.PostCommand(host, username, password, serialized); + 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(host, username, password, postJson.ToString()); if (CheckForJsonError(response)) return null; - var result = serializer.Deserialize(response); + var result = JsonConvert.DeserializeObject(response); return result.Result; } @@ -307,18 +317,19 @@ namespace NzbDrone.Core.Providers { try { - var fields = new string[] { "file", "imdbnumber" }; - var xbmcParams = new Params { fields = fields }; - var command = new Command { id = 10, method = "VideoLibrary.GetTvShows", @params = xbmcParams }; - var serializer = new JavaScriptSerializer(); - var serialized = serializer.Serialize(command); - var response = _httpProvider.PostCommand(host, username, password, serialized); + var postJson = new JObject(); + postJson.Add(new JProperty("jsonrpc", "2.0")); + postJson.Add(new JProperty("method", "VideoLibrary.GetTvShows")); + postJson.Add(new JProperty("params", new JObject { new JProperty("properties", new string[] { "file", "imdbnumber" }) })); + postJson.Add(new JProperty("id", 10)); + + var response = _httpProvider.PostCommand(host, username, password, postJson.ToString()); if (CheckForJsonError(response)) return null; - var result = serializer.Deserialize(response); - var shows = result.Result["tvshows"]; + var result = JsonConvert.DeserializeObject(response); + var shows = result.Result.TvShows; return shows; }