2011-03-06 20:45:35 +00:00
using System ;
2011-07-09 18:19:33 +00:00
using System.Collections.Generic ;
2011-03-06 21:36:09 +00:00
using System.IO ;
2011-03-06 20:45:35 +00:00
using System.Linq ;
2011-07-09 18:19:33 +00:00
using System.Web.Script.Serialization ;
2011-03-06 21:36:09 +00:00
using System.Xml.Linq ;
2011-06-14 01:23:04 +00:00
using Ninject ;
2011-03-06 20:45:35 +00:00
using NLog ;
2011-07-09 18:19:33 +00:00
using NzbDrone.Core.Model.Xbmc ;
2011-04-04 03:50:12 +00:00
using NzbDrone.Core.Providers.Core ;
2011-07-09 18:19:33 +00:00
using NzbDrone.Core.Providers.Xbmc ;
using NzbDrone.Core.Repository ;
2011-03-06 20:45:35 +00:00
namespace NzbDrone.Core.Providers
{
2011-04-08 15:15:51 +00:00
public class XbmcProvider
2011-03-06 20:45:35 +00:00
{
2011-04-10 02:44:01 +00:00
private static readonly Logger Logger = LogManager . GetCurrentClassLogger ( ) ;
2011-04-10 01:34:36 +00:00
private readonly ConfigProvider _configProvider ;
2011-04-07 02:25:52 +00:00
private readonly HttpProvider _httpProvider ;
2011-07-09 18:19:33 +00:00
private readonly EventClientProvider _eventClientProvider ;
2011-03-06 20:45:35 +00:00
2011-06-14 01:23:04 +00:00
[Inject]
2011-07-09 18:19:33 +00:00
public XbmcProvider ( ConfigProvider configProvider , HttpProvider httpProvider , EventClientProvider eventClientProvider )
2011-03-06 20:45:35 +00:00
{
_configProvider = configProvider ;
2011-03-07 06:33:59 +00:00
_httpProvider = httpProvider ;
2011-07-09 18:19:33 +00:00
_eventClientProvider = eventClientProvider ;
2011-03-06 20:45:35 +00:00
}
2011-04-08 15:15:51 +00:00
public virtual void Notify ( string header , string message )
2011-03-06 20:45:35 +00:00
{
2011-07-09 18:19:33 +00:00
//Always use EventServer, until Json has real support for it
foreach ( var host in _configProvider . XbmcHosts . Split ( ',' ) )
{
Logger . Trace ( "Sending Notifcation to XBMC Host: {0}" , host ) ;
_eventClientProvider . SendNotification ( header , message , IconType . Jpeg , "NzbDrone.jpg" , GetHostWithoutPort ( host ) ) ;
}
}
public XbmcProvider ( )
{
}
public virtual void Update ( Series series )
{
//Use Json for Eden/Nightly or depricated HTTP for 10.x (Dharma) to get the proper path
//Perform update with EventServer (Json currently doesn't support updating a specific path only - July 2011)
var username = _configProvider . XbmcUsername ;
var password = _configProvider . XbmcPassword ;
2011-03-06 20:45:35 +00:00
2011-07-09 18:19:33 +00:00
foreach ( var host in _configProvider . XbmcHosts . Split ( ',' ) )
2011-03-06 20:45:35 +00:00
{
2011-07-09 18:19:33 +00:00
Logger . Trace ( "Determining version of XBMC Host: {0}" , host ) ;
var version = GetJsonVersion ( host , username , password ) ;
2011-03-06 20:45:35 +00:00
2011-09-27 00:17:41 +00:00
Logger . Trace ( "Determining if there are any active players on XBMC host: {0}" , host ) ;
var activePlayers = GetActivePlayers ( host , username , password ) ;
//If video is currently playing, then skip update
if ( activePlayers [ "video" ] )
{
Logger . Debug ( "Video is currently playing, skipping library update" ) ;
continue ;
}
Logger . Trace ( "No video playing, proceeding with library update" ) ;
2011-07-09 18:19:33 +00:00
//If Dharma
if ( version = = 2 )
UpdateWithHttp ( series , host , username , password ) ;
//If Eden or newer (attempting to make it future compatible)
else if ( version > = 3 )
UpdateWithJson ( series , password , host , username ) ;
2011-03-06 20:45:35 +00:00
}
2011-07-09 18:19:33 +00:00
}
2011-03-06 20:45:35 +00:00
2011-07-09 18:19:33 +00:00
public virtual bool UpdateWithJson ( Series series , string host , string username , string password )
{
try
2011-03-06 21:36:09 +00:00
{
2011-07-09 18:19:33 +00:00
//Use Json!
var xbmcShows = GetTvShowsJson ( host , username , password ) ;
var path = xbmcShows . Where ( s = > s . ImdbNumber = = series . SeriesId | | s . Label = = series . Title ) . FirstOrDefault ( ) ;
var hostOnly = GetHostWithoutPort ( host ) ;
if ( path ! = null )
{
Logger . Trace ( "Updating series [{0}] on XBMC host: {1}" , series . Title , host ) ;
var command = String . Format ( "ExecBuiltIn(UpdateLibrary(video,{0}))" , path . File ) ;
_eventClientProvider . SendAction ( hostOnly , ActionType . ExecBuiltin , command ) ;
}
else
{
Logger . Trace ( "Series [{0}] doesn't exist on XBMC host: {1}, Updating Entire Library" , series . Title , host ) ;
var command = String . Format ( "ExecBuiltIn(UpdateLibrary(video))" ) ;
_eventClientProvider . SendAction ( hostOnly , ActionType . ExecBuiltin , command ) ;
}
2011-03-06 21:36:09 +00:00
}
2011-07-09 18:19:33 +00:00
catch ( Exception ex )
{
Logger . DebugException ( ex . Message , ex ) ;
return false ;
}
return true ;
2011-03-06 20:45:35 +00:00
}
2011-07-09 18:19:33 +00:00
public virtual bool UpdateWithHttp ( Series series , string host , string username , string password )
2011-03-06 20:45:35 +00:00
{
2011-07-09 18:19:33 +00:00
try
2011-03-06 21:36:09 +00:00
{
Logger . Trace ( "Sending Update DB Request to XBMC Host: {0}" , host ) ;
2011-07-09 18:19:33 +00:00
var xbmcSeriesPath = GetXbmcSeriesPath ( host , series . SeriesId , username , password ) ;
2011-03-06 21:36:09 +00:00
2011-07-09 18:19:33 +00:00
//If the path is found update it, else update the whole library
if ( ! String . IsNullOrEmpty ( xbmcSeriesPath ) )
{
Logger . Trace ( "Updating series [{0}] on XBMC host: {1}" , series . Title , host ) ;
var command = String . Format ( "ExecBuiltIn(UpdateLibrary(video,{0}))" , xbmcSeriesPath ) ;
SendCommand ( host , command , username , password ) ;
}
else
2011-03-06 21:36:09 +00:00
{
//Update the entire library
2011-07-09 18:19:33 +00:00
Logger . Trace ( "Series [{0}] doesn't exist on XBMC host: {1}, Updating Entire Library" , series . Title , host ) ;
SendCommand ( host , "ExecBuiltIn(UpdateLibrary(video))" , username , password ) ;
2011-03-06 21:36:09 +00:00
}
2011-07-09 18:19:33 +00:00
}
2011-03-06 21:36:09 +00:00
2011-07-09 18:19:33 +00:00
catch ( Exception ex )
{
Logger . DebugException ( ex . Message , ex ) ;
return false ;
2011-03-06 21:36:09 +00:00
}
2011-07-09 18:19:33 +00:00
return true ;
2011-03-06 20:45:35 +00:00
}
2011-04-08 15:15:51 +00:00
public virtual void Clean ( )
2011-03-06 20:45:35 +00:00
{
2011-07-09 18:19:33 +00:00
//Use EventServer, once Dharma is extinct use Json?
foreach ( var host in _configProvider . XbmcHosts . Split ( ',' ) )
2011-03-06 21:36:09 +00:00
{
Logger . Trace ( "Sending DB Clean Request to XBMC Host: {0}" , host ) ;
2011-07-09 18:19:33 +00:00
var command = "ExecBuiltIn(CleanLibrary(video))" ;
_eventClientProvider . SendAction ( GetHostWithoutPort ( host ) , ActionType . ExecBuiltin , command ) ;
2011-03-06 21:36:09 +00:00
}
2011-03-06 20:45:35 +00:00
}
2011-07-09 18:19:33 +00:00
public virtual string SendCommand ( string host , string command , string username , string password )
2011-03-06 20:45:35 +00:00
{
var url = String . Format ( "http://{0}/xbmcCmds/xbmcHttp?command={1}" , host , command ) ;
2011-03-07 06:33:59 +00:00
if ( ! String . IsNullOrEmpty ( username ) )
2011-03-06 20:45:35 +00:00
{
2011-03-07 06:33:59 +00:00
return _httpProvider . DownloadString ( url , username , password ) ;
2011-03-06 20:45:35 +00:00
}
2011-04-10 02:44:01 +00:00
2011-03-07 06:33:59 +00:00
return _httpProvider . DownloadString ( url ) ;
2011-03-06 20:45:35 +00:00
}
2011-03-06 21:36:09 +00:00
2011-07-09 18:19:33 +00:00
public virtual string GetXbmcSeriesPath ( string host , int seriesId , string username , string password )
2011-03-06 21:36:09 +00:00
{
2011-04-10 02:44:01 +00:00
var query =
String . Format (
"select path.strPath from path, tvshow, tvshowlinkpath where tvshow.c12 = {0} and tvshowlinkpath.idShow = tvshow.idShow and tvshowlinkpath.idPath = path.idPath" ,
seriesId ) ;
2011-03-06 21:36:09 +00:00
var command = String . Format ( "QueryVideoDatabase({0})" , query ) ;
2011-07-09 18:19:33 +00:00
const string setResponseCommand =
2011-04-10 02:44:01 +00:00
"SetResponseFormat(webheader;false;webfooter;false;header;<xml>;footer;</xml>;opentag;<tag>;closetag;</tag>;closefinaltag;false)" ;
2011-07-09 18:19:33 +00:00
const string resetResponseCommand = "SetResponseFormat()" ;
2011-03-06 21:36:09 +00:00
2011-07-09 18:19:33 +00:00
SendCommand ( host , setResponseCommand , username , password ) ;
var response = SendCommand ( host , command , username , password ) ;
SendCommand ( host , resetResponseCommand , username , password ) ;
2011-03-06 21:36:09 +00:00
if ( String . IsNullOrEmpty ( response ) )
return String . Empty ;
var xDoc = XDocument . Load ( new StringReader ( response ) ) ;
var xml = ( from x in xDoc . Descendants ( "xml" ) select x ) . FirstOrDefault ( ) ;
if ( xml = = null )
return String . Empty ;
var field = xml . Descendants ( "field" ) . FirstOrDefault ( ) ;
if ( field = = null )
return String . Empty ;
return field . Value ;
}
2011-07-09 18:19:33 +00:00
public virtual int GetJsonVersion ( string host , string username , string password )
{
//2 = Dharma
//3 = Eden/Nightly (as of July 2011)
var version = 0 ;
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 ) ;
if ( CheckForJsonError ( response ) )
return version ;
2011-09-27 00:17:41 +00:00
Logger . Trace ( "Getting version from response" ) ;
2011-07-09 18:19:33 +00:00
var result = serializer . Deserialize < VersionResult > ( response ) ;
result . Result . TryGetValue ( "version" , out version ) ;
}
catch ( Exception ex )
{
Logger . DebugException ( ex . Message , ex ) ;
}
return version ;
}
public virtual Dictionary < string , bool > GetActivePlayers ( string host , string username , string password )
{
//2 = Dharma
//3 = Eden/Nightly (as of July 2011)
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 ) ;
if ( CheckForJsonError ( response ) )
return null ;
var result = serializer . Deserialize < ActivePlayersResult > ( response ) ;
return result . Result ;
}
catch ( Exception ex )
{
Logger . DebugException ( ex . Message , ex ) ;
}
return null ;
}
public virtual List < TvShow > GetTvShowsJson ( string host , string username , string password )
{
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 ) ;
if ( CheckForJsonError ( response ) )
return null ;
var result = serializer . Deserialize < TvShowResult > ( response ) ;
var shows = result . Result [ "tvshows" ] ;
return shows ;
}
catch ( Exception ex )
{
Logger . DebugException ( ex . Message , ex ) ;
}
return null ;
}
public virtual bool CheckForJsonError ( string response )
{
2011-09-27 00:17:41 +00:00
Logger . Trace ( "Looking for error in response: {0}" , response ) ;
2011-07-09 18:19:33 +00:00
if ( response . StartsWith ( "{\"error\"" ) )
{
var serializer = new JavaScriptSerializer ( ) ;
var error = serializer . Deserialize < ErrorResult > ( response ) ;
var code = error . Error [ "code" ] ;
var message = error . Error [ "message" ] ;
Logger . Debug ( "XBMC Json Error. Code = {0}, Message: {1}" , code , message ) ;
return true ;
}
2011-09-27 17:41:36 +00:00
if ( String . IsNullOrWhiteSpace ( response ) )
{
Logger . Debug ( "Invalid response from XBMC, the response is not valid JSON" ) ;
return true ;
}
2011-07-09 18:19:33 +00:00
return false ;
}
private string GetHostWithoutPort ( string address )
{
return address . Split ( ':' ) [ 0 ] ;
}
2011-03-06 20:45:35 +00:00
}
2011-04-10 02:44:01 +00:00
}