summaryrefslogtreecommitdiff
path: root/odin/strm/send_media
blob: 4f3e576b3cc94b09186f9acccffb82ef5a447494 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!@zsh@/bin/zsh

logTag=${0:t}

exec 1> >(logger -t "$logTag" -p news.notice)
exec 2> >(logger -t "$logTag" -p news.error)

debug() { logger -t "$logTag" -p news.debug }

function notmuch {
    msg1=1
    while true; do
        result=$(env NOTMUCH_CONFIG=${HOME}/.notmuch-rss-config @notmuch@/bin/notmuch $@)
        if ! [[ "$result" =~ "already locked" ]]; then
            echo -nE "$result"
            return
        else
            [[ $msg1 == "1" ]] && echo "Already locked" >&2
            msg1=0
        fi
        sleep 2
    done
}

urldecode() { : "${*//+/ }"; echo -e "${_//%/\\x}"; }

force=0
if [[ "$1" == "-f" ]]; then
    shift
    force=1
    if [[ -n "$1" && "$1" -eq "$1" ]] 2>/dev/null; then
        force="$1"
        shift
    fi
fi

# print $force

msg=1

typeset -a checkedIds
checkedIds=()
typeset -a ids
ids=()

while {
    results=$(notmuch search --sort=oldest-first --output=messages "( tag:inbox AND is:cached AND is:media ) AND NOT ( tag:killed OR tag:failed OR tag:tv )")
    ids=("${(f)results}")
    for i in $checkedIds; do
        ids=(${ids#$i})
    done
    checkedIds=($checkedIds $ids)
} && [[ -n "${ids}" ]]; do
    url=""
    for id ($ids); do
        message=$(notmuch search --output=files --duplicate=1 ${id})

        ( flock -sn 9 || { printf "Could not lock ‘%s’\n" ${id} | debug; exit 0 }
          
          url=$(notmuch show ${id} | sed -r '/<a[^>]+href="http:\/\/odin\.asgard\.yggdrasil\//!d; s!^.*<a[^>]+href="(http://odin\.asgard\.yggdrasil/[^"]+)".*$!\1!; s|http://([^/]+)/|/srv/media/|; q' | urldecode)
          printf "Found ‘%s’\n" "${url}" | debug
          if [[ -z "${url}" || ! -e "${url}" ]]; then
              echo "thread ${id} not associated with any file or url, tagging for retry" >&2
              # notmuch search --output=files --format=text ${(q)id} \
                  #     | xargs -n 1 basename \
                  #     | xargs -I '{}' -- sed -i "/{}/d" .youtube-cache
              notmuch tag -cached -- ${id}
              continue
          fi
          typeset -a args
          args=("${url}")
          if [[ "${force}" -gt 0 ]]; then
              args=(-f $args)
              force=$(($force - 1))
          fi
          queue.hel $args && notmuch tag -unread -inbox -- ${(q)id}
        ) 9<>"${message}"
    done
done

[[ -z "${checkedIds}" ]] && queue.hel