forked from once-ui-system/magic-portfolio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_logos.py
More file actions
86 lines (68 loc) · 2.15 KB
/
fetch_logos.py
File metadata and controls
86 lines (68 loc) · 2.15 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
#!/usr/bin/env python3
import os
import requests
from xml.etree import ElementTree as ET
LOGOS = [
"solidity",
"typescript",
"nextdotjs",
"vercel",
"dotnet",
"postgresql",
"mongodb",
"python",
"r",
"metamask",
"walletconnect",
"github",
"tailwindcss",
"linux",
"docker",
"git",
]
BASE_URL = "https://cdn.simpleicons.org"
TARGET_DIR = "./public/icons"
os.makedirs(TARGET_DIR, exist_ok=True)
def make_fill_currentcolor(svg_data):
try:
ET.register_namespace("", "http://www.w3.org/2000/svg")
root = ET.fromstring(svg_data)
# Clean root style if present
root.attrib.pop("fill", None)
root.attrib.pop("color", None)
# Make all paths use `fill="currentColor"`
for el in root.iter():
if el.tag.endswith("path") or el.tag.endswith("g"):
el.set("fill", "currentColor")
return ET.tostring(root, encoding="unicode")
except Exception as e:
print(f"✖ Failed to process SVG: {e}")
return svg_data
def make_svg_theme_friendly(svg_data):
try:
ET.register_namespace("", "http://www.w3.org/2000/svg")
root = ET.fromstring(svg_data)
# Remove background rects
root[:] = [el for el in root if el.tag.split("}")[-1] != "rect"]
# Set all fills to currentColor
for el in root.iter():
if el.tag.endswith("path") or el.tag.endswith("g"):
el.set("fill", "#000")
return ET.tostring(root, encoding="unicode")
except Exception as e:
print(f"✖ Failed to process SVG: {e}")
return svg_data
for name in LOGOS:
url = f"{BASE_URL}/{name}"
out_path = os.path.join(TARGET_DIR, f"{name}.svg")
try:
print(f"Downloading {name}...")
r = requests.get(url, timeout=10)
r.raise_for_status()
original_svg = r.text
processed_svg = make_svg_theme_friendly(original_svg)
with open(out_path, "w", encoding="utf-8") as f:
f.write(processed_svg)
print(f"✔ Saved to {out_path}")
except Exception as e:
print(f"✖ Failed to download {name}: {e}")