Skip to content

Instantly share code, notes, and snippets.

@gkralik
Last active June 4, 2024 11:31
Show Gist options
  • Save gkralik/e89f2ec6978f92cbb4ad3ecd53fb1168 to your computer and use it in GitHub Desktop.
Save gkralik/e89f2ec6978f92cbb4ad3ecd53fb1168 to your computer and use it in GitHub Desktop.
sabnzbd post processing script: update kodi library
#!/usr/bin/bash
# Copyright 2017 Gregor Kralik <g.kralik (at) gmail.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# PP script for sabnzbd: moves finished downloads to folders depending
# on category and updates Kodi library
set -e
KODI_HOST='127.0.0.1'
KODI_PORT=8080
UNWANTED_FILES_EXT=".nfo .srr .sfv .nzb .jpg .rar"
declare -A CATEGORY_PATH_MAP
CATEGORY_PATH_MAP=(
[category1]="/data/category1"
[category2]="/data/category2"
)
script_name=$0
directory=$1
original_nzb_name=$2
job_name=$3
report_number=$4
category=$5
group=$6
postproc_status=$7
url=$8
echo "execute $script_name $@"
# check postproc stat == 0
if [[ $postproc_status -ne 0 ]]; then
echo "PP status is $postproc_status, aborting..."
exit 1
fi
kodi_update_video_library()
{
local path=$1
curl -s -H 'Content-Type: application/json' \
--data-binary "{\"jsonrpc\": \"2.0\", \"method\": \"VideoLibrary.Scan\", \"params\": {\"directory\": \"$path\"}, \"id\": \"1\"}" \
"http://$KODI_HOST:$KODI_PORT/jsonrpc"
echo ""
}
kodi_show_gui_notification()
{
local title=$1
local message=$2
curl -s -H 'Content-Type: application/json' \
--data-binary "{\"jsonrpc\": \"2.0\", \"method\": \"GUI.ShowNotification\", \"params\": {\"title\": \"$title\", \"message\": \"$message\"}, \"id\": \"2\"}" \
"http://$KODI_HOST:$KODI_PORT/jsonrpc"
echo ""
}
# check if directory exists
if [[ ! -d $directory ]]; then
echo "directory $directory does not exist"
exit 1
fi
# remove unwanted files
echo "removing unwanted files with extensions ($UNWANTED_FILES_EXT)"
for ext in $UNWANTED_FILES_EXT; do
find "$directory" -type f -name *$ext -delete
done
# if there is a sample folder, delete it
if [[ -d $directory/Sample ]]; then
echo "removing Sample folder"
rm -rf "$directory/Sample"
fi
# if a Subs folder exists, move files up and delete it
if [[ -d $directory/Subs ]]; then
echo "moving subtitle files"
mv -v "$directory/Subs/"* "$directory"
rmdir "$directory/Subs" || true
fi
# test -z == zero-length string, "${array[key]+foo}" returns foo if exists, nothing otherwise
if [[ -z "${CATEGORY_PATH_MAP[$5]+1}" ]]; then
echo "no folder mapping for category $category found, nothing to do"
fi
# move directory to Kodi library dir
target_directory="${CATEGORY_PATH_MAP[$category]}"
original_directory_name=$(basename "$directory")
echo "moving files to $target_directory"
mv -v "$directory" "$target_directory"
# make sure the target is named correctly (after $job_name)
# rename $target_directory/$original_directory_name to $job_name if it differs
if [[ $original_directory_name != $job_name ]]; then
echo "renaming directory in target to $job_name"
mv -v "$target_directory/$original_directory_name" "$target_directory/$job_name"
fi
echo "updating Kodi video library"
kodi_update_video_library "$target_directory"
kodi_show_gui_notification 'Video file added' "New video $job_name added to library."
# this will be displayed in sabnzbd
echo "$script_name successful"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment