2011-03-06 22:45:35 +02:00
using System ;
2011-03-06 23:36:09 +02:00
using System.IO ;
2011-03-06 22:45:35 +02:00
using System.Linq ;
2011-03-06 23:36:09 +02:00
using System.Xml.Linq ;
2011-06-14 04:23:04 +03:00
using Ninject ;
2011-03-06 22:45:35 +02:00
using NLog ;
2011-04-04 06:50:12 +03:00
using NzbDrone.Core.Providers.Core ;
2011-03-06 22:45:35 +02:00
namespace NzbDrone.Core.Providers
{
2011-04-08 18:15:51 +03:00
public class XbmcProvider
2011-03-06 22:45:35 +02:00
{
2011-04-10 05:44:01 +03:00
private static readonly Logger Logger = LogManager . GetCurrentClassLogger ( ) ;
2011-04-10 04:34:36 +03:00
private readonly ConfigProvider _configProvider ;
2011-04-07 05:25:52 +03:00
private readonly HttpProvider _httpProvider ;
2011-03-06 22:45:35 +02:00
2011-06-14 04:23:04 +03:00
[Inject]
2011-04-10 04:34:36 +03:00
public XbmcProvider ( ConfigProvider configProvider , HttpProvider httpProvider )
2011-03-06 22:45:35 +02:00
{
_configProvider = configProvider ;
2011-03-07 08:33:59 +02:00
_httpProvider = httpProvider ;
2011-03-06 22:45:35 +02:00
}
2011-04-08 18:15:51 +03:00
public virtual void Notify ( string header , string message )
2011-03-06 22:45:35 +02:00
{
//Get time in seconds and convert to ms
2011-06-17 05:27:10 +03:00
var time = Convert . ToInt32 ( _configProvider . GetValue ( "XbmcDisplayTime" , "3" ) ) * 1000 ;
2011-03-06 22:45:35 +02:00
var command = String . Format ( "ExecBuiltIn(Notification({0},{1},{2}))" , header , message , time ) ;
2011-06-17 05:27:10 +03:00
if ( Convert . ToBoolean ( _configProvider . GetValue ( "XbmcNotificationImage" , false ) ) )
2011-03-06 22:45:35 +02:00
{
2011-03-06 23:36:09 +02:00
//Todo: Get the actual port that NzbDrone is running on...
2011-04-20 04:20:20 +03:00
var serverInfo = String . Format ( "http://{0}:{1}" , Environment . MachineName , "8989" ) ;
2011-03-06 22:45:35 +02:00
2011-03-06 23:36:09 +02:00
var imageUrl = String . Format ( "{0}/Content/XbmcNotification.png" , serverInfo ) ;
command = String . Format ( "ExecBuiltIn(Notification({0},{1},{2}, {3}))" , header , message , time , imageUrl ) ;
2011-03-06 22:45:35 +02:00
}
2011-06-17 05:27:10 +03:00
foreach ( var host in _configProvider . GetValue ( "XbmcHosts" , "localhost:80" ) . Split ( ',' ) )
2011-03-06 23:36:09 +02:00
{
Logger . Trace ( "Sending Notifcation to XBMC Host: {0}" , host ) ;
SendCommand ( host , command ) ;
}
2011-03-06 22:45:35 +02:00
}
2011-04-08 18:15:51 +03:00
public virtual void Update ( int seriesId )
2011-03-06 22:45:35 +02:00
{
2011-06-17 05:27:10 +03:00
foreach ( var host in _configProvider . GetValue ( "XbmcHosts" , "localhost:80" ) . Split ( ',' ) )
2011-03-06 23:36:09 +02:00
{
Logger . Trace ( "Sending Update DB Request to XBMC Host: {0}" , host ) ;
var xbmcSeriesPath = GetXbmcSeriesPath ( host , seriesId ) ;
//If the path is not found & the user wants to update the entire library, do it now.
2011-04-10 05:44:01 +03:00
if ( String . IsNullOrEmpty ( xbmcSeriesPath ) & &
2011-06-17 05:27:10 +03:00
Convert . ToBoolean ( _configProvider . GetValue ( "XbmcFullUpdate" , false ) ) )
2011-03-06 23:36:09 +02:00
{
//Update the entire library
Logger . Trace ( "Series [{0}] doesn't exist on XBMC host: {1}, Updating Entire Library" , seriesId , host ) ;
SendCommand ( host , "ExecBuiltIn(UpdateLibrary(video))" ) ;
return ;
}
var command = String . Format ( "ExecBuiltIn(UpdateLibrary(video,{0}))" , xbmcSeriesPath ) ;
SendCommand ( host , command ) ;
}
2011-03-06 22:45:35 +02:00
}
2011-04-08 18:15:51 +03:00
public virtual void Clean ( )
2011-03-06 22:45:35 +02:00
{
2011-06-17 05:27:10 +03:00
foreach ( var host in _configProvider . GetValue ( "XbmcHosts" , "localhost:80" ) . Split ( ',' ) )
2011-03-06 23:36:09 +02:00
{
Logger . Trace ( "Sending DB Clean Request to XBMC Host: {0}" , host ) ;
2011-03-17 22:01:21 +02:00
var command = String . Format ( "ExecBuiltIn(CleanLibrary(video))" ) ;
2011-03-06 23:36:09 +02:00
SendCommand ( host , command ) ;
}
2011-03-06 22:45:35 +02:00
}
private string SendCommand ( string host , string command )
{
2011-06-17 05:27:10 +03:00
var username = _configProvider . GetValue ( "XbmcUsername" , String . Empty ) ;
var password = _configProvider . GetValue ( "XbmcPassword" , String . Empty ) ;
2011-03-06 22:45:35 +02:00
var url = String . Format ( "http://{0}/xbmcCmds/xbmcHttp?command={1}" , host , command ) ;
2011-03-07 08:33:59 +02:00
if ( ! String . IsNullOrEmpty ( username ) )
2011-03-06 22:45:35 +02:00
{
2011-03-07 08:33:59 +02:00
return _httpProvider . DownloadString ( url , username , password ) ;
2011-03-06 22:45:35 +02:00
}
2011-04-10 05:44:01 +03:00
2011-03-07 08:33:59 +02:00
return _httpProvider . DownloadString ( url ) ;
2011-03-06 22:45:35 +02:00
}
2011-03-06 23:36:09 +02:00
private string GetXbmcSeriesPath ( string host , int seriesId )
{
2011-04-10 05:44:01 +03: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 23:36:09 +02:00
var command = String . Format ( "QueryVideoDatabase({0})" , query ) ;
2011-04-10 05:44:01 +03:00
var setResponseCommand =
"SetResponseFormat(webheader;false;webfooter;false;header;<xml>;footer;</xml>;opentag;<tag>;closetag;</tag>;closefinaltag;false)" ;
2011-03-06 23:36:09 +02:00
var resetResponseCommand = "SetResponseFormat()" ;
SendCommand ( host , setResponseCommand ) ;
var response = SendCommand ( host , command ) ;
SendCommand ( host , resetResponseCommand ) ;
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-03-06 22:45:35 +02:00
}
2011-04-10 05:44:01 +03:00
}