Browse Source

Use jsonrpc for xbmc request. fix #927 #945

pull/992/merge
Ruud 13 years ago
parent
commit
09f723bda5
  1. 55
      couchpotato/core/notifications/xbmc/main.py
  2. 2
      couchpotato/core/plugins/base.py

55
couchpotato/core/notifications/xbmc/main.py

@ -1,7 +1,7 @@
from couchpotato.core.helpers.encoding import tryUrlencode
from couchpotato.core.helpers.variable import splitString from couchpotato.core.helpers.variable import splitString
from couchpotato.core.logger import CPLog from couchpotato.core.logger import CPLog
from couchpotato.core.notifications.base import Notification from couchpotato.core.notifications.base import Notification
from flask.helpers import json
import base64 import base64
log = CPLog(__name__) log = CPLog(__name__)
@ -17,28 +17,43 @@ class XBMC(Notification):
hosts = splitString(self.conf('host')) hosts = splitString(self.conf('host'))
successful = 0 successful = 0
for host in hosts: for host in hosts:
if self.send({'command': 'ExecBuiltIn', 'parameter': 'Notification(CouchPotato, %s)' % message}, host): response = self.request(host, [
successful += 1 ('GUI.ShowNotification', {"title":"CouchPotato", "message":message}),
if self.send({'command': 'ExecBuiltIn', 'parameter': 'XBMC.updatelibrary(video)'}, host): ('VideoLibrary.Scan', {}),
successful += 1 ])
for result in response:
if result['result'] == "OK":
successful += 1
return successful == len(hosts) * 2 return successful == len(hosts) * 2
def send(self, command, host): 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)
url = 'http://%s/xbmcCmds/xbmcHttp/?%s' % (host, tryUrlencode(command)) headers = {
'Content-Type': 'application/json',
}
headers = {}
if self.conf('password'): if self.conf('password'):
headers = { base64string = base64.encodestring('%s:%s' % (self.conf('username'), self.conf('password'))).replace('\n', '')
'Authorization': "Basic %s" % base64.encodestring('%s:%s' % (self.conf('username'), self.conf('password')))[:-1] headers['Authorization'] = 'Basic %s' % base64string
}
log.debug('Sending request to %s: %s', (host, data))
try: rdata = self.urlopen(server, headers = headers, params = data, multipart = True)
self.urlopen(url, headers = headers, show_error = False) response = json.loads(rdata)
except: log.debug('Returned from request %s: %s', (host, response))
log.error("Couldn't sent command to XBMC")
return False return response
log.info('XBMC notification to %s successful.', host)
return True

2
couchpotato/core/plugins/base.py

@ -124,7 +124,7 @@ class Plugin(object):
try: try:
if multipart: if multipart:
log.info('Opening multipart url: %s, params: %s', (url, [x for x in params.iterkeys()])) log.info('Opening multipart url: %s, params: %s', (url, [x for x in params.iterkeys()] if isinstance(params, dict) else 'with data'))
request = urllib2.Request(url, params, headers) request = urllib2.Request(url, params, headers)
cookies = cookielib.CookieJar() cookies = cookielib.CookieJar()

Loading…
Cancel
Save