Request info from Kodi
#16
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.
Reply
#17
If you would like to check multiple hosts you could store the hosts in a list. You could iterate through the items in the list; and update your function(s) to accept a parameter for the host:port.

Code:
hosts = ['127.0.0.1:80', '192.168.12.2:8080']

def json_request(host, kodi_request):
    pass

for host in hosts:
    json_request(host)
Reply
#18
Hello Curti,

I will have a play when I get time, still familiarising myself with Python. Do you think modifying the original json_request function, to accept additional parameters would be the best approach, or create a new one?

Regards.
Reply
#19
You could also pass a host to the python script on the command line using sys.argv[1] . I guess it all depends on how you are calling it in the first place. The bash script I referenced earlier takes an IP address as a parameter and is called 3 times although either method will obviously work. I just wrote it that way around because it was easier for me at the time to test it against one machine.

You can take a look at it here -> https://www.dropbox.com/s/ae423d2lbm18tk...ck.sh?dl=0 There are a lot of json calls in it that you might find useful and some logic to determine whether audio or video is playing and get and parse the required stuff.

I also re-wrote your python code to use a list, as suggested by curti and the link for that is https://www.dropbox.com/s/56wys2lo4pxzrz...di.py?dl=0

Having addressed the basics, I might go on and convert my bash script to python Smile It would probably be much lighter resource-wise (although the bash stuff runs well enough) and probably a lot more flexible. It's much easier to work with json in python than bash !!!
Learning Linux the hard way !!
Reply
#20
Hello,

My project was to basically have this information output to a raspberry pi screen, I have 5 Kodi boxes that I would like to query host and playing information, but my python skills are pretty basic at the min. I have also attempted this in bash using curl requests that seems to work, but still requires python to drive the OLED screen I'm currently using. This is why I decided on python to do it all, otherwise I'm having to call multiple commands.

It looks like you've been pretty busy with that bash script, its similar to what I was using but somewhat smaller in size Smile

Many thanks for the updated python script, I will give this a test at some point. I'm guessing this won't update play or host information in real time, e.g. if a machine isn't connected to the network?. If not I'm sure this can be done with a slight adjustment to the code.

Regards.
Reply
#21
Yeah, that bash script has been added to over the years lol.

I played around a bit more with the python script and it now correctly distinguishes between audio and video. If playing audio it basically just dumps out the title if its a live stream, or the artist, track, album & duration of track if its playing one. Video will give you either a movie title, a show title and episode title or a tv channel and show title, dependant on what's currently playing.

There is a lot more info that could be pulled out, even from the basic queries it currently does and the queries could be extended further if you desired.

The script is currently a one-shot deal, so you call it and it iterates through the hosts, gets the info and then stops. If you want it to be real-time, it would be easy to just run it in a loop (with some sort of delay in there so you aren't querying Kodi too fast).

I've also added a timeout of 3 seconds to the first HTTP call, because it's possible on linux for Kodi to have crashed, but the OS still be running in which case, the socket can still be open so the script can connect but will never get an answer. This is handled correctly now.

Anyway, you should be able to modify it to get your desired results. It was much easier to write than the corresponding bash script and it's much shorter so I think I'll be adding to it myself and then using the python version rather than the bash one.

Link for updated script is https://www.dropbox.com/s/56wys2lo4pxzrz...di.py?dl=0
Learning Linux the hard way !!
Reply
#22
I see you have now quickly migrated your bash script to python, and seems to run very well with Kodi. I think adding the timeout to the host discovery also makes sense. I think putting this in a loop will be nice to see, as the hosts will then update in really time.

The RiPi OLED screen works differently to a normal screen, you have to manually refresh it to clear or update the screen. Obviously this will have an impact when the Pi is querying Kodi hosts to update information. I've managed to get some good results but requires further work.

I'm currently struggling to get time to have a play, but many thanks for your time and work on this.
Reply

Logout Mark Read Team Forum Stats Members Help
Request info from Kodi0