import os import requests from bs4 import BeautifulSoup # ▼ ChatGPTに渡す仮のSVG生成(今は適当なSVGで代用) def generate_svg_from_name(name): return f""" {name[0].upper()} """.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!")