Skip to content

Instantly share code, notes, and snippets.

@SwagDevOps
Last active September 22, 2017 22:02
Show Gist options
  • Select an option

  • Save SwagDevOps/3619b7510168cff2d609f724fa2a2a6a to your computer and use it in GitHub Desktop.

Select an option

Save SwagDevOps/3619b7510168cff2d609f724fa2a2a6a to your computer and use it in GitHub Desktop.
#!/usr/bin/env sh
# config -------------------------------------------------------------
IMPORT_DIR='/var/kodi/imports'
OUTPUT_DIR='/var/kodi/series'
FILES_TIME='8'
# functions ----------------------------------------------------------
import_files() {
(cd && find -L "${IMPORT_DIR}" -maxdepth 2 -type f "$@")
}
import() {
(
cd "${IMPORT_DIR}" || exit 1
/usr/bin/env tvshow import . \
--strategy copy --verbose --output "${OUTPUT_DIR}"
)
}
notify() {
local ttl="${2:-165000}"
xbmc-command "Notification(Import, ${1}, ${ttl})"
}
notify_success() {
local count="${1:-?}"
local htime="$(elapsed true)"
local stime="$(elapsed)"
local messg="${count} fichiers importés en ${htime} (${stime} secondes)"
test "${count}" -gt 0 || return 0
beep -f 500
echo "${messg}"
notify "${messg}"
log "${messg}"
}
log() {
logger -t $(basename "$0") -- "${1}"
}
# Removes files older than 8 days
#
# FILES_TIME (mtime)
clean() {
import_files -mtime +"${FILES_TIME:-8}" -delete
}
purge() {
rm -rf "${IMPORT_DIR}/"*
}
elapsed() {
local elapsed=$(expr $(date +%s) - $START_TIME)
test -n "$1" && test "$1" = true && {
printf "%d:%0.2d\n" "$(expr $elapsed / 60)" "$(expr $elapsed % 60)"
} || {
echo "${elapsed}"
}
}
# execution ----------------------------------------------------------
START_TIME=$(date +%s)
main() {
HOME=$(getent passwd $(whoami) | cut -d: -f6)
import_files || return 0
local count="$(import | wc -l)" && {
clean
test "${count}" -gt 0 || return 0
xbmc-library update
notify_success "${count}"
}
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment