You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
2.1 KiB

from couchpotato.core.event import addEvent
from couchpotato.core.helpers.encoding import tryUrlencode
14 years ago
from couchpotato.core.helpers.variable import cleanHost
14 years ago
from couchpotato.core.logger import CPLog
from couchpotato.core.notifications.base import Notification
from xml.dom import minidom
14 years ago
import traceback
14 years ago
log = CPLog(__name__)
class Plex(Notification):
def __init__(self):
super(Plex, self).__init__()
addEvent('renamer.after', self.addToLibrary)
def addToLibrary(self, group = {}):
if self.isDisabled(): return
14 years ago
log.info('Sending notification to Plex')
hosts = [cleanHost(x.strip() + ':32400') for x in self.conf('host').split(",")]
14 years ago
for host in hosts:
source_type = ['movie']
14 years ago
base_url = '%slibrary/sections' % host
14 years ago
refresh_url = '%s/%%s/refresh' % base_url
try:
14 years ago
sections_xml = self.urlopen(base_url)
xml_sections = minidom.parseString(sections_xml)
14 years ago
sections = xml_sections.getElementsByTagName('Directory')
14 years ago
14 years ago
for s in sections:
if s.getAttribute('type') in source_type:
url = refresh_url % s.getAttribute('key')
14 years ago
x = self.urlopen(url)
14 years ago
14 years ago
except:
log.error('Plex library update failed for %s: %s', (host, traceback.format_exc()))
14 years ago
return False
14 years ago
return True
def notify(self, message = '', data = {}, listener = None):
if self.isDisabled(): return
for host in [x.strip() + ':3000' for x in self.conf('host').split(",")]:
self.send({'command': 'ExecBuiltIn', 'parameter': 'Notification(CouchPotato, %s)' % message}, host)
return True
def send(self, command, host):
url = 'http://%s/xbmcCmds/xbmcHttp/?%s' % (host, tryUrlencode(command))
headers = {}
try:
self.urlopen(url, headers = headers, show_error = False)
except:
log.error("Couldn't sent command to Plex")
return False
log.info('Plex notification to %s successful.', host)
return True