== Automatically refresh the library when downloads finish ==
Many clients, [[ https://github.com/transmission/transmission/wiki/Scripts#on-torrent-completion | notably Transmission ]], can call a script when downloads finish. Meanwhile, Kodi can be controlled via the HTTP API in many ways, [[ https://kodi.wiki/view/HOW-TO:Remotely_update_library | including to refresh the library ]]. You can combine all this to automatically call a clean and update of the library when a download has finished.
First, make sure that remote control over HTTP is allowed, via {nav name=Settings, icon=cog > Service settings > Control > Allow control of Kodi via HTTP > ON}.
Now, save a script file somewhere (ex. `~/bin/kodi-library-refresh.sh`) with the following contents, editing the variables at the start for your setup:
```
lang=bash, name=~/bin/kodi-library-refresh.sh
#!/bin/bash
# Change these values to yours if you aren't just blithely using Kodi's default settings
USERNAME="kodi"
PASSWORD="kodi"
HOSTNAME="localhost"
PORT="8080"
# First, we scan for new items
curl --data-binary '{ "jsonrpc": "2.0", "method": "VideoLibrary.Scan", "id": "mybash"}' -H 'content-type: application/json;' http://${USERNAME}:${PASSWORD}@${HOSTNAME}:${PORT}/jsonrpc
curl --data-binary '{ "jsonrpc": "2.0", "method": "AudioLibrary.Scan", "id": "mybash"}' -H 'content-type: application/json;' http://${USERNAME}:${PASSWORD}@${HOSTNAME}:${PORT}/jsonrpc
# Now, we clean up anything that's no longer available
curl --data-binary '{ "jsonrpc": "2.0", "method": "VideoLibrary.Clean", "id": "mybash"}' -H 'content-type: application/json;' http://${USERNAME}:${PASSWORD}@${HOSTNAME}:${PORT}/jsonrpc
curl --data-binary '{ "jsonrpc": "2.0", "method": "AudioLibrary.Clean", "id": "mybash"}' -H 'content-type: application/json;' http://${USERNAME}:${PASSWORD}@${HOSTNAME}:${PORT}/jsonrpc
```
{F496}
Make sure to make this script executable. From the terminal, `chmod +x ~/bin/kodi-library-refresh.sh` (or you can probably right-click and go into properties in your file manager of choice).
Now just open Transmission and go to {nav Edit > Preferences > Downloading > Incomplete > Call script when torrent is completed} and point that setting towards your script, et voila!
----
== Refresh and clean the library periodically ==
If you've saved the script above and made it executable, you could also just run it on a schedule, too. Probably the best way would be via your user crontab, which you can edit:
* via `crontab -e` from a terminal to edit your crontab directly with a text editor
* or via your desktop environment's task scheduler
** For KDE, this should be called {nav Task Scheduler} and if not already available on your system, this KCM module can be installed via `sudo apt install kde-config-cron` on Debian/Ubuntu systems.
** For GNOME systems apparently [[ https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=808060 | nobody maintains the equivalent GUI for GNOME anymore ]], so you'll have to use one of the above options.
If you're going the direct crontab editing route, see https://help.ubuntu.com/community/CronHowto for more information, or just cargo-cult copy the following entry into your crontab (having opened it up for editing via `crontab -e`) to get it to run twice a day:
```
name=crontab, lang=crontab
# Will run the Kodi library refresh script twice a day
0 */12 * * * ${HOME}/bin/kodi-library-refresh.sh > /tmp/kodi-library-refresh.log
```
You could also copy the `kodi-library-refresh.sh` script to `/etc/cron.daily` to just get it to run once every day, or even `/etc/cron.hourly` for every hour (again, make sure it's set as executable, otherwise it will not be run).
----
== Take screenshots from a script ==
```
lang=bash, name=~/bin/kodi-take-screenshot.sh
#!/bin/bash
# Change these values to yours if you aren't just blithely using Kodi's default settings
USERNAME="kodi"
PASSWORD="kodi"
HOSTNAME="localhost"
PORT="8080"
# First time (per session?) it'll prompt for a folder to use, future invocations will result in screenshots in that same folder
curl --data-binary '{ "jsonrpc": "2.0", "method": "Input.ExecuteAction", "params": {"action":"screenshot"}, "id": "screenshot-script"}' -H 'content-type: application/json;' http://${USERNAME}:${PASSWORD}@${HOSTNAME}:${PORT}/jsonrpc
```