Regular → Executable
+33
-18
@@ -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
|
||||||
|
|
||||||
url = 'http://%s/xbmcCmds/xbmcHttp/?%s' % (host, tryUrlencode(command))
|
data = []
|
||||||
|
for req in requests:
|
||||||
|
method, kwargs = req
|
||||||
|
data.append({
|
||||||
|
'method': method,
|
||||||
|
'params': kwargs,
|
||||||
|
'jsonrpc': '2.0',
|
||||||
|
'id': method,
|
||||||
|
})
|
||||||
|
data = json.dumps(data)
|
||||||
|
|
||||||
|
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
|
||||||
}
|
|
||||||
|
|
||||||
try:
|
log.debug('Sending request to %s: %s', (host, data))
|
||||||
self.urlopen(url, headers = headers, show_error = False)
|
rdata = self.urlopen(server, headers = headers, params = data, multipart = True)
|
||||||
except:
|
response = json.loads(rdata)
|
||||||
log.error("Couldn't sent command to XBMC")
|
log.debug('Returned from request %s: %s', (host, response))
|
||||||
return False
|
|
||||||
|
return response
|
||||||
|
|
||||||
log.info('XBMC notification to %s successful.', host)
|
|
||||||
return True
|
|
||||||
|
|||||||
@@ -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()
|
||||||
|
|||||||
Reference in New Issue
Block a user