Parcourir la source

[bin] Order html page for timelapses by latest first

Colin Powell il y a 21 heures
Parent
commit
9941550fa8
1 fichiers modifiés avec 25 ajouts et 36 suppressions
  1. 25 36
      bin/.bin/webcam-build-timelapses

+ 25 - 36
bin/.bin/webcam-build-timelapses

@@ -32,7 +32,7 @@ WEBCAMS=(
 TIMELAPSE_SCRIPT="/usr/local/bin/make_timelapse"
 
 NTFY_URL="https://ntfy.unbl.ink/timelapse"
-WEB_PREFIX="https://files.lab.unbl.ink"
+WEB_PREFIX="https://files.lab.unbl.ink/webcams"
 # ----------------
 
 # Yesterday's date
@@ -63,56 +63,45 @@ for cam in "${WEBCAMS[@]}"; do
   fi
 done
 
-# --- Update HTML index ---
-mkdir -p "$BASE_DIR"
-if [[ ! -f "$INDEX_FILE" ]]; then
-  cat >"$INDEX_FILE" <<EOF
+# --- Update index.html ---
+INDEX_HTML="$BASE_DIR/index.html"
+
+# Ensure file exists with basic structure
+if [[ ! -f "$INDEX_HTML" ]]; then
+  cat > "$INDEX_HTML" <<EOF
 <!DOCTYPE html>
 <html>
 <head>
-  <meta charset="UTF-8">
+  <meta charset="utf-8">
   <title>Webcam Timelapses</title>
-  <style>
-    body { font-family: sans-serif; padding: 1em; background: #f9f9f9; }
-    h2 { margin-top: 1.5em; }
-    ul { list-style: none; padding: 0; }
-    li { margin: 0.3em 0; }
-    a { text-decoration: none; color: #0366d6; }
-    a:hover { text-decoration: underline; }
-  </style>
 </head>
 <body>
   <h1>Webcam Timelapses</h1>
+  <div id="days">
+  </div>
 </body>
 </html>
 EOF
 fi
 
-# Insert new section for yesterday at the top
-TMP=$(mktemp)
-{
-  echo "<h2>$YESTERDAY</h2>"
-  echo "<ul>"
-  for entry in "${LINKS[@]}"; do
-    cam="${entry%%]*}"
-    cam="${cam#[}"
-    url="${entry#* }"
-    echo "  <li><a href=\"$url\">$cam</a></li>"
-  done
-  echo "</ul>"
-  echo "<hr>"
-} >"$TMP"
+# Build new day's section
+NEW_SECTION="<h2>$YESTERDAY</h2><ul>"
+for link in "${LINKS[@]}"; do
+  cam=\$(echo "\$link" | cut -d']' -f1 | tr -d '[]')
+  url=\$(echo "\$link" | awk '{print \$2}')
+  NEW_SECTION+="<li><a href=\"\$url\">\$cam</a></li>"
+done
+NEW_SECTION+="</ul>"
 
-# Place new block after <h1> line
-awk -v block="$(cat "$TMP")" '
-  /<h1>/ { print; getline; print; print block; next }
+# Insert at the top of <div id="days">
+tmpfile=\$(mktemp)
+awk -v section="\$NEW_SECTION" '
+  /<div id="days">/ { print; print section; next }
   { print }
-' "$INDEX_FILE" >"$INDEX_FILE.new"
-
-mv "$INDEX_FILE.new" "$INDEX_FILE"
-rm "$TMP"
+' "\$INDEX_HTML" > "\$tmpfile"
+mv "\$tmpfile" "\$INDEX_HTML"
 
-echo "📄 Updated HTML index: $INDEX_FILE"
+echo "📝 Updated index with $YESTERDAY timelapses (newest on top)"
 
 # --- Send combined notification ---
 if [[ -n "$NTFY_URL" ]] && [[ ${#LINKS[@]} -gt 0 ]]; then