-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathsync-image.py
More file actions
298 lines (248 loc) · 9.93 KB
/
sync-image.py
File metadata and controls
298 lines (248 loc) · 9.93 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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import os
import json
import hashlib
import requests
import argparse
from urllib.parse import urlparse
from pathlib import Path
from mimetypes import guess_extension
# --- Config ---
DATA_DIR = "content/maintainers/"
CACHE_FILE = ".image-cache.json"
IMAGES_DIR = "public/images/"
MAX_IMAGE_SIZE = 500 * 1024 # 500KB
Path(IMAGES_DIR).mkdir(parents=True, exist_ok=True)
# --- CLI Argument Parser ---
parser = argparse.ArgumentParser(
description="Sync or check remote images to local. If not present, download it and update json. If exists check the hash if updated."
)
parser.add_argument(
"json_file",
nargs="?",
help="Specific JSON file to process (inside content/maintainers/)",
)
parser.add_argument(
"--sync", action="store_true", help="Check and prompt if any image is out of sync"
)
parser.add_argument("--debug", action="store_true", help="Show debug logs")
parser.add_argument(
"--verify-cache", action="store_true", help="Verify existing image cache hashes"
)
args = parser.parse_args()
class Colors:
RESET = "\033[0m"
GREEN = "\033[92m"
RED = "\033[91m"
YELLOW = "\033[93m"
BLUE = "\033[94m"
CYAN = "\033[96m"
# --- Load image cache ---
if os.path.exists(CACHE_FILE):
with open(CACHE_FILE, "r") as f:
cache = json.load(f)
else:
cache = {}
# --- Helpers ---
def log(msg):
if args.debug:
print(msg)
def get_ext_from_url_or_content_type(url, headers):
path = urlparse(url).path
ext = os.path.splitext(path)[-1]
if ext:
return ext.split("?")[0]
content_type = headers.get("Content-Type", "")
if content_type:
return guess_extension(content_type.split(";")[0]) or ".img"
return ".img"
def get_hash(content):
return hashlib.sha256(content).hexdigest()
def get_hash_from_file(filepath):
with open(filepath, "rb") as f:
return hashlib.sha256(f.read()).hexdigest()
def download_image(url):
try:
response = requests.get(url)
response.raise_for_status()
return response.content, response.headers
except Exception as e:
print(f"[ERROR] Failed to download {url}: {e}")
return None, None
def check_image_size(filepath):
try:
size = os.path.getsize(filepath)
if size > MAX_IMAGE_SIZE:
size_kb = round(size / 1024, 2)
filename = os.path.basename(filepath)
print(
f"{Colors.RED}[SIZE]{Colors.RESET} {filename} is {size_kb} KB (limit 500 KB)"
)
ext = os.path.splitext(filename)[1].lower()
if ext in [".png", ".jpg", ".jpeg"]:
print(
f"{Colors.CYAN}[FIX]{Colors.RESET} Run:\n"
f"magick {filepath} -resize 200x200 -strip -quality 70 {filepath}"
)
else:
log(f"[SIZE OK] {filepath}")
except Exception as e:
print(f"[WARN] Could not check size for {filepath}: {e}")
def process_image(url, filename_base, force_download=False):
if not url.strip():
return ""
cached = cache.get(url)
for ext_guess in [".png", ".jpg", ".jpeg", ".svg", ".webp"]:
full_filename = f"{filename_base}{ext_guess}"
filepath = os.path.join(IMAGES_DIR, full_filename)
if os.path.exists(filepath):
local_hash = get_hash_from_file(filepath)
check_image_size(filepath)
if cached and cached["hash"] == local_hash:
log(f"[SKIP] Up-to-date: {full_filename}")
return full_filename
elif args.sync:
print(f"[CHECK] Needs update: {url} -> /images/{full_filename}")
return full_filename
else:
break
if args.sync and not force_download:
return ""
content, headers = download_image(url)
if not content:
return ""
ext = get_ext_from_url_or_content_type(url, headers)
full_filename = f"{filename_base}{ext}"
filepath = os.path.join(IMAGES_DIR, full_filename)
with open(filepath, "wb") as f:
f.write(content)
hash_value = get_hash(content)
cache[url] = {"hash": hash_value, "filename": full_filename}
check_image_size(filepath)
print(f"[UPDATE] Downloaded or replaced: {full_filename}")
return full_filename
# --- Main File Processor ---
def process_json(file):
json_path = os.path.join(DATA_DIR, file)
with open(json_path, "r", encoding="utf-8") as f:
try:
data = json.load(f)
except Exception as e:
print(f"[SKIP] Invalid JSON in {file}: {e}")
return
base_name = os.path.splitext(file)[0]
# Photo
if "photo" in data:
url = data["photo"]
if isinstance(url, str) and url.strip():
if url.startswith("/images/"):
local_path = os.path.join("public", url.lstrip("/"))
if os.path.exists(local_path):
for original_url, entry in cache.items():
if entry["filename"] == os.path.basename(local_path):
actual_hash = get_hash_from_file(local_path)
if actual_hash != entry["hash"]:
print(
f"{Colors.YELLOW}[MISMATCH] =====>{Colors.RESET} {url} differs from cache"
)
if args.sync:
print(
f"[CHECK] Needs update: {original_url} -> {url}"
)
else:
filename = os.path.splitext(
os.path.basename(local_path)
)[0]
result = process_image(
original_url, filename, force_download=True
)
data["photo"] = (
f"/images/{result}" if result else ""
)
break
else:
print(f"[WARN] Local photo missing: {url}")
data["photo"] = ""
else:
filename = f"{base_name}_photo"
result = process_image(url, filename)
data["photo"] = f"/images/{result}" if result else ""
else:
data["photo"] = ""
# Project logos
for i, project in enumerate(data.get("projects", [])):
url = project.get("logo", "")
if isinstance(url, str) and url.strip():
if url.startswith("/images/"):
local_path = os.path.join("public", url.lstrip("/"))
if os.path.exists(local_path):
for original_url, entry in cache.items():
if entry["filename"] == os.path.basename(local_path):
actual_hash = get_hash_from_file(local_path)
if actual_hash != entry["hash"]:
print(
f"{Colors.YELLOW}[MISMATCH] ========>{Colors.RESET} {url} differs from cache"
)
if args.sync:
print(
f"[CHECK] Needs update: {original_url} -> {url}"
)
else:
project_name = project.get("name", f"project_{i}")
safe_name = project_name.replace(" ", "_").lower()
filename = f"{base_name}_{safe_name}"
result = process_image(
original_url, filename, force_download=True
)
project["logo"] = (
f"/images/{result}" if result else ""
)
break
else:
print(f"[WARN] Local logo missing: {url}")
project["logo"] = ""
else:
project_name = project.get("name", f"project_{i}")
safe_name = project_name.replace(" ", "_").lower()
filename = f"{base_name}_{safe_name}"
result = process_image(url, filename)
project["logo"] = f"/images/{result}" if result else ""
else:
project["logo"] = ""
if not args.sync:
with open(json_path, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2, ensure_ascii=False)
print(f"[DONE] Updated: {file}")
# --- Verify cache integrity if requested ---
if hasattr(args, "verify_cache") and args.verify_cache:
print("[INFO] Verifying image hashes from cache...")
for url, info in cache.items():
try:
content, _ = download_image(url)
if not content:
print(f"[FAIL] Could not download: {url}")
continue
current_hash = get_hash(content)
if current_hash == info["hash"]:
print(f"[OK] Match: {url}")
else:
print(
f"{Colors.YELLOW}[DIFF] ===========>{Colors.RESET} Hash mismatch: {url}"
)
except Exception as e:
print(f"[FAIL] {url}: {e}")
exit(0)
# --- Process Files ---
if args.json_file:
if not args.json_file.endswith(".json"):
print("[ERROR] Please provide a valid .json file")
else:
process_json(args.json_file)
else:
for file in os.listdir(DATA_DIR):
if file.endswith(".json"):
process_json(file)
# --- Save cache ---
if not args.sync:
with open(CACHE_FILE, "w", encoding="utf-8") as f:
json.dump(cache, f, indent=2)
print("[OK] Image sync complete.")