#!@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 '/]+href="http:\/\/odin\.asgard\.yggdrasil\//!d; s!^.*]+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