webcam-build-timelapses 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/env bash
  2. # run_all_timelapses.sh
  3. #
  4. # Run timelapse creation for a fixed set of webcams,
  5. # collect public URLs, update HTML index, and send one combined ntfy notification.
  6. #
  7. # Requirements: make_timelapse.sh in same folder or PATH
  8. set -euo pipefail
  9. # --- Settings ---
  10. BASE_DIR="/media/photos/misc/webcams"
  11. INDEX_FILE="$BASE_DIR/timelapses.html"
  12. WEBCAMS=(
  13. "frontyard"
  14. "basement_table"
  15. "mailbox"
  16. "basement_extension"
  17. "backyard_low"
  18. "backyard_north"
  19. "basement_cape"
  20. "basement_tv"
  21. "backyard"
  22. "bulkhead"
  23. "barn"
  24. "garage"
  25. "birds"
  26. "porch"
  27. "garden"
  28. "diningroom"
  29. )
  30. TIMELAPSE_SCRIPT="/usr/local/bin/make_timelapse"
  31. NTFY_URL="https://ntfy.unbl.ink/timelapse"
  32. WEB_PREFIX="https://files.lab.unbl.ink/webcams"
  33. # ----------------
  34. # Yesterday's date
  35. if date -d "yesterday" +%Y >/dev/null 2>&1; then
  36. # GNU date (Linux)
  37. YESTERDAY=$(date -d "yesterday" +"%Y/%m/%d")
  38. else
  39. # BSD date (macOS)
  40. YESTERDAY=$(date -v-1d +"%Y/%m/%d")
  41. fi
  42. LINKS=()
  43. for cam in "${WEBCAMS[@]}"; do
  44. TARGET_DIR="$BASE_DIR/$cam/$YESTERDAY"
  45. if [[ -d "$TARGET_DIR" ]]; then
  46. echo "📷 Processing $cam ($TARGET_DIR)"
  47. OUTPUT=$("$TIMELAPSE_SCRIPT" "$TARGET_DIR")
  48. echo "$OUTPUT"
  49. LINK="$WEB_PREFIX/$cam/$YESTERDAY/timelapse.mp4"
  50. if [[ -f "$TARGET_DIR/timelapse.mp4" ]]; then
  51. LINKS+=("[$cam] $LINK")
  52. fi
  53. else
  54. echo "⚠️ Skipping $cam (no folder for $YESTERDAY)"
  55. fi
  56. done
  57. # --- Update index.html ---
  58. INDEX_HTML="$BASE_DIR/index.html"
  59. # Ensure file exists with basic structure
  60. if [[ ! -f "$INDEX_HTML" ]]; then
  61. cat > "$INDEX_HTML" <<EOF
  62. <!DOCTYPE html>
  63. <html>
  64. <head>
  65. <meta charset="utf-8">
  66. <title>Webcam Timelapses</title>
  67. </head>
  68. <body>
  69. <h1>Webcam Timelapses</h1>
  70. <div id="days">
  71. </div>
  72. </body>
  73. </html>
  74. EOF
  75. fi
  76. # Build new day's section
  77. NEW_SECTION="<h2>$YESTERDAY</h2><ul>"
  78. for link in "${LINKS[@]}"; do
  79. cam=\$(echo "\$link" | cut -d']' -f1 | tr -d '[]')
  80. url=\$(echo "\$link" | awk '{print \$2}')
  81. NEW_SECTION+="<li><a href=\"\$url\">\$cam</a></li>"
  82. done
  83. NEW_SECTION+="</ul>"
  84. # Insert at the top of <div id="days">
  85. tmpfile=\$(mktemp)
  86. awk -v section="\$NEW_SECTION" '
  87. /<div id="days">/ { print; print section; next }
  88. { print }
  89. ' "\$INDEX_HTML" > "\$tmpfile"
  90. mv "\$tmpfile" "\$INDEX_HTML"
  91. echo "📝 Updated index with $YESTERDAY timelapses (newest on top)"
  92. # --- Send combined notification ---
  93. if [[ -n "$NTFY_URL" ]] && [[ ${#LINKS[@]} -gt 0 ]]; then
  94. MSG="Timelapses for $YESTERDAY:\n$(printf '%s\n' "${LINKS[@]}")"
  95. curl -s -H "Title: Timelapses complete" \
  96. -H "Priority: high" \
  97. -d "$MSG" \
  98. "$NTFY_URL" >/dev/null || true
  99. echo "📢 Combined notification sent to ntfy."
  100. fi