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.

92 lines
3.6 KiB

from couchpotato.core.helpers.variable import splitString
14 years ago
from couchpotato.core.logger import CPLog
from couchpotato.core.notifications.base import Notification
from flask.helpers import json
14 years ago
import base64
import traceback
14 years ago
log = CPLog(__name__)
class XBMC(Notification):
listen_to = ['renamer.after']
def notify(self, message = '', data = {}, listener = None):
if self.isDisabled(): return
14 years ago
hosts = splitString(self.conf('host'))
successful = 0
for host in hosts:
if listener == "test":
# XBMC JSON-RPC version request
response = self.request(host, [
('JSONRPC.Version', {})
])
else:
response = self.request(host, [
('GUI.ShowNotification', {"title":"CouchPotato", "message":message}),
('VideoLibrary.Scan', {}),
])
try:
for result in response:
if (listener != "test" and result['result'] == "OK"):
successful += 1
elif (listener == "test"):
if (type(result['result']['version']).__name__ == 'int'):
# fail, only v2 and v4 return an int object
# v6 (as of XBMC v12(Frodo)) is required to send notifications
xbmc_rpc_version = str(result['result']['version'])
log.error("XBMC JSON-RPC Version: %s ; Notifications only supported for v6 [as of XBMC v12(Frodo)]", xbmc_rpc_version)
return False
elif (type(result['result']['version']).__name__ == 'dict'):
# success, v6 returns an array object containing
# major, minor and patch number
xbmc_rpc_version = str(result['result']['version']['major'])
xbmc_rpc_version += "." + str(result['result']['version']['minor'])
xbmc_rpc_version += "." + str(result['result']['version']['patch'])
log.debug("XBMC JSON-RPC Version: %s", xbmc_rpc_version)
# ok, XBMC version is supported, send the text message
self.notify(message = message, data = {}, listener = 'test-rpcversion-ok')
return True
except:
log.error('Failed parsing results: %s', traceback.format_exc())
return successful == len(hosts) * 2
14 years ago
def request(self, host, requests):
server = 'http://%s/jsonrpc' % host
data = []
for req in requests:
method, kwargs = req
data.append({
'method': method,
'params': kwargs,
'jsonrpc': '2.0',
'id': method,
})
data = json.dumps(data)
14 years ago
headers = {
'Content-Type': 'application/json',
}
14 years ago
if self.conf('password'):
base64string = base64.encodestring('%s:%s' % (self.conf('username'), self.conf('password'))).replace('\n', '')
headers['Authorization'] = 'Basic %s' % base64string
try:
log.debug('Sending request to %s: %s', (host, data))
rdata = self.urlopen(server, headers = headers, params = data, multipart = True)
response = json.loads(rdata)
log.debug('Returned from request %s: %s', (host, response))
return response
except:
log.error('Failed sending request to XBMC: %s', traceback.format_exc())
return []