#!/usr/bin/env python3 """Update dates for IMG_8xxx files in Immich.""" import subprocess import json from urllib.request import Request, urlopen API_KEY = "GsWQUTR6EXlkKp1M82jDJ3KmzhM0fMAbbIbfHDyI" API_URL = "http://localhost:2283/api" files = { "IMG_8007.jpg": "2010-09-20T12:16:54.000Z", "IMG_8027.jpg": "2010-09-20T12:16:54.000Z", "IMG_8028.jpg": "2010-09-20T12:17:00.000Z", "IMG_8030.jpg": "2010-09-20T12:17:00.000Z", "IMG_8625.jpg": "2009-08-28T13:48:22.000Z", "IMG_8627.jpg": "2009-08-28T13:48:22.000Z", "IMG_8629.jpg": "2009-08-28T13:48:22.000Z", "IMG_8630.jpg": "2009-08-28T13:48:22.000Z", "IMG_8631.jpg": "2009-08-28T13:48:22.000Z", "IMG_8633.jpg": "2009-08-28T13:48:22.000Z", "IMG_8634.jpg": "2009-08-28T13:48:22.000Z", "IMG_8635.jpg": "2009-08-28T13:48:24.000Z", "IMG_8636.jpg": "2009-08-28T13:48:24.000Z", "IMG_8638.jpg": "2009-08-28T13:48:24.000Z", } def get_asset_id(filename): result = subprocess.run([ "docker", "exec", "immich_postgres", "psql", "-U", "postgres", "-d", "immich", "-t", "-c", f"SELECT id FROM asset WHERE \"originalPath\" LIKE '%/2024/01/{filename}' AND \"deletedAt\" IS NULL;" ], capture_output=True, text=True) return result.stdout.strip() def update_date(asset_id, date): data = json.dumps({"dateTimeOriginal": date}).encode() req = Request(f"{API_URL}/assets/{asset_id}", data=data, method="PUT", headers={"x-api-key": API_KEY, "Content-Type": "application/json"}) urlopen(req) updated = 0 for fname, date in files.items(): asset_id = get_asset_id(fname) if asset_id: update_date(asset_id, date) print(f"Updated {fname} -> {date[:10]}") updated += 1 else: print(f"NOT FOUND: {fname}") print(f"\nTotal updated: {updated}")