From bb0022cb4db1388050d47b60b6161a873af253b6 Mon Sep 17 00:00:00 2001 From: Zsolt Ero Date: Tue, 2 Jan 2024 16:14:55 +0100 Subject: [PATCH] assets downloader --- scripts/http_host/.gitignore | 2 + scripts/http_host/download_assets.py | 73 +++++++++++++++++++ .../{downloader.py => download_tiles.py} | 0 scripts/http_host/mounter.py | 2 +- 4 files changed, 76 insertions(+), 1 deletion(-) create mode 100755 scripts/http_host/download_assets.py rename scripts/http_host/{downloader.py => download_tiles.py} (100%) diff --git a/scripts/http_host/.gitignore b/scripts/http_host/.gitignore index a6c57f5..5f718dc 100644 --- a/scripts/http_host/.gitignore +++ b/scripts/http_host/.gitignore @@ -1 +1,3 @@ *.json + +_assets diff --git a/scripts/http_host/download_assets.py b/scripts/http_host/download_assets.py new file mode 100755 index 0000000..b8ff5bf --- /dev/null +++ b/scripts/http_host/download_assets.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python3 +import shutil +import subprocess +import sys +from pathlib import Path + +import click +import requests + + +DEFAULT_ASSETS_DIR = Path('/data/ofm/http_host/assets') + + +@click.command() +@click.option( + '--assets-dir', + help='Specify assets directory', + type=click.Path(dir_okay=True, file_okay=False, path_type=Path), +) +def cli(assets_dir): + """ + Downloads and extracts assets + """ + + if not assets_dir: + assets_dir = DEFAULT_ASSETS_DIR + + if not assets_dir.parent.exists(): + sys.exit("asset dir's parent doesn't exist") + + download_fonts(assets_dir) + + +def download_fonts(assets_dir): + fonts_dir = assets_dir / 'fonts' + fonts_dir.mkdir(exist_ok=True, parents=True) + + for font in ['ml', 'omt', 'pm']: + url = f'https://assets.openfreemap.com/fonts/{font}.tgz' + local_file = fonts_dir / f'{font}.tgz' + download_if_size_differs(url, local_file) + + +def download_if_size_differs(url: str, local_file: Path): + if not local_file.exists() or local_file.stat().st_size != get_remote_file_size(url): + download_file(url, local_file) + + +def get_remote_file_size(url: str): + r = requests.head(url) + size = r.headers.get('Content-Length') + return int(size) if size else None + + +def download_file(url, local_file): + click.echo(f'Downloading: {url} into {local_file}') + + subprocess.run( + [ + 'aria2c', + '--split=8', + '--max-connection-per-server=8', + '--file-allocation=none', + '-o', + local_file, + url, + ], + check=True, + ) + + +if __name__ == '__main__': + cli() diff --git a/scripts/http_host/downloader.py b/scripts/http_host/download_tiles.py similarity index 100% rename from scripts/http_host/downloader.py rename to scripts/http_host/download_tiles.py diff --git a/scripts/http_host/mounter.py b/scripts/http_host/mounter.py index a64d659..c82cbf6 100755 --- a/scripts/http_host/mounter.py +++ b/scripts/http_host/mounter.py @@ -21,7 +21,7 @@ def cli(): sys.exit('Needs sudo') if not Path('/data/ofm/http_host/runs').exists(): - sys.exit('downloader.py needs to be run first') + sys.exit('download_tiles.py needs to be run first') clean_up_mounts()