Kodi 19.5 – jsonRPC to get movie list in Powershell

Kodi is primarily written and geared towards python users. This obviously makes it very difficult for people that prefer powershell to reference material and attempt to translate it into something understandable in powershell. The code below will demonstrate how you can get a full list of movies in kodi through the use of powershell.

First thing’s first, When I start to build code, I generally work the issue backwards. I will start out with an “Invoke-WebRequest”. This will be used to contact the Kodi API.

This is a basic form of a Json call to Kodi

$username ='username'  #username used to log on to kodi web interface
$pwd = 'password' #password for user ; used to log on to web interface
$hostname = 'IP Address' #IP Address of Kodi Host
$port = '8080' #port assignment of Kodi Host
$secpwd = ConvertTo-SecureString $pwd -AsPlainText -Force #Password is then converted to a secure password
$data = @{
    'jsonrpc' = '2.0' #json method used to for outlining Json verion
    'method' = 'VideoLibrary.GetMovies' #second of the Kodi API to call, in this case 'VideoLibrary.GetMovies'
    'id' = 1 
}
$json = $data | ConvertTo-Json -Depth 100 #Converting the API to call to a readable JSON format
$url = 'http://'+$hostname+':'+$port+'/jsonrpc' #the compiled URL of the above variables.
Invoke-WebRequest -Uri $url -Credential $mycreds -Body $json  -ContentType application/json -Method POST #putting it all together into an invoke webrequest call to send to Kodi
$username ='username'
$pwd = 'password'
$hostname = 'IP Address'
$port = '8080'
$secpwd = ConvertTo-SecureString $pwd -AsPlainText -Force
####################################################################
####################################################################
$data = @{
    'jsonrpc' = '2.0'
    'method' = 'VideoLibrary.GetMovies'
    'id' = 1
    params = @{
        properties = @(
            'genre',
            'playcount',
            'runtime',
            'tag'
        )

    }
}

$json = $data | ConvertTo-Json -Depth 100
$url = 'http://'+$hostname+':'+$port+'/jsonrpc'
$mycreds = New-Object System.Management.Automation.PSCredential ($username, $secpwd)
$webreq = Invoke-WebRequest -Uri $url -Credential $mycreds -Body $json  -ContentType application/json -Method POST
($webreq.content|convertfrom-json).result.movies

This will return a list of all your movies to the $webreq variable. Next, we’ll need to convert the output of the variable back from the returned JSON using “convertfrom-json”. the syntax will look like the example below.

$webreq.content|convertfrom-json

output 
id jsonrpc result
-- ------- ------
 1 2.0     @{limits=; movies=System.Object[]}

We’ll need to drill down into the $webreq.content variable, for this we’ll need to wrap $webreq.content in parenthesis ($webreq.content) and drill down to “.results.movies to see the final output. Each object will appear as the example below.

#example
genre     : {Horror}
label     : The Offering
movieid   : 3058
playcount : 0
runtime   : 5580
tag       : {}

Here is what the code looks like all put together.

#Returns a list movie and movie ID
#required param ID
$username ='user'
$pwd = 'password'
$hostname = 'ipaddress'
$port = '8080'
$secpwd = ConvertTo-SecureString $pwd -AsPlainText -Force
###########################################################################
###########################################################################
$data = @{
    'jsonrpc' = '2.0'
    'method' = 'VideoLibrary.GetMovies'
    'id' = 1
    params = @{
        properties = @(
            'genre',
            'playcount',
            'runtime',
            'tag'
        )
    }
}

$json = $data | ConvertTo-Json -Depth 100
$url = 'http://'+$hostname+':'+$port+'/jsonrpc'
$mycreds = New-Object System.Management.Automation.PSCredential ($username, $secpwd)
$webreq = Invoke-WebRequest -Uri $url -Credential $mycreds -Body $json  -ContentType application/json -Method POST 
$unwatchedfulllist = ($webreq.content|convertfrom-json).result.movies

The code above will give you a basic list of all movies including the genre, playcount, runtime, and tags associated with all of your movies. There are many properties that you can include in the output of your list. Once you have the list the way you want to see the data there are many things that you can do with it to shape the output to your liking.

Here is a full list of properties…

  "title"
      "playcount"
      "runtime"
      "director"
      "studio"
      "year"
      "plot"
      "album"
      "artist"
      "genre"
      "track"
      "streamdetails"
      "lastplayed"
      "fanart"
      "thumbnail"
      "file"
      "resume"
      "dateadded"
      "tag"
      "art"
      "rating"
      "userrating"
      "premiered"

Leave a Comment

Your email address will not be published. Required fields are marked *