Ai
1 Star 0 Fork 0

anydev/nicegui

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
fetch_google_fonts.py 1.62 KB
一键复制 编辑 原始数据 按行查看 历史
Falko Schindler 提交于 2024-02-04 18:34 +08:00 . shorten filepaths for static fonts
#!/usr/bin/env python3
import hashlib
import re
from pathlib import Path
import requests
AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'
FONTS_DIRECTORY = Path('nicegui/static/fonts')
for file in FONTS_DIRECTORY.glob('*'):
file.unlink()
css_families = [
'Roboto:100,300,400,500,700,900',
'Material+Icons',
'Material+Icons+Outlined',
'Material+Icons+Round',
'Material+Icons+Sharp',
]
css2_families = [
'Material+Symbols+Outlined',
'Material+Symbols+Rounded',
'Material+Symbols+Sharp',
]
css = '/* prettier-ignore */'
css += '\n' + requests.get(f'https://fonts.googleapis.com/css?family={"|".join(css_families)}',
headers={'User-Agent': AGENT}, timeout=5).content.decode()
css += '\n' + requests.get(f'https://fonts.googleapis.com/css?family={"|".join(css2_families)}',
headers={'User-Agent': AGENT}, timeout=5).content.decode()
for font_url in re.findall(r'url\((.*?)\)', css):
font = requests.get(font_url, timeout=5).content
filepath = FONTS_DIRECTORY.joinpath(font_url.split('/')[-1])
filepath = filepath.with_stem(hashlib.sha256(filepath.stem.encode()).hexdigest()[:16])
if filepath.exists():
raise RuntimeError(f'Duplicate filepath: {filepath}')
filepath.write_bytes(font)
css = css.replace(font_url, f'fonts/{filepath.name}')
css = css.replace('https://fonts.gstatic.com/s/materialicons/v140', 'fonts')
css = css.replace('https://fonts.gstatic.com/s/roboto/v30', 'fonts')
css = css.replace("'", '"')
Path('nicegui/static/fonts.css').write_text(css)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/anydev/nicegui.git
git@gitee.com:anydev/nicegui.git
anydev
nicegui
nicegui
main

搜索帮助