summaryrefslogtreecommitdiff
path: root/odin/strm/cleanup_youtube
diff options
context:
space:
mode:
Diffstat (limited to 'odin/strm/cleanup_youtube')
-rwxr-xr-xodin/strm/cleanup_youtube61
1 files changed, 61 insertions, 0 deletions
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 @@
1#!@zsh@/bin/zsh
2
3function notmuch {
4 while true; do
5 result=$($_env NOTMUCH_CONFIG=${HOME}/.notmuch-rss-config @notmuch@/bin/notmuch "$@" 2>&1)
6 if ! [[ $result =~ "already locked" ]]; then
7 echo -nE $result
8 return
9 fi
10 sleep 2
11 done
12}
13
14tagFile=""
15
16function cleanup {
17 [[ -n "${tagFile}" ]] && rm -fv ${tagFile}
18}
19
20dir=/srv/media/youtube
21maxsize=$((1024 * 1024 * 1024 * 50))
22
23while [[ $(du -bs $dir | awk '{ print $1; }') -gt $maxsize ]]; do
24 find $dir -type f -not -path $dir/CACHEDIR.TAG -print0 | \
25 xargs -0 -- $_stat --printf '%W %Y %Z %X %n\0' | \
26 sort -t '\0' -z | \
27 awk -F '\0' '{ gsub("^\\S+\\s+\\S+\\s+\\S+\\s+\\S+\\s+", "", $1); printf "%s\0", $1; }' | \
28 xargs -0 -- rm -v
29done
30
31find $dir -xtype l -delete
32find $dir -type d -empty -delete
33
34notmuch search --output=messages --format=text 'is:cached' | \
35 while read id; do
36 untag=false
37 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)
38
39 printf "%s\n ‘%s’\n" ${id} ${dir}/${url}
40
41 if [[ -z "${url}" ]]; then
42 printf " Could not extract filename.\n"
43 untag=true
44 fi
45
46 if [[ -n "${url}" && ! -e "${dir}/${url}" ]]; then
47 printf " File vanished\n"
48 untag=true
49 fi
50
51 if ${untag}; then
52 if [[ -z "${tagFile}" ]]; then
53 tagFile=$(mktemp --tmpdir $$.tags.XXXXXXXXXX)
54 printf "Using %s\n" ${tagFile}
55 fi
56
57 { printf "-cached -- %s\n" ${id} >> ${tagFile} } && printf " Tagging ‘-cached’...\n"
58 fi
59 done
60
61[[ -n "${tagFile}" ]] && notmuch tag --batch --input=${tagFile}