2016-04-05, 17:45
Hi Curti,
With the help of your code, I have managed to cobble together a small script that will check the host, and if Kodi is currently playing. I would like to be able to check multiple hosts at the same time, any helpful tips would be grateful from your python knowledge.
#!/usr/bin/python
import socket
import requests
import json
import urllib2
from contextlib import closing
PORT = '8080'
HOST = 'localhost'
URL = "http://" + HOST + ":" + str(PORT) + "/jsonrpc"
HEADER = {'Content-Type': 'application/json'}
def json_request(kodi_request):
request = urllib2.Request(URL, json.dumps(kodi_request), HEADER)
with closing(urllib2.urlopen(request)) as response:
return json.loads(response.read())
kodi_playing = {
'jsonrpc': '2.0',
'method': 'Player.GetActivePlayers', 'id':1}
kodi_request = {
'jsonrpc': '2.0',
'method': 'Player.GetItem',
'params': {'properties': ['title',],'playerid': 1}, 'id': 'VideoGetItem'}
def playerStatus():
if json_request(kodi_playing)['result']:
print 'Playing: ',json_request(kodi_request)['result']['item']['title']
else:
print 'Nothing is playing.'
try:
r = requests.head("http://localhost:8080")
print "Host is up!"
playerStatus()
except requests.ConnectionError:
print("Host is down!")
exit()
Please excuse the above code, as my Python knowledge is very limited.
Regards.
With the help of your code, I have managed to cobble together a small script that will check the host, and if Kodi is currently playing. I would like to be able to check multiple hosts at the same time, any helpful tips would be grateful from your python knowledge.
#!/usr/bin/python
import socket
import requests
import json
import urllib2
from contextlib import closing
PORT = '8080'
HOST = 'localhost'
URL = "http://" + HOST + ":" + str(PORT) + "/jsonrpc"
HEADER = {'Content-Type': 'application/json'}
def json_request(kodi_request):
request = urllib2.Request(URL, json.dumps(kodi_request), HEADER)
with closing(urllib2.urlopen(request)) as response:
return json.loads(response.read())
kodi_playing = {
'jsonrpc': '2.0',
'method': 'Player.GetActivePlayers', 'id':1}
kodi_request = {
'jsonrpc': '2.0',
'method': 'Player.GetItem',
'params': {'properties': ['title',],'playerid': 1}, 'id': 'VideoGetItem'}
def playerStatus():
if json_request(kodi_playing)['result']:
print 'Playing: ',json_request(kodi_request)['result']['item']['title']
else:
print 'Nothing is playing.'
try:
r = requests.head("http://localhost:8080")
print "Host is up!"
playerStatus()
except requests.ConnectionError:
print("Host is down!")
exit()
Please excuse the above code, as my Python knowledge is very limited.
Regards.