webcam-build-timelapses 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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"
  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 HTML index ---
  58. mkdir -p "$BASE_DIR"
  59. if [[ ! -f "$INDEX_FILE" ]]; then
  60. cat >"$INDEX_FILE" <<EOF
  61. <!DOCTYPE html>
  62. <html>
  63. <head>
  64. <meta charset="UTF-8">
  65. <title>Webcam Timelapses</title>
  66. <style>
  67. body { font-family: sans-serif; padding: 1em; background: #f9f9f9; }
  68. h2 { margin-top: 1.5em; }
  69. ul { list-style: none; padding: 0; }
  70. li { margin: 0.3em 0; }
  71. a { text-decoration: none; color: #0366d6; }
  72. a:hover { text-decoration: underline; }
  73. </style>
  74. </head>
  75. <body>
  76. <h1>Webcam Timelapses</h1>
  77. </body>
  78. </html>
  79. EOF
  80. fi
  81. # Insert new section for yesterday at the top
  82. TMP=$(mktemp)
  83. {
  84. echo "<h2>$YESTERDAY</h2>"
  85. echo "<ul>"
  86. for entry in "${LINKS[@]}"; do
  87. cam="${entry%%]*}"
  88. cam="${cam#[}"
  89. url="${entry#* }"
  90. echo " <li><a href=\"$url\">$cam</a></li>"
  91. done
  92. echo "</ul>"
  93. echo "<hr>"
  94. } >"$TMP"
  95. # Place new block after <h1> line
  96. awk -v block="$(cat "$TMP")" '
  97. /<h1>/ { print; getline; print; print block; next }
  98. { print }
  99. ' "$INDEX_FILE" >"$INDEX_FILE.new"
  100. mv "$INDEX_FILE.new" "$INDEX_FILE"
  101. rm "$TMP"
  102. echo "📄 Updated HTML index: $INDEX_FILE"
  103. # --- Send combined notification ---
  104. if [[ -n "$NTFY_URL" ]] && [[ ${#LINKS[@]} -gt 0 ]]; then
  105. MSG="Timelapses for $YESTERDAY:\n$(printf '%s\n' "${LINKS[@]}")"
  106. curl -s -H "Title: Timelapses complete" \
  107. -H "Priority: high" \
  108. -d "$MSG" \
  109. "$NTFY_URL" >/dev/null || true
  110. echo "📢 Combined notification sent to ntfy."
  111. fi