For my management purposes, it is easier most times to just run a scan or clean script from where I'm at instead of going to one of my Apple TV boxes to manually do a library scan or clean. In fact, sometimes I manage my media remotely (outside of the house) so being able to send a JSON command via Curl works out well. You may also want to do periodic or nightly scans / cleans, so this can tie in perfectly with a cron job.
Prerequisites:
1. MrMC must allow remote connections
2. Computer with curl installed
1. To allow remote connections to MrMC, go to Settings --> Services. It should be under the web server section. You'll want to enable "allow remote control via HTTP". Default port is fine; this is what I reference in the commands below. I didn't bother setting a username or password since this is only accessible inside my LAN.
2. Curl is probably the most popular method of manipulating / transferring a URL string from a command prompt. I have a FreeNAS server running MythTV, so this works out well in my case.
Essentially you are sending an HTTP POST to the MrMC web server that you enabled in step 1 that contains a JSON method & parameters to interact the with MrMC API. In my examples, I am using the VideoLibrary.Scan and VideoLibrary.Clean methods. Since I want this to be unattended and nonintrusive to the client, I pass the params "showdialogs": false. If left true, the Clean method would end up prompting to keep / remove at the end, and that defeats the purpose of having it run remotely! If you left the showdiags true for the Scan method, anyone using the client would see the message in the top right that the library is being updated. If you would like more info on all the methods available, check out: http://kodi.wiki/view/JSON-RPC_API/v6
Well enough talk. Here is the main code portion that you can incorporate into any shell script.
First the Scan:
Code: Select all
curl -s -L -m3 --connect-timeout 5 -X POST -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","method":"VideoLibrary.Scan", "params": { "showdialogs": false }, "id":1}' http://<MrMC_host>:8080/jsonrpc > /dev/null
Code: Select all
curl -s -L -m3 --connect-timeout 5 -X POST -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","method":"VideoLibrary.Clean", "params": { "showdialogs": false }, "id":1}' http://<MrMC_host>:8080/jsonrpc > /dev/null
-s = silent, as in no output from Curl (does not suppress the JSON API output however).
-m3 = max time of 3 seconds to stay connected. I just want to pass the method request and leave.
--connect-timeout 5 = timeout after 5 seconds. If the client is down or MrMC is not running, don't spend more time than necessary trying to connect.
If you happened to set a username and/or password when you enabled remote access in MrMC settings, you can use the -u option to pass a "username:password" or just a username and have it prompt you for the password.
At the very end, I do redirect all messages sent to stout (screen) to the black hole of /dev/null. If you want JSON output, then you can leave this off, but for my script all I want is the curl exit code. If curl exits with anything but 0, then there was an error and so I know the scan or clean didn't run. For more info on curl: https://curl.haxx.se/docs/manpage.html
I hope this helps anyone looking for a way to script library updates or cleans. I also hope this prompts the devs to keep the API included in future releases