From 04fe40544c0ef912bdd72a4146f88827eb3bf5f9 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Fri, 6 Apr 2018 14:17:41 +0200 Subject: strm --- odin/strm/cleanup_youtube | 61 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100755 odin/strm/cleanup_youtube (limited to 'odin/strm/cleanup_youtube') diff --git a/odin/strm/cleanup_youtube b/odin/strm/cleanup_youtube new file mode 100755 index 00000000..0df15d0b --- /dev/null +++ b/odin/strm/cleanup_youtube @@ -0,0 +1,61 @@ +#!@zsh@/bin/zsh + +function notmuch { + while true; do + result=$($_env NOTMUCH_CONFIG=${HOME}/.notmuch-rss-config @notmuch@/bin/notmuch "$@" 2>&1) + if ! [[ $result =~ "already locked" ]]; then + echo -nE $result + return + fi + sleep 2 + done +} + +tagFile="" + +function cleanup { + [[ -n "${tagFile}" ]] && rm -fv ${tagFile} +} + +dir=/srv/media/youtube +maxsize=$((1024 * 1024 * 1024 * 50)) + +while [[ $(du -bs $dir | awk '{ print $1; }') -gt $maxsize ]]; do + find $dir -type f -not -path $dir/CACHEDIR.TAG -print0 | \ + xargs -0 -- $_stat --printf '%W %Y %Z %X %n\0' | \ + sort -t '\0' -z | \ + awk -F '\0' '{ gsub("^\\S+\\s+\\S+\\s+\\S+\\s+\\S+\\s+", "", $1); printf "%s\0", $1; }' | \ + xargs -0 -- rm -v +done + +find $dir -xtype l -delete +find $dir -type d -empty -delete + +notmuch search --output=messages --format=text 'is:cached' | \ + while read id; do + untag=false + url=$(notmuch show --format=raw "$id" | grep 'http://odin.asgard.yggdrasil/youtube' | sed -r 's|^.*href="http://odin.asgard.yggdrasil/youtube/([^"]+)".*$|\1|' | sed -f /usr/lib/url_unescape.sed) + + printf "%s\n ‘%s’\n" ${id} ${dir}/${url} + + if [[ -z "${url}" ]]; then + printf " Could not extract filename.\n" + untag=true + fi + + if [[ -n "${url}" && ! -e "${dir}/${url}" ]]; then + printf " File vanished\n" + untag=true + fi + + if ${untag}; then + if [[ -z "${tagFile}" ]]; then + tagFile=$(mktemp --tmpdir $$.tags.XXXXXXXXXX) + printf "Using %s\n" ${tagFile} + fi + + { printf "-cached -- %s\n" ${id} >> ${tagFile} } && printf " Tagging ‘-cached’...\n" + fi + done + +[[ -n "${tagFile}" ]] && notmuch tag --batch --input=${tagFile} -- cgit v1.2.3