You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

58 lines
1.9 KiB

  1. #!/usr/bin/env zsh
  2. # Dependencies: exastash `es` in PATH, `path-to-eye-path` in PATH, and
  3. # apt install zsh ripgrep rclone curl jq
  4. set -eu -o pipefail
  5. # Enable job control for wait-until-jobs-below
  6. set -m
  7. # Wait until the number of shell jobs drops below a limit.
  8. # This can be used as an alternative to parallel using
  9. # for i in ...; do something $i &; wait-until-jobs-below 8; done
  10. wait-until-jobs-below() {
  11. while test $(jobs | wc -l) -ge "$1"; do
  12. sleep 0.1
  13. done
  14. }
  15. # provide as stdin the output of e.g.
  16. # es x find -t f -- ~/stash/YouTube | take-wanted-ids ~/dead-video-ids | rg '\.(mp4|webm|flv|video)$'
  17. RCLONE_DEST=jeff
  18. while read i; do
  19. echo -E $i
  20. eye_path=$(path-to-eye-path "$i" | sed -r "s,$HOME/stash/,,g")
  21. info=$(es x info -- "$i")
  22. file_id=$(echo -E "$info" | jq .id)
  23. size=$(echo -E "$info" | jq .size)
  24. # If already in namedfiles i.e. the-eye, skip
  25. echo -E "$info" | rg -q -F '"type": "namedfiles"' && continue || true
  26. is_video=0
  27. echo -nE "$i" | rg -q '\.(mp4|webm|flv|video)$' && is_video=1 || true
  28. the_eye_video_bytes_saved=0
  29. the_eye_video_files_saved=0
  30. if [[ $is_video -eq 1 ]]; then
  31. the_eye_video_bytes_saved=$size
  32. the_eye_video_files_saved=1
  33. fi
  34. # This is mostly a demo which adds it to ceph over SFTP with `rclone`;
  35. # ideally we instead have the `es x get -s -- "$i"` step putting it directly into ceph.
  36. (
  37. ionice -c 3 nice es x get -s -- "$i" && \
  38. rclone --metadata --inplace --size-only --progress copyto "$i" "$RCLONE_DEST:$eye_path" && \
  39. 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 && \
  40. 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;
  41. rm -f -- "$i"
  42. ) &
  43. wait-until-jobs-below 16
  44. done