1
0
moji/scpt/ai_svg_package/fontawesome.py
2025-05-28 15:36:54 +09:00

59 lines
2.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
import requests
from bs4 import BeautifulSoup
# ▼ ChatGPTに渡す仮のSVG生成今は適当なSVGで代用
def generate_svg_from_name(name):
return f"""
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64">
<text x="0" y="55" font-size="60">{name[0].upper()}</text>
</svg>
""".strip()
# ▼ 1. Font Awesomeの名前一覧をスクレイピングFont Awesome 6 Free icons
def fetch_fontawesome_names(url="https://fontawesome.com/icons"):
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
icons = soup.select('[class^="fa-"]')
names = set()
for tag in icons:
classes = tag.get("class", [])
for cls in classes:
if cls.startswith("fa-") and not cls.startswith("fa-fw"):
names.add(cls.replace("fa-", ""))
return sorted(names)
# ▼ 2. SVGファイル保存
def save_svgs(icon_names, output_dir="svgs"):
os.makedirs(output_dir, exist_ok=True)
for name in icon_names:
svg = generate_svg_from_name(name)
with open(f"{output_dir}/{name}.svg", "w", encoding="utf-8") as f:
f.write(svg)
# ▼ 3. CSS生成
def generate_css(icon_names, output_file="icons.css"):
lines = [":root {"]
for name in icon_names:
lines.append(f""" --icon-{name}: url("svgs/{name}.svg");""")
lines.append("}\n")
lines.append(".icon { width: 1em; height: 1em; display: inline-block; background-size: contain; background-repeat: no-repeat; }\n")
for name in icon_names:
lines.append(f""".icon-{name} {{ background-image: var(--icon-{name}); }}""")
with open(output_file, "w", encoding="utf-8") as f:
f.write("\n".join(lines))
# ▼ 全自動実行
if __name__ == "__main__":
print("Fetching font names...")
icon_names = fetch_fontawesome_names()
print(f"Found {len(icon_names)} icons")
print("Generating SVGs...")
save_svgs(icon_names[:10]) # ※テストとして10件のみ処理全件だと数百個以上
print("Generating CSS...")
generate_css(icon_names[:10])
print("Complete!")