XbmcProvider uses Json.net for reading/writing JSON.

Updated for v3 of XBMC JSON API.
This commit is contained in:
Mark McDowall 2012-01-04 16:41:42 -08:00
parent b42acb4b20
commit 3dce784672
7 changed files with 61 additions and 68 deletions

View File

@ -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<Series>.CreateNew()
@ -507,17 +507,17 @@ namespace NzbDrone.Core.Test.ProviderTests
.Build();
var fakeHttp = Mocker.GetMock<HttpProvider>();
fakeHttp.Setup(s => s.PostCommand(host, username, password, serializedQuery))
fakeHttp.Setup(s => s.PostCommand(host, username, password, It.Is<string>(e => e.Replace(" ", "").Replace("\r\n", "").Replace("\t", "") == expectedJson.Replace(" ", ""))))
.Returns(tvshows);
var fakeEventClient = Mocker.GetMock<EventClientProvider>();
fakeEventClient.Setup(s => s.SendAction("localhost", ActionType.ExecBuiltin, "ExecBuiltIn(UpdateLibrary(video,smb://HOMESERVER/TV/30 Rock/))"));
//Act
Mocker.Resolve<XbmcProvider>().UpdateWithJson(fakeSeries, host, username, password);
var result = Mocker.Resolve<XbmcProvider>().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<Series>.CreateNew()
@ -538,17 +538,17 @@ namespace NzbDrone.Core.Test.ProviderTests
.Build();
var fakeHttp = Mocker.GetMock<HttpProvider>();
fakeHttp.Setup(s => s.PostCommand(host, username, password, serializedQuery))
fakeHttp.Setup(s => s.PostCommand(host, username, password, It.Is<string>(e => e.Replace(" ", "").Replace("\r\n", "").Replace("\t", "") == expectedJson.Replace(" ", ""))))
.Returns(tvshows);
var fakeEventClient = Mocker.GetMock<EventClientProvider>();
fakeEventClient.Setup(s => s.SendAction("localhost", ActionType.ExecBuiltin, "ExecBuiltIn(UpdateLibrary(video))"));
//Act
Mocker.Resolve<XbmcProvider>().UpdateWithJson(fakeSeries, host, username, password);
var result = Mocker.Resolve<XbmcProvider>().UpdateWithJson(fakeSeries, host, username, password);
//Assert
Mocker.VerifyAllMocks();
result.Should().BeTrue();
}
}
}

View File

@ -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; }
}
}

View File

@ -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; }
}
}

View File

@ -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; }
}
}

View File

@ -6,8 +6,7 @@ namespace NzbDrone.Core.Model.Xbmc
{
public class TvShowResult
{
public string Id { get; set; }
public string JsonRpc { get; set; }
public Dictionary<string, List<TvShow>> Result { get; set; }
public Dictionary<string, int> Limits { get; set; }
public List<TvShow> TvShows;
}
}

View File

@ -234,6 +234,7 @@
<Compile Include="Model\Xbmc\ActionType.cs" />
<Compile Include="Model\Xbmc\ActivePlayersDharmaResult.cs" />
<Compile Include="Model\Xbmc\ActivePlayersEdenResult.cs" />
<Compile Include="Model\Xbmc\TvShowResult.cs" />
<Compile Include="Model\Xbmc\ErrorResult.cs" />
<Compile Include="Model\Xbmc\IconType.cs" />
<Compile Include="Providers\Converting\AtomicParsleyProvider.cs" />
@ -267,9 +268,7 @@
<Compile Include="Providers\TwitterProvider.cs" />
<Compile Include="Providers\UpdateProvider.cs" />
<Compile Include="Providers\Xbmc\ResourceManager.cs" />
<Compile Include="Model\Xbmc\TvShowResult.cs" />
<Compile Include="Model\Xbmc\Params.cs" />
<Compile Include="Model\Xbmc\Command.cs" />
<Compile Include="Model\Xbmc\TvShowResponse.cs" />
<Compile Include="Model\Xbmc\TvShow.cs" />
<Compile Include="Model\Xbmc\VersionResult.cs" />
<Compile Include="Providers\DiskScanProvider.cs" />

View File

@ -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<VersionResult>(response);
var result = JsonConvert.DeserializeObject<VersionResult>(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<ActivePlayersDharmaResult>(response);
var result = JsonConvert.DeserializeObject<ActivePlayersDharmaResult>(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<ActivePlayersEdenResult>(response);
var result = JsonConvert.DeserializeObject<ActivePlayersEdenResult>(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<TvShowResult>(response);
var shows = result.Result["tvshows"];
var result = JsonConvert.DeserializeObject<TvShowResponse>(response);
var shows = result.Result.TvShows;
return shows;
}