aboutsummaryrefslogtreecommitdiffstats
path: root/bin
diff options
context:
space:
mode:
Diffstat (limited to 'bin')
-rwxr-xr-xbin/immich-fetch.sh31
1 files changed, 26 insertions, 5 deletions
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/<asset id>.png per photo plus index.txt:
# generated <epoch>
# url <server URL>
+# scope week (only when the past week's memories stand in for today's)
# photo <asset id>|<memory year>|<localDateTime>|<place>
-# 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)