-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembed.py
More file actions
42 lines (33 loc) · 1.24 KB
/
embed.py
File metadata and controls
42 lines (33 loc) · 1.24 KB
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
# /// script
# dependencies = [
# "httpx",
# ]
# requires-python = ">=3.11"
# ///
import asyncio
from pathlib import Path
import httpx
mapping = {
"https://cdn.jsdelivr.net/npm/@stoplight/elements/web-components.min.js": "web-components.min.js",
"https://cdn.jsdelivr.net/npm/@stoplight/elements/styles.min.css": "styles.min.css",
"https://cdn.jsdelivr.net/npm/@scalar/api-reference": "scalar-api-reference.js",
}
async def download(url: str, root: Path, filename: str) -> None:
async with httpx.AsyncClient(
headers={
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
}
) as client:
resp = await client.get(url, follow_redirects=True)
resp.raise_for_status()
file = root.joinpath(filename)
await asyncio.to_thread(file.write_bytes, resp.content)
async def main():
static = Path(__file__).parent.joinpath("src/stopments/static")
async with asyncio.TaskGroup() as tg:
for url, filename in mapping.items():
task = download(url, static, filename)
tg.create_task(task)
print("Downloaded all files.") # noqa: T201
if __name__ == "__main__":
asyncio.run(main())