From b3b957b92d66dcfa3452e377b1fa15b2342cd7b1 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Fri, 6 Apr 2018 14:25:00 +0200 Subject: send_media --- odin/strm.nix | 3 +- odin/strm/send_media | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 odin/strm/send_media (limited to 'odin') diff --git a/odin/strm.nix b/odin/strm.nix index 33e605af..36d0e876 100644 --- a/odin/strm.nix +++ b/odin/strm.nix @@ -11,11 +11,12 @@ stdenv.mkDerivation { buildPhase = '' substituteAllInPlace download_youtube substituteAllInPlace cleanup_youtube + substituteAllInPlace send_media ''; installPhase = '' mkdir -p $out/bin - install -m 755 -t $out/bin download_youtube cleanup_youtube + install -m 755 -t $out/bin download_youtube cleanup_youtube send_media ''; } diff --git a/odin/strm/send_media b/odin/strm/send_media new file mode 100644 index 00000000..4f3e576b --- /dev/null +++ b/odin/strm/send_media @@ -0,0 +1,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 '/]+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 -- cgit v1.2.3