2012-02-22 00:10:42 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Xml.Linq;
|
|
|
|
|
using NLog;
|
|
|
|
|
using NzbDrone.Common;
|
2013-02-24 07:48:52 +01:00
|
|
|
|
using NzbDrone.Core.Configuration;
|
2012-02-22 00:10:42 +01:00
|
|
|
|
|
2013-02-25 00:47:57 +01:00
|
|
|
|
namespace NzbDrone.Core.ExternalNotification
|
2012-02-22 00:10:42 +01:00
|
|
|
|
{
|
|
|
|
|
public class PlexProvider
|
|
|
|
|
{
|
|
|
|
|
private readonly HttpProvider _httpProvider;
|
2013-02-24 07:48:52 +01:00
|
|
|
|
private readonly IConfigService _configService;
|
2012-02-22 00:10:42 +01:00
|
|
|
|
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
2013-02-24 07:48:52 +01:00
|
|
|
|
public PlexProvider(HttpProvider httpProvider, IConfigService configService)
|
2012-02-22 00:10:42 +01:00
|
|
|
|
{
|
|
|
|
|
_httpProvider = httpProvider;
|
2013-02-24 07:48:52 +01:00
|
|
|
|
_configService = configService;
|
2012-02-22 00:10:42 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public PlexProvider()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void Notify(string header, string message)
|
|
|
|
|
{
|
|
|
|
|
//Foreach plex client send a notification
|
2013-02-24 07:48:52 +01:00
|
|
|
|
foreach(var host in _configService.PlexClientHosts.Split(','))
|
2012-02-22 00:10:42 +01:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var command = String.Format("ExecBuiltIn(Notification({0}, {1}))", header, message);
|
2013-02-24 07:48:52 +01:00
|
|
|
|
SendCommand(host.Trim(), command, _configService.PlexUsername, _configService.PlexPassword);
|
2012-02-22 00:10:42 +01:00
|
|
|
|
}
|
|
|
|
|
catch(Exception ex)
|
|
|
|
|
{
|
2012-02-22 01:53:17 +01:00
|
|
|
|
logger.WarnException("Failed to send notification to Plex Client: " + host.Trim(), ex);
|
2012-02-22 00:10:42 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void UpdateLibrary()
|
|
|
|
|
{
|
2013-02-24 07:48:52 +01:00
|
|
|
|
var host = _configService.PlexServerHost;
|
2012-02-22 00:10:42 +01:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2013-02-25 00:47:57 +01:00
|
|
|
|
logger.Trace("Sending Update Request to Plex Server");
|
2012-02-22 00:10:42 +01:00
|
|
|
|
var sections = GetSectionKeys(host);
|
|
|
|
|
sections.ForEach(s => UpdateSection(host, s));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch(Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logger.WarnException("Failed to Update Plex host: " + host, ex);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<int> GetSectionKeys(string host)
|
|
|
|
|
{
|
|
|
|
|
logger.Trace("Getting sections from Plex host: {0}", host);
|
|
|
|
|
var url = String.Format("http://{0}/library/sections", host);
|
|
|
|
|
var xmlStream = _httpProvider.DownloadStream(url, null);
|
|
|
|
|
var xDoc = XDocument.Load(xmlStream);
|
|
|
|
|
var mediaContainer = xDoc.Descendants("MediaContainer").FirstOrDefault();
|
|
|
|
|
var directories = mediaContainer.Descendants("Directory").Where(x => x.Attribute("type").Value == "show");
|
|
|
|
|
|
|
|
|
|
return directories.Select(d => Int32.Parse(d.Attribute("key").Value)).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UpdateSection(string host, int key)
|
|
|
|
|
{
|
|
|
|
|
logger.Trace("Updating Plex host: {0}, Section: {1}", host, key);
|
|
|
|
|
var url = String.Format("http://{0}/library/sections/{1}/refresh", host, key);
|
|
|
|
|
_httpProvider.DownloadString(url);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual string SendCommand(string host, string command, string username, string password)
|
|
|
|
|
{
|
|
|
|
|
var url = String.Format("http://{0}/xbmcCmds/xbmcHttp?command={1}", host, command);
|
|
|
|
|
|
|
|
|
|
if (!String.IsNullOrEmpty(username))
|
|
|
|
|
{
|
|
|
|
|
return _httpProvider.DownloadString(url, username, password);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _httpProvider.DownloadString(url);
|
|
|
|
|
}
|
2012-10-22 06:29:12 +02:00
|
|
|
|
|
|
|
|
|
public virtual void TestNotification(string hosts, string username, string password)
|
|
|
|
|
{
|
|
|
|
|
foreach (var host in hosts.Split(','))
|
|
|
|
|
{
|
|
|
|
|
logger.Trace("Sending Test Notifcation to XBMC Host: {0}", host);
|
|
|
|
|
var command = String.Format("ExecBuiltIn(Notification({0}, {1}))", "Test Notification", "Success! Notifications are setup correctly");
|
2013-02-24 07:48:52 +01:00
|
|
|
|
SendCommand(host.Trim(), command, _configService.PlexUsername, _configService.PlexPassword);
|
2012-10-22 06:29:12 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2012-02-22 00:10:42 +01:00
|
|
|
|
}
|
|
|
|
|
}
|