summaryrefslogtreecommitdiff
path: root/odin/strm/cleanup_youtube
blob: 06574adbb80508df32fe9677501017702aa2ed4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!@zsh@/bin/zsh

set -e

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}
}

urldecode() { local x="${$(cat)//+/ }"; echo -e "${x//\%/\\x}" }

dir=/srv/media/youtube
@rollingdirectory@/bin/rolling-directory ${dir} 50Gi

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|' | urldecode )

        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}

exit 0