mirror of
https://github.com/SickGear/SickGear.git
synced 2025-01-05 17:43:37 +00:00
Merge pull request #72 from JackDandy/feature/UpdatePlexNotifier
Feature/update plex notifier
This commit is contained in:
commit
04a2aa174b
4 changed files with 154 additions and 40 deletions
|
@ -7,6 +7,9 @@
|
|||
* Add TVRage network name standardization
|
||||
* Change some provider images. Add a few new images
|
||||
* Remove redundant Coming Eps template code used in the old UI
|
||||
* Change update Plex notifier (port from SickBeard)
|
||||
* Change Plex notifications to allow authenticated library updates (port from mmccurdy07/Sick-Beard)
|
||||
* Change Config/Notifications/Plex logo and description (adapted port from mmccurdy07/Sick-Beard)
|
||||
|
||||
[develop changelog]
|
||||
|
||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 4.3 KiB After Width: | Height: | Size: 2.4 KiB |
|
@ -159,7 +159,7 @@
|
|||
<div class="component-group-desc">
|
||||
<img class="notifier-icon" src="$sbRoot/images/notifiers/plex.png" alt="" title="Plex Media Server" />
|
||||
<h3><a href="<%= anon_url('http://www.plexapp.com/') %>" rel="noreferrer" onclick="window.open(this.href, '_blank'); return false;">Plex Media Server</a></h3>
|
||||
<p>Experience your media on a visually stunning, easy to use interface on your Mac connected to your TV. Your media library has never looked this good!</p>
|
||||
<p>Plex organizes all of your personal media, wherever you keep it, so you can enjoy it on any device</p>
|
||||
<p class="plexinfo hide">For sending notifications to Plex Home Theater (PHT) clients, use the XBMC notifier with port <b>3005</b>.</p>
|
||||
</div>
|
||||
<fieldset class="component-group-list">
|
||||
|
@ -222,12 +222,12 @@
|
|||
</div>
|
||||
<div class="field-pair">
|
||||
<label for="plex_host">
|
||||
<span class="component-title">Plex Client IP:Port</span>
|
||||
<span class="component-title">Plex client IP:Port</span>
|
||||
<input type="text" name="plex_host" id="plex_host" value="$sickbeard.PLEX_HOST" class="form-control input-sm input350" />
|
||||
</label>
|
||||
<label>
|
||||
<span class="component-title"> </span>
|
||||
<span class="component-desc">host running Plex Client (eg. 192.168.1.100:3000)</span>
|
||||
<span class="component-desc">host running Plex client (eg. 192.168.1.100:3000)</span>
|
||||
</label>
|
||||
<label>
|
||||
<span class="component-title"> </span>
|
||||
|
@ -236,22 +236,22 @@
|
|||
</div>
|
||||
<div class="field-pair">
|
||||
<label for="plex_username">
|
||||
<span class="component-title">Plex Client username</span>
|
||||
<span class="component-title">Plex client username</span>
|
||||
<input type="text" name="plex_username" id="plex_username" value="$sickbeard.PLEX_USERNAME" class="form-control input-sm input250" />
|
||||
</label>
|
||||
<label>
|
||||
<span class="component-title"> </span>
|
||||
<span class="component-desc">username for your Plex client API (blank for none)</span>
|
||||
<span class="component-desc">needed if your client / server requires authentication (blank for none)</span>
|
||||
</label>
|
||||
</div>
|
||||
<div class="field-pair">
|
||||
<label for="plex_password">
|
||||
<span class="component-title">Plex Client password</span>
|
||||
<span class="component-title">Plex client password</span>
|
||||
<input type="password" name="plex_password" id="plex_password" value="$sickbeard.PLEX_PASSWORD" class="form-control input-sm input250" />
|
||||
</label>
|
||||
<label>
|
||||
<span class="component-title"> </span>
|
||||
<span class="component-desc">password for your Plex client API (blank for none)</span>
|
||||
<span class="component-desc">needed if your client / server requires authentication (blank for none)</span>
|
||||
</label>
|
||||
</div>
|
||||
<div class="testNotification" id="testPLEX-result">Click below to test.</div>
|
||||
|
|
|
@ -27,33 +27,113 @@ from sickbeard import common
|
|||
from sickbeard.exceptions import ex
|
||||
from sickbeard.encodingKludge import fixStupidEncodings
|
||||
|
||||
from sickbeard.notifiers.xbmc import XBMCNotifier
|
||||
|
||||
# TODO: switch over to using ElementTree
|
||||
from xml.dom import minidom
|
||||
try:
|
||||
import xml.etree.cElementTree as etree
|
||||
except ImportError:
|
||||
import elementtree.ElementTree as etree
|
||||
|
||||
|
||||
class PLEXNotifier(XBMCNotifier):
|
||||
def _notify_pmc(self, message, title="SickGear", host=None, username=None, password=None, force=False):
|
||||
class PLEXNotifier:
|
||||
|
||||
def _send_to_plex(self, command, host, username=None, password=None):
|
||||
"""Handles communication to Plex hosts via HTTP API
|
||||
|
||||
Args:
|
||||
command: Dictionary of field/data pairs, encoded via urllib and passed to the legacy xbmcCmds HTTP API
|
||||
host: Plex host:port
|
||||
username: Plex API username
|
||||
password: Plex API password
|
||||
|
||||
Returns:
|
||||
Returns 'OK' for successful commands or False if there was an error
|
||||
|
||||
"""
|
||||
|
||||
# fill in omitted parameters
|
||||
if not host:
|
||||
if sickbeard.PLEX_HOST:
|
||||
host = sickbeard.PLEX_HOST # Use the default Plex host
|
||||
else:
|
||||
logger.log(u"No Plex host specified, check your settings", logger.DEBUG)
|
||||
return False
|
||||
if not username:
|
||||
username = sickbeard.PLEX_USERNAME
|
||||
if not password:
|
||||
password = sickbeard.PLEX_PASSWORD
|
||||
|
||||
# suppress notifications if the notifier is disabled but the notify options are checked
|
||||
if not sickbeard.USE_PLEX and not force:
|
||||
logger.log("Notification for Plex not enabled, skipping this notification", logger.DEBUG)
|
||||
if not host:
|
||||
logger.log(u"PLEX: No host specified, check your settings", logger.ERROR)
|
||||
return False
|
||||
|
||||
return self._notify_xbmc(message=message, title=title, host=host, username=username, password=password,
|
||||
force=True)
|
||||
for key in command:
|
||||
if type(command[key]) == unicode:
|
||||
command[key] = command[key].encode('utf-8')
|
||||
|
||||
enc_command = urllib.urlencode(command)
|
||||
logger.log(u"PLEX: Encoded API command: " + enc_command, logger.DEBUG)
|
||||
|
||||
url = 'http://%s/xbmcCmds/xbmcHttp/?%s' % (host, enc_command)
|
||||
try:
|
||||
req = urllib2.Request(url)
|
||||
# if we have a password, use authentication
|
||||
if password:
|
||||
base64string = base64.encodestring('%s:%s' % (username, password))[:-1]
|
||||
authheader = "Basic %s" % base64string
|
||||
req.add_header("Authorization", authheader)
|
||||
logger.log(u"PLEX: Contacting (with auth header) via url: " + url, logger.DEBUG)
|
||||
else:
|
||||
logger.log(u"PLEX: Contacting via url: " + url, logger.DEBUG)
|
||||
|
||||
response = urllib2.urlopen(req)
|
||||
|
||||
result = response.read().decode(sickbeard.SYS_ENCODING)
|
||||
response.close()
|
||||
|
||||
logger.log(u"PLEX: HTTP response: " + result.replace('\n', ''), logger.DEBUG)
|
||||
# could return result response = re.compile('<html><li>(.+\w)</html>').findall(result)
|
||||
return 'OK'
|
||||
|
||||
except (urllib2.URLError, IOError), e:
|
||||
logger.log(u"PLEX: Warning: Couldn't contact Plex at " + fixStupidEncodings(url) + " " + ex(e), logger.WARNING)
|
||||
return False
|
||||
|
||||
def _notify(self, message, title="SickGear", host=None, username=None, password=None, force=False):
|
||||
"""Internal wrapper for the notify_snatch and notify_download functions
|
||||
|
||||
Args:
|
||||
message: Message body of the notice to send
|
||||
title: Title of the notice to send
|
||||
host: Plex Media Client(s) host:port
|
||||
username: Plex username
|
||||
password: Plex password
|
||||
force: Used for the Test method to override config safety checks
|
||||
|
||||
Returns:
|
||||
Returns a list results in the format of host:ip:result
|
||||
The result will either be 'OK' or False, this is used to be parsed by the calling function.
|
||||
|
||||
"""
|
||||
|
||||
# suppress notifications if the notifier is disabled but the notify options are checked
|
||||
if not sickbeard.USE_PLEX and not force:
|
||||
return False
|
||||
|
||||
# fill in omitted parameters
|
||||
if not host:
|
||||
host = sickbeard.PLEX_HOST
|
||||
if not username:
|
||||
username = sickbeard.PLEX_USERNAME
|
||||
if not password:
|
||||
password = sickbeard.PLEX_PASSWORD
|
||||
|
||||
result = ''
|
||||
for curHost in [x.strip() for x in host.split(",")]:
|
||||
logger.log(u"PLEX: Sending notification to '" + curHost + "' - " + message, logger.MESSAGE)
|
||||
|
||||
command = {'command': 'ExecBuiltIn', 'parameter': 'Notification(' + title.encode("utf-8") + ',' + message.encode("utf-8") + ')'}
|
||||
notifyResult = self._send_to_plex(command, curHost, username, password)
|
||||
if notifyResult:
|
||||
result += curHost + ':' + str(notifyResult)
|
||||
|
||||
return result
|
||||
|
||||
##############################################################################
|
||||
# Public functions
|
||||
##############################################################################
|
||||
|
||||
def notify_snatch(self, ep_name):
|
||||
if sickbeard.PLEX_NOTIFY_ONSNATCH:
|
||||
|
@ -74,10 +154,9 @@ class PLEXNotifier(XBMCNotifier):
|
|||
self._notify_pmc(update_text + new_version, title)
|
||||
|
||||
def test_notify(self, host, username, password):
|
||||
return self._notify_pmc("Testing Plex notifications from SickGear", "Test Notification", host, username,
|
||||
password, force=True)
|
||||
return self._notify("This is a test notification from SickGear", "Test", host, username, password, force=True)
|
||||
|
||||
def update_library(self):
|
||||
def update_library(self, ep_obj=None, host=None, username=None, password=None):
|
||||
"""Handles updating the Plex Media Server host via HTTP API
|
||||
|
||||
Plex Media Server currently only supports updating the whole video library and not a specific path.
|
||||
|
@ -87,36 +166,68 @@ class PLEXNotifier(XBMCNotifier):
|
|||
|
||||
"""
|
||||
|
||||
# fill in omitted parameters
|
||||
if not host:
|
||||
host = sickbeard.PLEX_SERVER_HOST
|
||||
if not username:
|
||||
username = sickbeard.PLEX_USERNAME
|
||||
if not password:
|
||||
password = sickbeard.PLEX_PASSWORD
|
||||
|
||||
if sickbeard.USE_PLEX and sickbeard.PLEX_UPDATE_LIBRARY:
|
||||
if not sickbeard.PLEX_SERVER_HOST:
|
||||
logger.log(u"No Plex Media Server host specified, check your settings", logger.DEBUG)
|
||||
logger.log(u"PLEX: No Plex Media Server host specified, check your settings", logger.DEBUG)
|
||||
return False
|
||||
|
||||
logger.log(u"Updating library for the Plex Media Server host: " + sickbeard.PLEX_SERVER_HOST,
|
||||
logger.MESSAGE)
|
||||
logger.log(u"PLEX: Updating library for the Plex Media Server host: " + host, logger.MESSAGE)
|
||||
|
||||
url = "http://%s/library/sections" % sickbeard.PLEX_SERVER_HOST
|
||||
# if username and password were provided, fetch the auth token from plex.tv
|
||||
token_arg = ""
|
||||
if username and password:
|
||||
|
||||
logger.log(u"PLEX: fetching credentials for Plex user: " + username, logger.DEBUG)
|
||||
req = urllib2.Request("https://plex.tv/users/sign_in.xml", data="")
|
||||
base64string = base64.encodestring('%s:%s' % (username, password))[:-1]
|
||||
authheader = "Basic %s" % base64string
|
||||
req.add_header("Authorization", authheader)
|
||||
req.add_header("X-Plex-Client-Identifier", "Sick-Beard-Notifier")
|
||||
|
||||
try:
|
||||
response = urllib2.urlopen(req)
|
||||
except urllib2.URLError, e:
|
||||
logger.log(u"PLEX: Error fetching credentials from from plex.tv for user %s: %s" % (username, ex(e)), logger.MESSAGE)
|
||||
return False
|
||||
|
||||
try:
|
||||
auth_tree = etree.parse(response)
|
||||
token = auth_tree.findall(".//authentication-token")[0].text
|
||||
token_arg = "?X-Plex-Token=" + token
|
||||
except (ValueError, IndexError) as e:
|
||||
logger.log(u"PLEX: Error parsing plex.tv response: " + ex(e), logger.MESSAGE)
|
||||
return False
|
||||
|
||||
url = "http://%s/library/sections%s" % (sickbeard.PLEX_SERVER_HOST, token_arg)
|
||||
try:
|
||||
xml_sections = minidom.parse(urllib.urlopen(url))
|
||||
xml_tree = etree.parse(urllib.urlopen(url))
|
||||
media_container = xml_tree.getroot()
|
||||
except IOError, e:
|
||||
logger.log(u"Error while trying to contact Plex Media Server: " + ex(e), logger.ERROR)
|
||||
logger.log(u"PLEX: Error while trying to contact Plex Media Server: " + ex(e), logger.ERROR)
|
||||
return False
|
||||
|
||||
sections = xml_sections.getElementsByTagName('Directory')
|
||||
sections = media_container.findall('.//Directory')
|
||||
if not sections:
|
||||
logger.log(u"Plex Media Server not running on: " + sickbeard.PLEX_SERVER_HOST, logger.MESSAGE)
|
||||
logger.log(u"PLEX: Plex Media Server not running on: " + sickbeard.PLEX_SERVER_HOST, logger.MESSAGE)
|
||||
return False
|
||||
|
||||
for s in sections:
|
||||
if s.getAttribute('type') == "show":
|
||||
url = "http://%s/library/sections/%s/refresh" % (sickbeard.PLEX_SERVER_HOST, s.getAttribute('key'))
|
||||
for section in sections:
|
||||
if section.attrib['type'] == "show":
|
||||
url = "http://%s/library/sections/%s/refresh%s" % (sickbeard.PLEX_SERVER_HOST, section.attrib['key'], token_arg)
|
||||
try:
|
||||
urllib.urlopen(url)
|
||||
except Exception, e:
|
||||
logger.log(u"Error updating library section for Plex Media Server: " + ex(e), logger.ERROR)
|
||||
logger.log(u"PLEX: Error updating library section for Plex Media Server: " + ex(e), logger.ERROR)
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
notifier = PLEXNotifier
|
||||
|
|
Loading…
Reference in a new issue