parent
aa66358725
commit
c0d77af26d
@ -1,70 +0,0 @@
|
||||
using System.Linq;
|
||||
using FluentAssertions;
|
||||
using NUnit.Framework;
|
||||
using NzbDrone.Common.Http;
|
||||
using NzbDrone.Core.Notifications.Xbmc;
|
||||
using NzbDrone.Core.Test.Framework;
|
||||
|
||||
namespace NzbDrone.Core.Test.NotificationTests.Xbmc.Http
|
||||
{
|
||||
[TestFixture]
|
||||
public class ActivePlayersFixture : CoreTest<HttpApiProvider>
|
||||
{
|
||||
private XbmcSettings _settings;
|
||||
private string _expectedUrl;
|
||||
|
||||
private void WithNoActivePlayers()
|
||||
{
|
||||
Mocker.GetMock<IHttpProvider>()
|
||||
.Setup(s => s.DownloadString(_expectedUrl, _settings.Username, _settings.Password))
|
||||
.Returns("<html><li>Filename:[Nothing Playing]</html>");
|
||||
}
|
||||
|
||||
private void WithVideoPlayerActive()
|
||||
{
|
||||
var activePlayers = @"<html><li>Filename:C:\Test\TV\2 Broke Girls\Season 01\2 Broke Girls - S01E01 - Pilot [SDTV].avi" +
|
||||
"<li>PlayStatus:Playing<li>VideoNo:0<li>Type:Audio<li>Thumb:special://masterprofile/Thumbnails/Video/a/auto-a664d5a2.tbn" +
|
||||
"<li>Time:00:06<li>Duration:21:35<li>Percentage:0<li>File size:183182590<li>Changed:True</html>";
|
||||
|
||||
Mocker.GetMock<IHttpProvider>()
|
||||
.Setup(s => s.DownloadString(_expectedUrl, _settings.Username, _settings.Password))
|
||||
.Returns(activePlayers);
|
||||
}
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
_settings = new XbmcSettings
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 8080,
|
||||
Username = "xbmc",
|
||||
Password = "xbmc",
|
||||
AlwaysUpdate = false,
|
||||
CleanLibrary = false,
|
||||
UpdateLibrary = true
|
||||
};
|
||||
|
||||
_expectedUrl = string.Format("http://{0}/xbmcCmds/xbmcHttp?command={1}", _settings.Address, "getcurrentlyplaying");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void _should_be_empty_when_no_active_players()
|
||||
{
|
||||
WithNoActivePlayers();
|
||||
|
||||
Subject.GetActivePlayers(_settings).Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_have_active_audio_player()
|
||||
{
|
||||
WithVideoPlayerActive();
|
||||
|
||||
var result = Subject.GetActivePlayers(_settings);
|
||||
|
||||
result.Should().HaveCount(1);
|
||||
result.First().Type.Should().Be("audio");
|
||||
}
|
||||
}
|
||||
}
|
@ -1,94 +0,0 @@
|
||||
using FluentAssertions;
|
||||
using NUnit.Framework;
|
||||
using NzbDrone.Common.Http;
|
||||
using NzbDrone.Core.Notifications.Xbmc;
|
||||
using NzbDrone.Core.Test.Framework;
|
||||
using NzbDrone.Core.Music;
|
||||
|
||||
namespace NzbDrone.Core.Test.NotificationTests.Xbmc.Http
|
||||
{
|
||||
[TestFixture]
|
||||
public class GetArtistPathFixture : CoreTest<HttpApiProvider>
|
||||
{
|
||||
private XbmcSettings _settings;
|
||||
private Artist _artist;
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
_settings = new XbmcSettings
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 8080,
|
||||
Username = "xbmc",
|
||||
Password = "xbmc",
|
||||
AlwaysUpdate = false,
|
||||
CleanLibrary = false,
|
||||
UpdateLibrary = true
|
||||
};
|
||||
|
||||
_artist = new Artist
|
||||
{
|
||||
ForeignArtistId = "9f4e41c3-2648-428e-b8c7-dc10465b49ac",
|
||||
Name = "Shawn Desman"
|
||||
};
|
||||
|
||||
const string setResponseUrl = "http://localhost:8080/xbmcCmds/xbmcHttp?command=SetResponseFormat(webheader;false;webfooter;false;header;<xml>;footer;</xml>;opentag;<tag>;closetag;</tag>;closefinaltag;false)";
|
||||
const string resetResponseUrl = "http://localhost:8080/xbmcCmds/xbmcHttp?command=SetResponseFormat()";
|
||||
|
||||
Mocker.GetMock<IHttpProvider>()
|
||||
.Setup(s => s.DownloadString(setResponseUrl, _settings.Username, _settings.Password))
|
||||
.Returns("<xml><tag>OK</xml>");
|
||||
|
||||
Mocker.GetMock<IHttpProvider>()
|
||||
.Setup(s => s.DownloadString(resetResponseUrl, _settings.Username, _settings.Password))
|
||||
.Returns(@"<html>
|
||||
<li>OK
|
||||
</html>");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_get_artist_path()
|
||||
{
|
||||
const string queryResult = @"<xml><record><field>smb://xbmc:xbmc@HOMESERVER/Music/Shawn Desman/</field></record></xml>";
|
||||
var query = string.Format("http://localhost:8080/xbmcCmds/xbmcHttp?command=QueryMusicDatabase(select path.strPath from path, artist, artistlinkpath where artist.c12 = 9f4e41c3-2648-428e-b8c7-dc10465b49ac and artistlinkpath.idArtist = artist.idArtist and artistlinkpath.idPath = path.idPath)");
|
||||
|
||||
Mocker.GetMock<IHttpProvider>()
|
||||
.Setup(s => s.DownloadString(query, _settings.Username, _settings.Password))
|
||||
.Returns(queryResult);
|
||||
|
||||
Subject.GetArtistPath(_settings, _artist)
|
||||
.Should().Be("smb://xbmc:xbmc@HOMESERVER/Music/Shawn Desman/");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_get_null_for_artist_path()
|
||||
{
|
||||
const string queryResult = @"<xml></xml>";
|
||||
var query = string.Format("http://localhost:8080/xbmcCmds/xbmcHttp?command=QueryMusicDatabase(select path.strPath from path, artist, artistlinkpath where artist.c12 = 9f4e41c3-2648-428e-b8c7-dc10465b49ac and artistlinkpath.idArtist = artist.idArtist and artistlinkpath.idPath = path.idPath)");
|
||||
|
||||
Mocker.GetMock<IHttpProvider>()
|
||||
.Setup(s => s.DownloadString(query, _settings.Username, _settings.Password))
|
||||
.Returns(queryResult);
|
||||
|
||||
|
||||
Subject.GetArtistPath(_settings, _artist)
|
||||
.Should().BeNull();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_get_artist_path_with_special_characters_in_it()
|
||||
{
|
||||
const string queryResult = @"<xml><record><field>smb://xbmc:xbmc@HOMESERVER/Music/-wumpscut-/</field></record></xml>";
|
||||
var query = string.Format("http://localhost:8080/xbmcCmds/xbmcHttp?command=QueryMusicDatabase(select path.strPath from path, artist, artistlinkpath where artist.c12 = 9f4e41c3-2648-428e-b8c7-dc10465b49ac and artistlinkpath.idArtist = artist.idArtist and artistlinkpath.idPath = path.idPath)");
|
||||
|
||||
Mocker.GetMock<IHttpProvider>()
|
||||
.Setup(s => s.DownloadString(query, _settings.Username, _settings.Password))
|
||||
.Returns(queryResult);
|
||||
|
||||
|
||||
Subject.GetArtistPath(_settings, _artist)
|
||||
.Should().Be("smb://xbmc:xbmc@HOMESERVER/Music/-wumpscut-/");
|
||||
}
|
||||
}
|
||||
}
|
@ -1,75 +0,0 @@
|
||||
using FizzWare.NBuilder;
|
||||
using NUnit.Framework;
|
||||
using NzbDrone.Common.Http;
|
||||
using NzbDrone.Core.Notifications.Xbmc;
|
||||
using NzbDrone.Core.Test.Framework;
|
||||
using NzbDrone.Core.Music;
|
||||
|
||||
namespace NzbDrone.Core.Test.NotificationTests.Xbmc.Http
|
||||
{
|
||||
[TestFixture]
|
||||
public class UpdateFixture : CoreTest<HttpApiProvider>
|
||||
{
|
||||
private XbmcSettings _settings;
|
||||
private string _artistQueryUrl = "http://localhost:8080/xbmcCmds/xbmcHttp?command=QueryMusicDatabase(select path.strPath from path, artist, artistlinkpath where artist.c12 = 9f4e41c3-2648-428e-b8c7-dc10465b49ac and artistlinkpath.idArtist = artist.idArtist and artistlinkpath.idPath = path.idPath)";
|
||||
private Artist _fakeArtist;
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
_settings = new XbmcSettings
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 8080,
|
||||
Username = "xbmc",
|
||||
Password = "xbmc",
|
||||
AlwaysUpdate = false,
|
||||
CleanLibrary = false,
|
||||
UpdateLibrary = true
|
||||
};
|
||||
|
||||
_fakeArtist = Builder<Artist>.CreateNew()
|
||||
.With(s => s.ForeignArtistId = "9f4e41c3-2648-428e-b8c7-dc10465b49ac")
|
||||
.With(s => s.Name = "Shawn Desman")
|
||||
.Build();
|
||||
}
|
||||
|
||||
private void WithArtistPath()
|
||||
{
|
||||
Mocker.GetMock<IHttpProvider>()
|
||||
.Setup(s => s.DownloadString(_artistQueryUrl, _settings.Username, _settings.Password))
|
||||
.Returns("<xml><record><field>smb://xbmc:xbmc@HOMESERVER/Music/Shawn Desman/</field></record></xml>");
|
||||
}
|
||||
|
||||
private void WithoutArtistPath()
|
||||
{
|
||||
Mocker.GetMock<IHttpProvider>()
|
||||
.Setup(s => s.DownloadString(_artistQueryUrl, _settings.Username, _settings.Password))
|
||||
.Returns("<xml></xml>");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_update_using_artist_path()
|
||||
{
|
||||
WithArtistPath();
|
||||
const string url = "http://localhost:8080/xbmcCmds/xbmcHttp?command=ExecBuiltIn(UpdateLibrary(music,smb://xbmc:xbmc@HOMESERVER/Music/Shawn Desman/))";
|
||||
|
||||
Mocker.GetMock<IHttpProvider>().Setup(s => s.DownloadString(url, _settings.Username, _settings.Password));
|
||||
|
||||
Subject.Update(_settings, _fakeArtist);
|
||||
Mocker.VerifyAllMocks();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_update_all_paths_when_artist_path_not_found()
|
||||
{
|
||||
WithoutArtistPath();
|
||||
const string url = "http://localhost:8080/xbmcCmds/xbmcHttp?command=ExecBuiltIn(UpdateLibrary(music))";
|
||||
|
||||
Mocker.GetMock<IHttpProvider>().Setup(s => s.DownloadString(url, _settings.Username, _settings.Password));
|
||||
|
||||
Subject.Update(_settings, _fakeArtist);
|
||||
Mocker.VerifyAllMocks();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,187 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Xml.Linq;
|
||||
using NLog;
|
||||
using NzbDrone.Common.Http;
|
||||
using NzbDrone.Core.Notifications.Xbmc.Model;
|
||||
using NzbDrone.Core.Music;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Xbmc
|
||||
{
|
||||
public class HttpApiProvider : IApiProvider
|
||||
{
|
||||
private readonly IHttpProvider _httpProvider;
|
||||
private readonly Logger _logger;
|
||||
|
||||
public HttpApiProvider(IHttpProvider httpProvider, Logger logger)
|
||||
{
|
||||
_httpProvider = httpProvider;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public bool CanHandle(XbmcVersion version)
|
||||
{
|
||||
return version < new XbmcVersion(5);
|
||||
}
|
||||
|
||||
public void Notify(XbmcSettings settings, string title, string message)
|
||||
{
|
||||
var notification = string.Format("Notification({0},{1},{2},{3})", title, message, settings.DisplayTime * 1000, "https://raw.github.com/Lidarr/Lidarr/develop/Logo/64.png");
|
||||
var command = BuildExecBuiltInCommand(notification);
|
||||
|
||||
SendCommand(settings, command);
|
||||
}
|
||||
|
||||
public void Update(XbmcSettings settings, Artist artist)
|
||||
{
|
||||
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("audio")))
|
||||
{
|
||||
_logger.Debug("Audio is currently playing, skipping library update");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
UpdateLibrary(settings, artist);
|
||||
}
|
||||
|
||||
public void Clean(XbmcSettings settings)
|
||||
{
|
||||
const string cleanMusicLibrary = "CleanLibrary(music)";
|
||||
var command = BuildExecBuiltInCommand(cleanMusicLibrary);
|
||||
|
||||
SendCommand(settings, command);
|
||||
}
|
||||
|
||||
internal List<ActivePlayer> GetActivePlayers(XbmcSettings settings)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = new List<ActivePlayer>();
|
||||
var response = SendCommand(settings, "getcurrentlyplaying");
|
||||
|
||||
if (response.Contains("<li>Filename:[Nothing Playing]")) return new List<ActivePlayer>();
|
||||
if (response.Contains("<li>Type:Audio")) result.Add(new ActivePlayer(1, "audio"));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Debug(ex, ex.Message);
|
||||
}
|
||||
|
||||
return new List<ActivePlayer>();
|
||||
}
|
||||
|
||||
internal string GetArtistPath(XbmcSettings settings, Artist artist)
|
||||
{
|
||||
var query =
|
||||
string.Format(
|
||||
"select path.strPath from path, artist, artistlinkpath where artist.c12 = {0} and artistlinkpath.idArtist = artist.idArtist and artistlinkpath.idPath = path.idPath",
|
||||
artist.Metadata.Value.ForeignArtistId);
|
||||
var command = string.Format("QueryMusicDatabase({0})", query);
|
||||
|
||||
const string setResponseCommand =
|
||||
"SetResponseFormat(webheader;false;webfooter;false;header;<xml>;footer;</xml>;opentag;<tag>;closetag;</tag>;closefinaltag;false)";
|
||||
const string resetResponseCommand = "SetResponseFormat()";
|
||||
|
||||
SendCommand(settings, setResponseCommand);
|
||||
var response = SendCommand(settings, command);
|
||||
SendCommand(settings, resetResponseCommand);
|
||||
|
||||
if (string.IsNullOrEmpty(response))
|
||||
return string.Empty;
|
||||
|
||||
var xDoc = XDocument.Load(new StringReader(response.Replace("&", "&")));
|
||||
var xml = xDoc.Descendants("xml").Select(x => x).FirstOrDefault();
|
||||
|
||||
if (xml == null)
|
||||
return null;
|
||||
|
||||
var field = xml.Descendants("field").FirstOrDefault();
|
||||
|
||||
if (field == null)
|
||||
return null;
|
||||
|
||||
return field.Value;
|
||||
}
|
||||
|
||||
internal bool CheckForError(string response)
|
||||
{
|
||||
_logger.Debug("Looking for error in response: {0}", response);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(response))
|
||||
{
|
||||
_logger.Debug("Invalid response from XBMC, the response is not valid JSON");
|
||||
return true;
|
||||
}
|
||||
|
||||
var errorIndex = response.IndexOf("Error", StringComparison.InvariantCultureIgnoreCase);
|
||||
|
||||
if (errorIndex > -1)
|
||||
{
|
||||
var errorMessage = response.Substring(errorIndex + 6);
|
||||
errorMessage = errorMessage.Substring(0, errorMessage.IndexOfAny(new char[] { '<', ';' }));
|
||||
|
||||
_logger.Debug("Error found in response: {0}", errorMessage);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private void UpdateLibrary(XbmcSettings settings, Artist artist)
|
||||
{
|
||||
try
|
||||
{
|
||||
_logger.Debug("Sending Update DB Request to XBMC Host: {0}", settings.Address);
|
||||
var xbmcArtistPath = GetArtistPath(settings, artist);
|
||||
|
||||
//If the path is found update it, else update the whole library
|
||||
if (!string.IsNullOrEmpty(xbmcArtistPath))
|
||||
{
|
||||
_logger.Debug("Updating artist [{0}] on XBMC host: {1}", artist, settings.Address);
|
||||
var command = BuildExecBuiltInCommand(string.Format("UpdateLibrary(music,{0})", xbmcArtistPath));
|
||||
SendCommand(settings, command);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
//Update the entire library
|
||||
_logger.Debug("Artist [{0}] doesn't exist on XBMC host: {1}, Updating Entire Library", artist, settings.Address);
|
||||
var command = BuildExecBuiltInCommand("UpdateLibrary(music)");
|
||||
SendCommand(settings, command);
|
||||
}
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Debug(ex, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
private string SendCommand(XbmcSettings settings, string command)
|
||||
{
|
||||
var url = string.Format("http://{0}/xbmcCmds/xbmcHttp?command={1}", settings.Address, command);
|
||||
|
||||
if (!string.IsNullOrEmpty(settings.Username))
|
||||
{
|
||||
return _httpProvider.DownloadString(url, settings.Username, settings.Password);
|
||||
}
|
||||
|
||||
return _httpProvider.DownloadString(url);
|
||||
}
|
||||
|
||||
private string BuildExecBuiltInCommand(string command)
|
||||
{
|
||||
return string.Format("ExecBuiltIn({0})", command);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
using NzbDrone.Core.Notifications.Xbmc.Model;
|
||||
using NzbDrone.Core.Music;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Xbmc
|
||||
{
|
||||
public interface IApiProvider
|
||||
{
|
||||
void Notify(XbmcSettings settings, string title, string message);
|
||||
void Update(XbmcSettings settings, Artist artist);
|
||||
void Clean(XbmcSettings settings);
|
||||
bool CanHandle(XbmcVersion version);
|
||||
}
|
||||
}
|
@ -1,109 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using NLog;
|
||||
using NzbDrone.Core.Notifications.Xbmc.Model;
|
||||
using NzbDrone.Core.Music;
|
||||
|
||||
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 Update(XbmcSettings settings, Artist artist)
|
||||
{
|
||||
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("audio")))
|
||||
{
|
||||
_logger.Debug("Audio is currently playing, skipping library update");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
UpdateLibrary(settings, artist);
|
||||
}
|
||||
|
||||
public void Clean(XbmcSettings settings)
|
||||
{
|
||||
_proxy.CleanLibrary(settings);
|
||||
}
|
||||
|
||||
public List<ActivePlayer> GetActivePlayers(XbmcSettings settings)
|
||||
{
|
||||
return _proxy.GetActivePlayers(settings);
|
||||
}
|
||||
|
||||
public string GetArtistPath(XbmcSettings settings, Artist artist)
|
||||
{
|
||||
var allArtists = _proxy.GetArtist(settings);
|
||||
|
||||
if (!allArtists.Any())
|
||||
{
|
||||
_logger.Debug("No Artists returned from XBMC");
|
||||
return null;
|
||||
}
|
||||
|
||||
var matchingArtist = allArtists.FirstOrDefault(s =>
|
||||
{
|
||||
var musicBrainzId = s.MusicbrainzArtistId.FirstOrDefault();
|
||||
|
||||
return musicBrainzId == artist.Metadata.Value.ForeignArtistId || s.Label == artist.Name;
|
||||
});
|
||||
|
||||
return matchingArtist?.File;
|
||||
}
|
||||
|
||||
private void UpdateLibrary(XbmcSettings settings, Artist artist)
|
||||
{
|
||||
try
|
||||
{
|
||||
var artistPath = GetArtistPath(settings, artist);
|
||||
|
||||
if (artistPath != null)
|
||||
{
|
||||
_logger.Debug("Updating artist {0} (Path: {1}) on XBMC host: {2}", artist, artistPath, settings.Address);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
_logger.Debug("Artist {0} doesn't exist on XBMC host: {1}, Updating Entire Library", artist,
|
||||
settings.Address);
|
||||
}
|
||||
|
||||
var response = _proxy.UpdateLibrary(settings, artistPath);
|
||||
|
||||
if (!response.Equals("OK", StringComparison.InvariantCultureIgnoreCase))
|
||||
{
|
||||
_logger.Debug("Failed to update library for: {0}", settings.Address);
|
||||
}
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Debug(ex, ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue