mirror of
https://github.com/SickGear/SickGear.git
synced 2025-01-05 17:43:37 +00:00
Update the Plex notifier
The new Plex HT notification API is exactly the same as XBMC's new notification API. It uses the same JSON-RPC methods. Thus, I've changed the Plex notifier class to inherit from the XBMC notifier class and use its notification method.
This commit is contained in:
parent
4b5fa9582a
commit
10a17d09cb
1 changed files with 7 additions and 112 deletions
|
@ -27,131 +27,27 @@ 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
|
||||
|
||||
|
||||
class PLEXNotifier:
|
||||
def _send_to_plex(self, command, host, username=None, password=None):
|
||||
"""Handles communication to Plex hosts via HTTP API
|
||||
|
||||
def notify_subtitle_download(self, ep_name, lang):
|
||||
if sickbeard.PLEX_NOTIFY_ONSUBTITLEDOWNLOAD:
|
||||
self._notifyXBMC(ep_name + ": " + lang, common.notifyStrings[common.NOTIFY_SUBTITLE_DOWNLOAD])
|
||||
|
||||
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 username:
|
||||
username = sickbeard.PLEX_USERNAME
|
||||
if not password:
|
||||
password = sickbeard.PLEX_PASSWORD
|
||||
|
||||
if not host:
|
||||
logger.log(u"No Plex host specified, check your settings", logger.DEBUG)
|
||||
return False
|
||||
|
||||
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"Contacting Plex (with auth header) via url: " + url, logger.DEBUG)
|
||||
else:
|
||||
logger.log(u"Contacting Plex 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"Warning: Couldn't contact Plex at " + fixStupidEncodings(url) + " " + ex(e), logger.WARNING)
|
||||
return False
|
||||
|
||||
def _notify_pmc(self, message, title="Sick Beard", 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 saftey 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.
|
||||
|
||||
"""
|
||||
|
||||
# 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
|
||||
|
||||
# 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)
|
||||
return False
|
||||
|
||||
result = ''
|
||||
for curHost in [x.strip() for x in host.split(",")]:
|
||||
logger.log(u"Sending Plex 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
|
||||
##############################################################################
|
||||
|
||||
class PLEXNotifier(XBMCNotifier):
|
||||
def notify_snatch(self, ep_name):
|
||||
if sickbeard.PLEX_NOTIFY_ONSNATCH:
|
||||
self._notify_pmc(ep_name, common.notifyStrings[common.NOTIFY_SNATCH])
|
||||
self._notify_xbmc(ep_name, common.notifyStrings[common.NOTIFY_SNATCH])
|
||||
|
||||
def notify_download(self, ep_name):
|
||||
if sickbeard.PLEX_NOTIFY_ONDOWNLOAD:
|
||||
self._notify_pmc(ep_name, common.notifyStrings[common.NOTIFY_DOWNLOAD])
|
||||
self._notify_xbmc(ep_name, common.notifyStrings[common.NOTIFY_DOWNLOAD])
|
||||
|
||||
def notify_subtitle_download(self, ep_name, lang):
|
||||
if sickbeard.PLEX_NOTIFY_ONSUBTITLEDOWNLOAD:
|
||||
self._notify_pmc(ep_name + ": " + lang, common.notifyStrings[common.NOTIFY_SUBTITLE_DOWNLOAD])
|
||||
self._notify_xbmc(ep_name + ": " + lang, common.notifyStrings[common.NOTIFY_SUBTITLE_DOWNLOAD])
|
||||
|
||||
def test_notify(self, host, username, password):
|
||||
return self._notify_pmc("Testing Plex notifications from Sick Beard", "Test Notification", host, username,
|
||||
return self._notify_xbmc("Testing Plex notifications from Sick Beard", "Test Notification", host, username,
|
||||
password, force=True)
|
||||
|
||||
def update_library(self):
|
||||
|
@ -195,5 +91,4 @@ class PLEXNotifier:
|
|||
|
||||
return True
|
||||
|
||||
|
||||
notifier = PLEXNotifier
|
||||
|
|
Loading…
Reference in a new issue