Skip to content

Commit

Permalink
feat: /design url and utf-8 support (Issue #1, #2)
Browse files Browse the repository at this point in the history
  • Loading branch information
Axorax committed May 17, 2024
1 parent 98acdb1 commit 2ce1127
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 43 deletions.
36 changes: 7 additions & 29 deletions core.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
import requests
import os
from utils import rgb_to_hex, get_foreground_color

def write_file(text, out=None, frame=None):
if out is None:
folder_path = 'TkForge'
else:
folder_path = os.path.join(out, 'TkForge')

if not os.path.exists(folder_path):
os.makedirs(folder_path)
def get_file(file, token):
response = requests.get(f"https://api.figma.com/v1/files/{file}", headers={'X-FIGMA-TOKEN': token})

if frame is not None:
file_name = f'frame_{frame}.py'
if response.status_code == 200:
return response.json()
else:
file_name = 'main.py'

with open(os.path.join(folder_path, file_name), 'w') as file:
file.write(text)
return response.status_code, response.text

def download_image(file, id, count, token, out=None, frame=None):
response = requests.get(f"https://api.figma.com/v1/images/{file}", headers={'X-FIGMA-TOKEN': token}, params={'ids': id})
Expand All @@ -39,7 +31,7 @@ def download_image(file, id, count, token, out=None, frame=None):
image_response = requests.get(image_url)

if image_response.status_code == 200:
with open(file_path, 'wb') as f:
with open(file_path, 'wb', encoding='utf-8') as f:
f.write(image_response.content)

if frame is not None:
Expand All @@ -55,20 +47,6 @@ def download_image(file, id, count, token, out=None, frame=None):

return None

def get_file(file, token):
response = requests.get(f"https://api.figma.com/v1/files/{file}", headers={'X-FIGMA-TOKEN': token})

if response.status_code == 200:
return response.json()
else:
return response.status_code, response.text

def rgb_to_hex(r, g, b):
return f"#{int(r*255):02x}{int(g*255):02x}{int(b*255):02x}"

def get_foreground_color(r, g, b):
return '#000000' if (r*0.299 + g*0.587 + b*0.114) > 186 else '#ffffff'

def parse_file(file, token, download_images=True, out=None):
output = []
result = get_file(file, token)
Expand Down
14 changes: 1 addition & 13 deletions gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from tk import tk_code
from tkinter import filedialog
from tkinter import messagebox
from urllib.parse import urlparse
from utils import extract_figma_id

def load_asset(path):
base = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
Expand Down Expand Up @@ -117,18 +117,6 @@ def get_placeholder(self): return self.p

# Generate code

def extract_figma_id(url):
if url.startswith('http'):
url = urlparse(url)

if 'figma.com' in url.netloc and '/file/' in url.path:
path_parts = url.path.split('/')
if 'file' in path_parts:
file_index = path_parts.index('file')
if file_index + 1 < len(path_parts):
return path_parts[file_index + 1]
return url

def clear_token_input(t=True):
token_input.delete(0, tk.END)
if t:
Expand Down
3 changes: 2 additions & 1 deletion tk.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from core import parse_file, write_file
from core import parse_file
from utils import write_file

def text(i):
return f'''
Expand Down
1 change: 1 addition & 0 deletions tkforge.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from tk import tk_code
from colorama import Fore, init
from urllib.parse import urlparse
from utils import extract_figma_id

init(autoreset=True)

Expand Down
42 changes: 42 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import os
from urllib.parse import urlparse

def rgb_to_hex(r, g, b):
return f"#{int(r*255):02x}{int(g*255):02x}{int(b*255):02x}"

def get_foreground_color(r, g, b):
return '#000000' if (r*0.299 + g*0.587 + b*0.114) > 186 else '#ffffff'

def write_file(text, out=None, frame=None):
if out is None:
folder_path = 'TkForge'
else:
folder_path = os.path.join(out, 'TkForge')

if not os.path.exists(folder_path):
os.makedirs(folder_path)

if frame is not None:
file_name = f'frame_{frame}.py'
else:
file_name = 'main.py'

with open(os.path.join(folder_path, file_name), 'w', encoding='utf-8') as file:
file.write(text)

def extract_figma_id(url):
if url.startswith('http'):
url = urlparse(url)

if 'figma.com' in url.netloc and ('/file/' in url.path or '/design/' in url.path):
path_parts = url.path.split('/')
if 'file' in path_parts:
file_index = path_parts.index('file')
elif 'design' in path_parts:
file_index = path_parts.index('design')
else:
return None

if file_index + 1 < len(path_parts):
return path_parts[file_index + 1]
return None

0 comments on commit 2ce1127

Please sign in to comment.