#!/usr/bin/env zsh # Dependencies: exastash `es` in PATH, `path-to-eye-path` in PATH, and # apt install zsh ripgrep rclone curl jq set -eu -o pipefail # Enable job control for wait-until-jobs-below set -m # Wait until the number of shell jobs drops below a limit. # This can be used as an alternative to parallel using # for i in ...; do something $i &; wait-until-jobs-below 8; done wait-until-jobs-below() { while test $(jobs | wc -l) -ge "$1"; do sleep 0.1 done } # provide as stdin the output of e.g. # es x find -t f -- ~/stash/YouTube | take-wanted-ids ~/dead-video-ids | rg '\.(mp4|webm|flv|video)$' RCLONE_DEST=jeff while read i; do echo -E $i eye_path=$(path-to-eye-path "$i" | sed -r "s,$HOME/stash/,,g") info=$(es x info -- "$i") file_id=$(echo -E "$info" | jq .id) size=$(echo -E "$info" | jq .size) # If already in namedfiles i.e. the-eye, skip echo -E "$info" | rg -q -F '"type": "namedfiles"' && continue || true is_video=0 echo -nE "$i" | rg -q '\.(mp4|webm|flv|video)$' && is_video=1 || true the_eye_video_bytes_saved=0 the_eye_video_files_saved=0 if [[ $is_video -eq 1 ]]; then the_eye_video_bytes_saved=$size the_eye_video_files_saved=1 fi # This is mostly a demo which adds it to ceph over SFTP with `rclone`; # ideally we instead have the `es x get -s -- "$i"` step putting it directly into ceph. ( ionice -c 3 nice es x get -s -- "$i" && \ rclone --metadata --inplace --size-only --progress copyto "$i" "$RCLONE_DEST:$eye_path" && \ curl --fail-with-body -u "ya: ." -X POST -d "{\"file_id\": $file_id, \"location\": \"the-eye\", \"pathname\": \"$eye_path\"}" https://ya.borg.xyz/cgi-bin/new-namedfiles && \ curl -X POST -d "{\"the_eye_any_bytes_saved\": $size, \"the_eye_video_bytes_saved\": $the_eye_video_bytes_saved, \"the_eye_any_files_saved\": 1, \"the_eye_video_files_saved\": $the_eye_video_files_saved}" "http://eye.borg.xyz:31416/metrics" || true; rm -f -- "$i" ) & wait-until-jobs-below 16 done