Kodi Community Forum

Full Version: Request info from Kodi
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hello,

Wondered if anyone could please help. I'm currently trying to find a way to request information from Kodi using the Linux curl command. For example, what movie or music track name is currently playing. I also have the web server enabled in Kodi, I'm guessing this is possible but not exactly sure the best way of achieving this.

Any help would be much appreciated.

Regards.
Code:
curl -H "Content-Type: application/json" --data-binary "{\"jsonrpc\": \"2.0\", \"method\": \"Player.GetItem\", \"params\": { \"properties\": [\"title\"], \"playerid\": 0 }, \"id\": \"AudioGetItem\"}" http://localhost/jsonrpc

Quote:{"id":"AudioGetItem","jsonrpc":"2.0","result":{"item":{"id":1,"label":"Kalimba","title":"Kalimba","type":"song"}}}

Code:
curl -H "Content-Type: application/json" --data-binary "{\"jsonrpc\": \"2.0\", \"method\": \"Player.GetItem\", \"params\": { \"properties\": [\"title\"], \"playerid\": 1 }, \"id\": \"VideoGetItem\"}" http://localhost/jsonrpc

Quote:{"id":"VideoGetItem","jsonrpc":"2.0","result":{"item":{"id":1,"label":"Avengers: Age of Ultron","title":"Avengers: Age of Ultron","type":"movie"}}}

You could use this to determine what player is currently playing: http://kodi.wiki/view/JSON-RPC_API/Examp...playing.3F
Hello,

Many thanks for your quick reply, this seems to work a treat. Is it possible to strip out just the movie name, have tried using grep, awk to no success?.

Regards.
I have managed to find the solution with the following command, as a result this seems to successfully strip the movie name.

curl -sH "Content-Type: application/json" --data-binary "{\"jsonrpc\": \"2.0\", \"method\": \"Player.GetItem\", \"params\": { \"properties\": [\"title\"], \"playerid\": 1 }, \"id\": \"VideoGetItem\"}" http://localhost:8080/jsonrpc | cut -d ',' -f5 | cut -d '"' -f4

Alternatively if you know of any better ways of doing this.

Regards.
Hello,

Not sure, but can the same command be run from within a python script?. I understand the command is bash and have tried import.sh from python to run bash commands, but having trouble wrapping the command in a variable.

Regards.
Python has its own ways of communicating with Kodi and processing JSON-RPC replies.
Part of a bash script I wrote to query Kodi via JSONRPC. Script is called from a conky script and queries all my Kodi boxes, putting the results on my desktop.

I wrote this probably 2 years ago and my bash skills are better now, so apologies if it's hard to read, but it works !!! The full script takes at least one parameter ($xbmcAddress which is the ip address of machine) but also optional parameters to initiate playback of radio and tv channels from a terminal. $xbmcPort is currently set at the top of the script.

