114 lines
3.9 KiB
Python
Executable File
114 lines
3.9 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
from pathlib import Path
|
|
from urllib.parse import urljoin
|
|
from urllib.request import Request, urlopen
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
LIBS_STATIC_DIR = "libs"
|
|
BOOTSTRAP_VERSION = "5.3.3"
|
|
BOOTSWATCH_VERSION = "5.3.3"
|
|
FONTAWESOME_VERSION = "6.5.2"
|
|
FLAG_ICONS_VERSION = "7.2.3"
|
|
SWAGGER_UI_VERSION = "5"
|
|
SOCKET_IO_VERSION = "4.7.5"
|
|
BOOTSTRAP_THEMES = (
|
|
"default",
|
|
"flatly",
|
|
"litera",
|
|
"lumen",
|
|
"minty",
|
|
"sketchy",
|
|
"solar",
|
|
"spacelab",
|
|
"united",
|
|
"zephyr",
|
|
)
|
|
STATIC_ASSETS = {
|
|
"bootstrap_js": {
|
|
"local": f"{LIBS_STATIC_DIR}/bootstrap/{BOOTSTRAP_VERSION}/js/bootstrap.bundle.min.js",
|
|
"cdn": f"https://cdn.jsdelivr.net/npm/bootstrap@{BOOTSTRAP_VERSION}/dist/js/bootstrap.bundle.min.js",
|
|
},
|
|
"socket_io_js": {
|
|
"local": f"{LIBS_STATIC_DIR}/socket.io/{SOCKET_IO_VERSION}/socket.io.min.js",
|
|
"cdn": f"https://cdn.socket.io/{SOCKET_IO_VERSION}/socket.io.min.js",
|
|
},
|
|
"fontawesome_css": {
|
|
"local": f"{LIBS_STATIC_DIR}/fontawesome/{FONTAWESOME_VERSION}/css/all.min.css",
|
|
"cdn": f"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/{FONTAWESOME_VERSION}/css/all.min.css",
|
|
},
|
|
"flag_icons_css": {
|
|
"local": f"{LIBS_STATIC_DIR}/flag-icons/{FLAG_ICONS_VERSION}/css/flag-icons.min.css",
|
|
"cdn": f"https://cdn.jsdelivr.net/gh/lipis/flag-icons@{FLAG_ICONS_VERSION}/css/flag-icons.min.css",
|
|
},
|
|
"swagger_css": {
|
|
"local": f"{LIBS_STATIC_DIR}/swagger-ui/{SWAGGER_UI_VERSION}/swagger-ui.css",
|
|
"cdn": f"https://cdn.jsdelivr.net/npm/swagger-ui-dist@{SWAGGER_UI_VERSION}/swagger-ui.css",
|
|
},
|
|
"swagger_js": {
|
|
"local": f"{LIBS_STATIC_DIR}/swagger-ui/{SWAGGER_UI_VERSION}/swagger-ui-bundle.js",
|
|
"cdn": f"https://cdn.jsdelivr.net/npm/swagger-ui-dist@{SWAGGER_UI_VERSION}/swagger-ui-bundle.js",
|
|
},
|
|
}
|
|
URL_RE = re.compile(r"url\((['\"]?)(?!data:)(?!https?:)([^)'\"]+)\1\)")
|
|
|
|
|
|
def bootstrap_css_asset(theme: str) -> dict[str, str]:
|
|
if theme == "default":
|
|
return {
|
|
"local": f"{LIBS_STATIC_DIR}/bootstrap/{BOOTSTRAP_VERSION}/css/bootstrap.min.css",
|
|
"cdn": f"https://cdn.jsdelivr.net/npm/bootstrap@{BOOTSTRAP_VERSION}/dist/css/bootstrap.min.css",
|
|
}
|
|
return {
|
|
"local": f"{LIBS_STATIC_DIR}/bootswatch/{BOOTSWATCH_VERSION}/{theme}/bootstrap.min.css",
|
|
"cdn": f"https://cdn.jsdelivr.net/npm/bootswatch@{BOOTSWATCH_VERSION}/dist/{theme}/bootstrap.min.css",
|
|
}
|
|
|
|
|
|
def download(url: str, dest: Path) -> None:
|
|
dest.parent.mkdir(parents=True, exist_ok=True)
|
|
req = Request(url, headers={"User-Agent": "pyTorrent installer"})
|
|
with urlopen(req, timeout=60) as response:
|
|
data = response.read()
|
|
if not data:
|
|
raise RuntimeError(f"Empty response for {url}")
|
|
tmp = dest.with_suffix(dest.suffix + ".tmp")
|
|
tmp.write_bytes(data)
|
|
tmp.replace(dest)
|
|
print(f"OK {dest.relative_to(ROOT)}")
|
|
|
|
|
|
def download_css_with_assets(url: str, dest: Path) -> None:
|
|
download(url, dest)
|
|
text = dest.read_text(encoding="utf-8", errors="ignore")
|
|
for match in URL_RE.finditer(text):
|
|
rel = match.group(2).split("#", 1)[0].split("?", 1)[0]
|
|
if not rel:
|
|
continue
|
|
asset_url = urljoin(url, rel)
|
|
asset_dest = (dest.parent / rel).resolve()
|
|
try:
|
|
asset_dest.relative_to(ROOT)
|
|
except ValueError:
|
|
continue
|
|
if not asset_dest.exists():
|
|
download(asset_url, asset_dest)
|
|
|
|
|
|
def main() -> None:
|
|
items = list(STATIC_ASSETS.values())
|
|
items.extend(bootstrap_css_asset(theme) for theme in BOOTSTRAP_THEMES)
|
|
for item in items:
|
|
url = item["cdn"]
|
|
dest = ROOT / "pytorrent" / "static" / item["local"]
|
|
if dest.suffix == ".css":
|
|
download_css_with_assets(url, dest)
|
|
else:
|
|
download(url, dest)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|