== 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
```
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!