[bin] Order html page for timelapses by latest first

This commit is contained in:
2025-08-29 11:41:02 -04:00
parent c363cbcde6
commit 9941550fa8

View File

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