从Json文件中读取内容,遍历Json对象,将下载地址提取出来,注入异步下载函数。
数据源:
{
"FangZhengShuSong_Regular": {
"type": "2",
"code": "fangzhengshusong",
"name": "方正书宋",
"preview": "https://wordshub.github.io/free-font/images/font/fangzhengshusong/font.svg",
"posters": [
"images/font/fangzhengshusong/poster.svg"
],
"born": "20191225",
"version": "",
"license": "商免",
"auth": true,
"source": "https://www.foundertype.com/index.php/About/bookAuth/key/my_sysq.html",
"download": "https://raw.githubusercontent.com/wordshub/free-font/master/assets/font/%E4%B8%AD%E6%96%87/%E6%96%B9%E6%AD%A3%E5%AD%97%E4%BD%93%E7%B3%BB%E5%88%97/%E6%96%B9%E6%AD%A3%E4%B9%A6%E5%AE%8B%E7%AE%80%E4%BD%93.ttf",
"desc": "「方正书宋」源自上海印刷技术研究所的宋二,原字形距今已有近60年。最初的设计就是专门针对最常用的小字号正文排版。方正书宋的字形端正清秀,中宫适度。凭着清晰爽目、久读不易疲劳、印刷适性好的优点,长期被作为杂志和书籍的正文选字体,所以也是一款非常经典的正文宋体。"
},
"FangZhengFangSong_Regular": {
"type": "2",
"code": "fangzhengfangsong",
"name": "方正仿宋",
"preview": "https://wordshub.github.io/free-font/images/font/fangzhengfangsong/font.svg",
"posters": [
"images/font/fangzhengfangsong/poster.svg"
],
"license": "商免",
"auth": true,
"source": "https://www.foundertype.com/index.php/About/bookAuth/key/my_sysq.html",
"download": "https://raw.githubusercontent.com/wordshub/free-font/master/assets/font/%E4%B8%AD%E6%96%87/%E6%96%B9%E6%AD%A3%E5%AD%97%E4%BD%93%E7%B3%BB%E5%88%97/%E6%96%B9%E6%AD%A3%E4%BB%BF%E5%AE%8B%E7%AE%80%E4%BD%93.ttf",
"desc": "「仿宋体」产生于20世纪初,由当时的铅字制作者仿宋版书中的瘦细字体而制成。方正仿宋源于铅字时代的字稿,其字身略窄,笔画瘦硬,横竖笔画等粗,起笔处有斜势棱角,折笔处棱角分明,整体字形挺拔俊秀,给人以悦目之感。因清晰美观,容易辨认,长期被用于工程图纸文字和书刊中的引文。"
}
。。。
废话不多说,上代码:
import json
import asyncio
import aiohttp
import urllib.parse
import os
from tqdm.asyncio import tqdm
basedir = 'd:/phpweb/vue-fabric-editor-main/src/assets/fonts/'
savebase = 'd:/phpweb/vue-fabric-editor-main/public/fontFile/'
async def download_file(url, save_path):
if not os.path.exists(save_path) or os.path.getsize(save_path) < 1024*1024:
try:
async with aiohttp.ClientSession() as session:
async with session.get(url) as resp:
total_size = int(resp.headers.get('Content-Length', 0))
print(f'Download: {url}')
with open(save_path, 'wb') as fd:
with tqdm(total=total_size, desc=save_path, unit='B', unit_scale=True) as pbar:
async for chunk in resp.content.iter_chunked(1024):
fd.write(chunk)
pbar.update(len(chunk))
await asyncio.sleep(1)
return save_path
except Exception as e:
print(f"Error downloading {url}: {e}")
return None
else:
print(f"File Exist {save_path}")
return None
async def main():
json_file = basedir + 'free-font.json'
with open(json_file, 'r', encoding='UTF-8') as f:
fonts = json.load(f)
print(f'Source: {json_file}')
for key, val in fonts.items():
font_savefile = urllib.parse.unquote(val['download'].replace('https://raw.githubusercontent.com/wordshub/free-font/master/', savebase))
if not os.path.exists(os.path.dirname(font_savefile)):
os.makedirs(os.path.dirname(font_savefile))
await download_file(val['download'], font_savefile)
if __name__ == "__main__":
asyncio.run(main())
自行修改参数。
运行效果如下: