Kodi Community Forum

Full Version: rtorrent notification and conditional scan to library
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey, I wanted a way to keep my XBMC library in sync with the downloads from rtorrent/rutorrent in an event based way. So I've come up with a simple script that notifies XBMC on any download completion and also scans in those downloads that go somewhere below the video source directories. Maybe someone finds it useful

in ~/.rtorrent.rc
Code:
system.method.set_key = event.download.finished,update_xbmc,"execute=/home/xbmc/xbmcnotify.sh,\"Download finished\",\"$d.get_name=\",\"$d.get_base_path=\""

the script, saved as ~/.xbmcnotify.sh

Code:
#!/bin/bash
# Send a GUI notification to XBMC and optionally scan a directory to video library
# Checks that the directory begins with a path from XBMC video sources
user="xbmc"
pass="xbmc"
url="http://localhost:8080/jsonrpc"

if [ -z "$2" ]; then
echo "Usage: $0 title message [directory]" 1>&2
exit 1
fi

type curl >/dev/null 2>&1 || { echo >&2 "ERROR: Cannot find curl, aborting."; exit 1; }

curl -o /dev/null -s -X POST --fail -H "Content-type: application/json" -d "{\"jsonrpc\":\"2.0\",\"method\":\"GUI.ShowNotification\",\"params\":{\"title\":\"$1\",\"message\":\"$2\"}}" -u $user:$pass $url || { echo >&2 "ERROR: Check that the user/pass/url are correct and XBMC webserver is enabled and running."; exit 1; }

if [ -z "$3" ]; then
exit
else
sources=($(curl -s -X POST -H 'Content-type: application/json' -d '{"jsonrpc":"2.0","method":"Files.GetSources","params":{"media":"video"},"id":1}' -u $user:$pass $url | grep -Po '(?<="file":").*?[^\\](?=")'))
inarray() { local n=$1 h; shift; for h; do [[ $n = "$h"* ]] && return; done; return 1; }
if inarray $3 "${sources[@]}"; then
  curl -o /dev/null -s -X POST -H "Content-type: application/json" -d "{\"jsonrpc\":\"2.0\",\"method\":\"VideoLibrary.Scan\",\"params\":{\"directory\":\"$3\"}}" -u $user:$pass $url
else
  echo "NOTICE: Directory did not match any of the video sources, not scanning to library" 1>&2
  exit 0
fi
fi