-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
59 lines (43 loc) · 1.61 KB
/
run.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
import requests
import json
from color import Color
# personal access token generated in Figma. User needs to be a member of an enterprise org to read from variables
# endpoints and an editor to post
access_token='PAT_HERE'
# key of the figma file we want to retrieve values from
file_key='FILE_KEY'
base_url='https://api.figma.com/v1/'
endpoint=f"files/{file_key}/variables/local"
headers={
'X-FIGMA-TOKEN': access_token
}
output_css_file_name="demo_figma_css_extraction.scss"
response = requests.get(f"{base_url}{endpoint}", headers=headers)
variable_collections=response.json()['meta']['variables']
# extend this list with desired types
figma_types_and_keys_to_extract = {'COLOR': '26:0'}
output_variables=[]
for variable in variable_collections.values():
try:
if variable['resolvedType'] in figma_types_and_keys_to_extract.keys():
if variable['resolvedType'] == 'COLOR':
r,g,b,a = variable['valuesByMode'][figma_types_and_keys_to_extract[variable['resolvedType']]].values()
output_variables.append(Color(
variable['name'],
r,
g,
b,
a,
description=variable['description']
))
elif False:
# additional types here, e.g. padding
pass
except Exception as e:
print(f"error parsing {json.dumps(variable), e}")
f = open(output_css_file_name, 'w')
f.write(":root{ \n")
for variable in output_variables:
f.write(f"\t{variable.to_css()}\n")
f.write("}")
f.close()