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.
 
 

79 lines
2.4 KiB

  1. #!/usr/bin/env zsh
  2. set -eu -o pipefail
  3. # Dependencies:
  4. # - apt install zsh ripgrep rclone curl jq
  5. # - exastash `es` in PATH
  6. # - environmental variables for exastash including EXASTASH_NO_GDRIVE=1
  7. # - download-scripts `path-to-eye-path` in PATH
  8. # provide as stdin the output of e.g.
  9. # es x find -t f -- ~/stash/YouTube | take-wanted-ids ~/dead-video-ids
  10. # The temporary files will land in ~/stash/YouTube which should be on your SSDs
  11. # The finished files will be moved to:
  12. complete_dir=$1
  13. #
  14. # Enable job control for wait-until-jobs-below
  15. set -m
  16. # Wait until the number of shell jobs drops below a limit.
  17. # This can be used as an alternative to parallel using
  18. # for i in ...; do something $i &; wait-until-jobs-below 8; done
  19. wait-until-jobs-below() {
  20. while test $(jobs | wc -l) -ge "$1"; do
  21. sleep 0.1
  22. done
  23. }
  24. mkdir -p ~/eye-control
  25. cat ~/eye-control/download-parallelism || echo -n 50 > ~/eye-control/download-parallelism
  26. mkdir -p -- "$complete_dir"
  27. while read i; do
  28. if [[ -f ~/eye-control/stop ]]; then
  29. echo "exiting because stop file is present"
  30. exit 1
  31. fi
  32. echo -E $i
  33. eye_path=$(path-to-eye-path "$i" | sed -r "s,$HOME/stash/,,g")
  34. complete_eye_dir="$(dirname -- "$complete_dir/$eye_path")"
  35. info=$(es x info -- "$i")
  36. file_id=$(echo -E "$info" | jq .id)
  37. size=$(echo -E "$info" | jq .size)
  38. # If already in namedfiles i.e. the-eye, skip
  39. echo -E "$info" | rg -q -F '"type": "namedfiles"' && continue || true
  40. is_video=0
  41. echo -nE "$i" | rg -q '\.(mp4|webm|flv|video)$' && is_video=1 || true
  42. the_eye_video_bytes_saved=0
  43. the_eye_video_files_saved=0
  44. if [[ $is_video -eq 1 ]]; then
  45. the_eye_video_bytes_saved=$size
  46. the_eye_video_files_saved=1
  47. fi
  48. # Ideally, we would created a `namedfiles` entity only after it's in ceph,
  49. # but this is okay: we can later recreate all the `namedfiles` by scanning
  50. # all of the files in the ceph directory.
  51. (
  52. ionice -c 3 nice es x get -s -- "$i" && \
  53. 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 && \
  54. 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" && \
  55. mkdir -p -- "$complete_eye_dir" && \
  56. mv -- "$i" "$complete_eye_dir"/ || true
  57. ) &
  58. wait-until-jobs-below $(cat ~/eye-control/download-parallelism)
  59. done
  60. wait