assets downloader

This commit is contained in:
Zsolt Ero
2024-01-02 16:14:55 +01:00
parent 3bcee287bf
commit bb0022cb4d
4 changed files with 76 additions and 1 deletions

View File

@@ -1 +1,3 @@
*.json *.json
_assets

View File

@@ -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()

View File

@@ -21,7 +21,7 @@ def cli():
sys.exit('Needs sudo') sys.exit('Needs sudo')
if not Path('/data/ofm/http_host/runs').exists(): 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() clean_up_mounts()