mirror of
https://github.com/hyperknot/openfreemap.git
synced 2026-05-21 14:02:15 +00:00
assets downloader
This commit is contained in:
2
scripts/http_host/.gitignore
vendored
2
scripts/http_host/.gitignore
vendored
@@ -1 +1,3 @@
|
||||
*.json
|
||||
|
||||
_assets
|
||||
|
||||
73
scripts/http_host/download_assets.py
Executable file
73
scripts/http_host/download_assets.py
Executable 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()
|
||||
@@ -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()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user