From 9f6414838d4fabc174bb193f34225de8d9fc8079 Mon Sep 17 00:00:00 2001 From: "Danilo M." Date: Tue, 29 Sep 2026 14:17:33 +0200 Subject: feat(immich): fall back to the past week's memories A day with no memories left the card showing only a notice. When today has none, immich-fetch.sh now merges the memories of each of the past six days, skipping a day whose request fails, and marks the index with a scope week line. The subtitle names where the photos come from, Today or This week, before the i/n counter, and the empty notice reads no memories this week. Co-Authored-By: Claude Opus 5.5 --- bin/immich-fetch.sh | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) (limited to 'bin/immich-fetch.sh') diff --git a/bin/immich-fetch.sh b/bin/immich-fetch.sh index 961cd63..0fb5b41 100755 --- a/bin/immich-fetch.sh +++ b/bin/immich-fetch.sh @@ -6,8 +6,10 @@ # Writes $CACHE_DIR/.png per photo plus index.txt: # generated # url +# scope week (only when the past week's memories stand in for today's) # photo ||| -# An index with no photo lines means "no memories today", which the card shows +# When today has none, the past six days' memories are used instead. An +# index with no photo lines means "no memories this week", which the card shows # differently from a missing index ("the fetch never ran"). # # Leaves any existing cache untouched when the server cannot be reached, rather @@ -40,21 +42,40 @@ mkdir -p "$CACHE_DIR" TMP="$INDEX.tmp.$$" MEM="$CACHE_DIR/memories.json.tmp.$$" -trap 'rm -f "$TMP" "$MEM" "$CACHE_DIR"/*.part.$$' EXIT +trap 'rm -f "$TMP" "$MEM" "$CACHE_DIR"/*.part.$$ "$CACHE_DIR"/week.json.tmp.$$*' EXIT api() { curl -fsS --max-time 20 -H "x-api-key: $KEY" "${URL%/}/api/$1" } -if ! api "memories?for=$(date +%F)" > "$MEM" 2>/dev/null \ - || ! jq -e 'type == "array"' "$MEM" > /dev/null 2>&1; then +memories() { + api "memories?for=$1" > "$MEM" 2>/dev/null \ + && jq -e 'type == "array"' "$MEM" > /dev/null 2>&1 +} + +if ! memories "$(date +%F)"; then echo "immich-fetch: memories request failed" >&2 exit 1 fi +# No memories today: fall back to the past week's, so the card still shows +# something. Each day's memories go in one merged array; a day that fails is +# skipped rather than failing the whole fetch. +if ! jq -e 'any(.[]; .type == "on_this_day")' "$MEM" > /dev/null; then + WEEK="$CACHE_DIR/week.json.tmp.$$" + echo '[]' > "$WEEK" + for d in 1 2 3 4 5 6; do + memories "$(date -d "$d days ago" +%F)" || continue + jq -s 'add' "$WEEK" "$MEM" > "$WEEK.new" && mv -f "$WEEK.new" "$WEEK" + done + mv -f "$WEEK" "$MEM" + SCOPE=week +fi + echo "generated $(date +%s)" > "$TMP" # For the card's click-through to the photo in Immich's web UI. echo "url ${URL%/}" >> "$TMP" +[ -n "${SCOPE:-}" ] && echo "scope $SCOPE" >> "$TMP" # One line per asset: id, the memory's year, and when the photo was taken. # localDateTime is the camera's wall clock, which is what "taken at" means; @@ -88,7 +109,7 @@ done mv -f "$TMP" "$INDEX" -# Drop photos no longer in today's memories, so the directory holds one day. +# Drop photos no longer in the index, so the directory holds one fetch's worth. for f in "$CACHE_DIR"/*.png; do [ -e "$f" ] || continue id=$(basename "$f" .png) -- cgit v1.2.3