-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdl_google_fonts.py
42 lines (32 loc) · 1.21 KB
/
dl_google_fonts.py
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
import asyncio
import datetime
import os
import re
from concurrent.futures import ThreadPoolExecutor
from urllib.parse import urlparse
import requests
exector = ThreadPoolExecutor(max_workers=10)
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36"
}
addr = "https://fonts.googleapis.com/css2?family=Noto+Sans+SC&display=swap"
r = requests.get(addr, headers=headers)
rule = re.compile(r"https://[-\w\d/.]*")
def dl(uri, file_path):
# await asyncio.sleep(0.1)
print(uri, file_path, datetime.datetime.now())
rr = requests.get(uri, headers=headers)
with open(file_path, "wb") as wf:
wf.write(rr.content)
async def main():
loop = asyncio.get_event_loop()
for l in r.text.splitlines():
if re.search(rule, l):
src = re.findall(rule, l)[0]
split_path = urlparse(src)
dirs = split_path.path.split("/")[1:-1]
os.makedirs("/".join(dirs), exist_ok=True)
file_path = "/".join(split_path.path.split("/")[1:])
if not os.path.exists(file_path):
await loop.run_in_executor(exector, dl, src, file_path)
asyncio.run(main())