Repository navigation
Expand file tree
/
Copy pathlocal_source.py
More file actions
49 lines (41 loc) · 1.94 KB
/
Copy pathlocal_source.py
File metadata and controls
49 lines (41 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""Local-folder alternative to immich_source.py, for anyone running Share
Pile without a self-hosted Immich instance. Walks a folder recursively for
image files modified since a given timestamp - e.g. point it at a phone's
auto-synced camera-upload folder (iCloud, Google Photos takeout, Dropbox
Camera Uploads, etc).
Exposes the same two-function shape as immich_source.py (iter_new_images,
download_original) so run.py can use either source interchangeably.
"""
import os
from datetime import datetime, timezone
IMAGE_EXTS = {".jpg", ".jpeg", ".png", ".heic", ".webp", ".gif", ".bmp"}
def _iso(ts):
return datetime.fromtimestamp(ts, tz=timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.000Z")
def iter_new_images(folder, since_iso):
"""Yields dicts shaped like immich_source's asset items (id,
originalFileName, fileCreatedAt) for every image file under `folder`
modified after since_iso. `id` is the absolute file path - it plays the
same role Immich's asset UUID does as the state.json dedup key, so
don't rename/move already-processed files if you want dedup to hold.
"""
for root, _, files in sorted(os.walk(folder)):
for fname in sorted(files):
ext = os.path.splitext(fname)[1].lower()
if ext not in IMAGE_EXTS:
continue
path = os.path.abspath(os.path.join(root, fname))
try:
mtime = os.path.getmtime(path)
except OSError:
continue
created_iso = _iso(mtime)
if created_iso <= since_iso:
continue
yield {"id": path, "originalFileName": fname, "fileCreatedAt": created_iso}
def download_original(folder, path):
"""`folder` is unused (kept only so the call signature matches
immich_source.download_original(base_url, api_key, asset_id)) - `path`
is already the absolute path yielded by iter_new_images.
"""
with open(path, "rb") as f:
return f.read()