-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimmich_create_albums.py
108 lines (91 loc) · 4.34 KB
/
immich_create_albums.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/python
import argparse
import json
import os
import requests
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description="Create albums in Immich by their metadata")
parser.add_argument('--metadata-dir', type=str,
help='path to directory with album description metadata files')
parser.add_argument('--immich-url', type=str,
help='Immich server URL')
parser.add_argument('--api-key', type=str,
help='Immich API key')
args = parser.parse_args()
immich_url = args.immich_url
api_key = args.api_key
# Мap originalPath of all the assets to assetId
response = requests.request("GET", f"{immich_url}api/asset",
headers={
'Accept': 'application/json',
"x-api-key": api_key
},
data={})
assets = {}
for asset in response.json():
assets["\\".join(asset["originalPath"].split("/")
[-2:])] = asset["id"]
# Get all existing albums
response = requests.request("GET", f"{immich_url}api/album",
headers={
'Accept': 'application/json',
"x-api-key": api_key
},
data={})
albums = {}
for album in response.json():
albums[album["albumName"]] = album["id"]
# Enumerate albums metadata files in the directory
metadata_files = []
for file in os.listdir(args.metadata_dir):
if file.endswith(".json"):
metadata_files.append(os.path.join(args.metadata_dir, file))
# For each metadata file create album in Immich
for metadata_file in metadata_files:
with open(metadata_file, encoding='utf-8') as metadata_file:
data = json.load(metadata_file)
if not data['files']:
print("[INFO] Metadata file is empty")
continue
# Check if album already exists
album_id = None
if data['title'] in albums.keys():
album_id = albums[data['title']]
else:
# Create new album
response = requests.request("POST", f"{immich_url}api/album",
headers={
'Content-Type': 'application/json',
'Accept': 'applicationa/json',
"x-api-key": api_key
},
data=json.dumps({
"albumName": data['title'],
"description": data['description']
}))
if response.status_code != 201:
print(
f"[ERROR] Can't create album {data['title']}. Status code: {response.status_code}")
continue
album_id = response.json()['id']
# Collect asset ids
asset_ids = []
for file in data['files']:
file_path, file_extension = os.path.splitext(file)
file = file_path + file_extension.lower()
if file in assets.keys():
asset_ids.append(assets[file])
else:
print(f"[ERROR] Can't add {file} to album {data['title']}. File not found on Immich server")
# Add assets to album
response = requests.request("PUT", f"{immich_url}api/album/{album_id}/assets",
headers={
'Content-Type': 'application/json',
'Accept': 'application/json',
"x-api-key": api_key
},
data=json.dumps({"ids": asset_ids}))
if response.status_code != 200:
print(
f"[ERROR] Can't add assets to album {data['title']}. Status code: {response.status_code}")