PHP Code:
PlayerId=$(curl ---data-binary '{ "jsonrpc": "2.0", "method": "Player.GetActivePlayers", "id": "mybash"}' -'content-type: application/json;' http://$xbmcAddress:$xbmcPort/jsonrpc)
PlayerId=(`echo "$PlayerId" | grep "result"`)
PlayerType=$( echo "$PlayerIdcut -d',' -f4)
PlayerType=$(echo "$PlayerTypecut -d':' -f2 sed 's/"//'sed 's/}//g' sed 's/]//')
#echo "Player type is : $PlayerType"
PlayerId=$(echo "$PlayerIdcut -d'{' -f3 sed 's/[^0-9]*//g')
NowPlayingRecord=$(curl ---data-binary '{"jsonrpc": "2.0", "method": "Player.GetItem", "params": { "properties": [ "showtitle", "streamdetails","title"], "playerid": '"$PlayerId"' }, "id": "VideoGetItem"}'
 
-'content-type: application/json;' http://$xbmcAddress:$xbmcPort/jsonrpc)

ShowTitle=$(echo "$NowPlayingRecordawk -Fshowtitle '{print $2}' awk -Fstreamdetails '{print $1}'sed 's/"://g'sed 's/[",]//g')

Type=$(echo "$NowPlayingRecordawk -Ftype '{print $2}' sed 's/"://g'sed 's/[",}]//g')

if [ 
"$Type"episode" ]
    
then 

        showDetails
=$(curl ---data-binary '{"jsonrpc": "2.0", "method": "Player.GetItem", "params": { "properties": [ "season", "episode"], "playerid": '"$PlayerId"' }, "id": "VideoGetItem"}' 
-'content-type: application/json;' http://$xbmcAddress:$xbmcPort/jsonrpc)

        
Episode=$(echo "$showDetailsawk -Fepisode '{print $2}' awk -Fid '{print $1}'sed 's/"://g'sed 's/[",]//g')
        
Title=$(echo "$showDetailsawk -Flabel '{print $2}' awk -Fseason '{print $1}'sed 's/"://g'sed 's/[",]//g')
        
Season=$(echo "$showDetailsawk -Fseason '{print $2}' awk -Ftype '{print $1}'sed 's/"://g'sed 's/[",]//g')

        echo 
"Playing $ShowTitle - S$Season E$Episode - $Title"
    
else
        if [ 
"$Type"movie" ]
            
then 
            movieDetails
=$(curl ---data-binary '{"jsonrpc": "2.0", "method": "Player.GetItem", "params": { "properties": [ "title","runtime"], "playerid": '"$PlayerId"' }, "id": "VideoGetItem"}'
 
-'content-type: application/json;' http://$xbmcAddress:$xbmcPort/jsonrpc)
    
            
currentPosition=$(curl ---data-binary '{"jsonrpc":"2.0","method":"Player.GetProperties","params":{"playerid":'"$PlayerId"',"properties":["time"]},"id":"1"}'
 
-'content-type: application/json;' http://$xbmcAddress:$xbmcPort/jsonrpc)
 # percentage , totaltime, time
            
currenttime=$(echo "$currentPositionawk -Fhours '{print $2}' awk -Fmilliseconds '{print $1}' sed 's/"://g'sed 's/[",]//g')
            
currenttime=$(($currenttime 60)) # currenttime now = hours
            
currenttime2=$(echo "$currentPositionawk -Fminutes '{print $2}' awk -Fseconds '{print $1}' sed 's/"://g'sed 's/[",]//g')
            
currenttime=$(($currenttime $currenttime2))

            
movieTitle=$(echo "$movieDetailsawk -Ftitle '{print $2}' awk -Ftype '{print $1}'sed 's/"://g'sed 's/[",]//g')
            
runtime=$(echo "$movieDetailsawk -Fruntime '{print $2}' awk -Ftitle '{print $1}' sed 's/"://g'sed 's/[",]//g'
            
runtime=$(( $runtime 60 ))
            
runtime=$(( $runtime $currenttime ))
            echo 
"Playing film - $movieTitle - $runtime mins remaining"
        
else
        if [ 
"$Type"channel" ]
            
then 
            Channel
=$(echo "$NowPlayingRecordawk -Flabel '{print $2}' awk -Ftitle '{print $1}'sed 's/"://g'sed 's/[",]//g')
            
Title=$(echo "$NowPlayingRecordawk -Ftitle '{print $2}' awk -Ftype '{print $1}' sed 's/"://g'sed 's/[",]//g')
#            echo "Title is $Title"
            
echo -"Watching \${color green}$Title\${color white} on \${color teal}$Channel\${color}"
    
        
else 
        if [ 
"$Type"unknown" 
            
then
            Video
=$(echo "$NowPlayingRecordawk -Flabel '{print $2}' awk -Fshowtitle '{print $1}'sed 's/"://g'sed 's/[",]//g')
            echo 
"Playing video - $Video"
            
fi    
        fi
    fi
fi
fi
exit 
Hello,

Many thanks for the script, I will certainly take a look at this. I'm currently developing for the Raspberry Pi, basically I have an OLED screen I would like to display notifications and currently playing media from my Kodi boxes, but I need to achieve this in Python due to the libraries required to drive the screen. I'm pretty new to Python but I'm aware you can import.sh to allow bash commands to run, but I cannot seem to wrap a curl command within the python script.

Any help would be greatly appreciated.

Regards.
2 nightowl7670

Have you read my reply above? If you are using Python, you don't need curl, bash and all that stuff to communicate with Kodi. A short example in pure Python:
Code:
import json
import urllib2
from contextlib import closing

URL    =    'http://127.0.0.1:8080/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_request = {
    'jsonrpc': '2.0',
    'method': 'VideoLibrary.GetMovies',
    'params': {'properties': ['imdbnumber', 'playcount'], 'sort': {'order': 'ascending', 'method': 'label'}},
    'id': '1'}
print json_request(kodi_request)
Hi Roman_V_M,

I did indeed read your reply, but now having an example should help me with what I need to achieve. Many thanks for your time and help.

Regards.
Hi Roman_V_M,

I am using the following code to get the currently playing movie:

kodi_request = {
'jsonrpc': '2.0',
'method': 'Player.GetItem',
'params': {'properties': ['title',],'playerid': 1}, 'id': 'VideoGetItem'}


I would like to filter the movie name only, I'm currently getting the following results:

{u'jsonrpc': u'2.0', u'id': u'VideoGetItem', u'result': {u'item': {u'title': u'A Mouse Tale', u'type': u'movie', u'id': 11, u'label': u'A Mouse Tale'}}}

Do you know if its possible to just return the label or title only, discarding the remaining result?.

Many thanks.
You should be able to access it like a dictionary.

Code:
print json_request(kodi_request)['result']['item']['title']
Hi Curti,

Brilliant! as this is just what I'm looking for. Could you suggest any good resources, so I can learn more about this kind of stuff. Basically integration with Kodi and Python?.

Regards.
I would refer to the Python documentation and Kodi PyDocs along with resources on the Add-ons forum / http://kodi.wiki/view/Add-ons
Hi Curti,

Thanks for the links, I will take a look at these. I'm just trying to figure out the best way to check if kodi's web server is available before I try to do a query. Or possibly just check if the URL is available, e.g: http://localhost:8080

Regards.
Pages: 1 2