Skip to content

Instantly share code, notes, and snippets.

@Aanok
Last active March 13, 2020 21:29
Show Gist options
  • Save Aanok/d2340812c19db1d061050552ddf01f3b to your computer and use it in GitHub Desktop.
Save Aanok/d2340812c19db1d061050552ddf01f3b to your computer and use it in GitHub Desktop.
#!/bin/bash
# Dependencies: curl, jq, jftui config file
VERSION='1.0'
########## HELPER FUNCTIONS ###########
usage() {
echo "jfadm v${VERSION}
-l
List libraries.
-s <idx>
Request scan of library by index <idx> as reported by -l.
-log
Print most recent server log."
}
missing_arg_error() {
echo "Error: \""$1"\" requires a non-empty option argument." >&2
exit 1
}
number_assert_ok() {
local regex='[[:digit:]]+'
[[ ! "$1" =~ $regex ]] && { echo 'Error: argument should be a number.' >&2; exit 1; }
[[ "$1" -lt 1 ]] && { echo 'Error: number should be greater than 0.' >&2; exit 1; }
}
jf_request() {
curl -s -H "accept: application/json" -H "content-Type: application/json" -H "x-emby-token: ${token}" "${server}${1}"
}
jf_request_post() {
curl -s -H "accept: application/json" -H "content-Type: application/json" -H "x-emby-token: ${token}" --data-raw "${2}" "${server}${1}"
}
#######################################
# check dependencies
command -v jq >/dev/null || { echo "Error: program jq not found." >&2; exit 1; }
command -v curl >/dev/null || { echo "Error: program curl not found." >&2; exit 1; }
# check argcount
[[ "$#" -lt 1 ]] && { usage; exit 1; }
# import settings
. "${XDG_CONFIG_HOME:-${HOME}/.config}"/jftui/settings
while [[ -n "$1" ]]; do
case "$1" in
-l)
json_reply="$(jf_request "/library/mediafolders")"
mapfile -t libraries <<< "$(jq -r '.Items[].Name' <<< "${json_reply}")"
for i in "${!libraries[@]}"; do
echo "${i}. ${libraries[${i}]}"
done
;;
-s)
[[ -z "$2" ]] && missing_arg_error 'list'
number_assert_ok "$2"
json_reply="$(jf_request "/library/mediafolders")"
library_id="$(jq -r --arg lib_index "${2}" '.Items[($lib_index | tonumber)].Id' <<< "${json_reply}")"
jf_request_post "/items/${library_id}/refresh?recursive=true&ImageRefreshMode=Default&MetadataRefreshMode=Default&ReplaceAllImages=false&ReplaceAllMetadata=false"
shift
;;
-log)
log_name="$(jq -r '.[0].Name' <<< "$(jf_request "/system/logs")")"
jf_request "/system/logs/log?name=${log_name}"
;;
*)
usage
exit 0
;;
esac
shift
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment