Skip to content

Commit da5bb9c

Browse files
committed
feat: Hash images
1 parent c9528c2 commit da5bb9c

1 file changed

Lines changed: 20 additions & 7 deletions

File tree

core.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import os
2+
import hashlib
23
import requests
34
import threading
45
from utils import rgb_to_hex, get_foreground_color
56

7+
HASH_IMAGES = True
8+
69
def get_file(file, token):
710
response = requests.get(f"https://api.figma.com/v1/files/{file}", headers={'X-FIGMA-TOKEN': token})
811

@@ -11,6 +14,11 @@ def get_file(file, token):
1114
else:
1215
return response.status_code, response.text
1316

17+
downloaded_images = {}
18+
19+
def get_image_hash(image_content):
20+
return hashlib.sha256(image_content).hexdigest()
21+
1422
def download_image(file, id, name, token, out=None, frame=None):
1523
response = requests.get(f"https://api.figma.com/v1/images/{file}", headers={'X-FIGMA-TOKEN': token}, params={'ids': id})
1624

@@ -32,13 +40,20 @@ def download_image(file, id, name, token, out=None, frame=None):
3240
image_response = requests.get(image_url)
3341

3442
if image_response.status_code == 200:
43+
image_content = image_response.content
44+
if HASH_IMAGES:
45+
image_hash = get_image_hash(image_content)
46+
47+
if image_hash in downloaded_images:
48+
return downloaded_images[image_hash]
49+
3550
with open(file_path, 'wb') as f:
36-
f.write(image_response.content)
51+
f.write(image_content)
3752

38-
if frame is not None:
39-
return os.path.join(f'frame_{frame}', file_name).replace('\\', '/')
40-
else:
41-
return file_name
53+
if HASH_IMAGES:
54+
downloaded_images[image_hash] = os.path.join(f'frame_{frame}', file_name).replace('\\', '/') if frame else file_name
55+
56+
return os.path.join(f'frame_{frame}', file_name).replace('\\', '/') if frame else file_name
4257
else:
4358
print("Failed to download the image.")
4459
else:
@@ -58,8 +73,6 @@ def parse_file(file, token, download_images=True, out=None):
5873
try:
5974
frames = result['document']['children'][0]['children']
6075
frame_count = 1 if len(frames) > 1 else 0
61-
# import json
62-
# print(json.dumps(frames, indent=4, sort_keys=True))
6376

6477
def parse_frame(frame, frame_count):
6578
nonlocal output

0 commit comments

Comments
 (0)