From 8f6e45dc6191842063966039a9d5ee258e9e8f25 Mon Sep 17 00:00:00 2001 From: Zsolt Ero Date: Wed, 10 Jan 2024 21:14:23 +0100 Subject: [PATCH] work --- scripts/http_host/host_manager.py | 3 +- .../http_host_lib/download_assets.py | 36 +++++++++++++++++-- .../http_host_lib/templates/nginx_cf.conf | 4 +-- scripts/http_host/http_host_lib/utils.py | 2 +- 4 files changed, 39 insertions(+), 6 deletions(-) diff --git a/scripts/http_host/host_manager.py b/scripts/http_host/host_manager.py index 334061e..21ed521 100755 --- a/scripts/http_host/host_manager.py +++ b/scripts/http_host/host_manager.py @@ -7,7 +7,7 @@ from pathlib import Path import click import requests from http_host_lib import DEFAULT_ASSETS_DIR, DEFAULT_RUNS_DIR, MNT_DIR -from http_host_lib.download_assets import download_fonts, download_natural_earth +from http_host_lib.download_assets import download_fonts, download_natural_earth, download_sprites from http_host_lib.download_tileset import download_and_extract_tileset from http_host_lib.mount import clean_up_mounts, create_fstab from http_host_lib.nginx import write_nginx_config @@ -95,6 +95,7 @@ def download_assets(assets_dir: Path): download_fonts(assets_dir) download_natural_earth(assets_dir) + download_sprites(assets_dir) @cli.command() diff --git a/scripts/http_host/http_host_lib/download_assets.py b/scripts/http_host/http_host_lib/download_assets.py index 293e1d6..e3f0f55 100644 --- a/scripts/http_host/http_host_lib/download_assets.py +++ b/scripts/http_host/http_host_lib/download_assets.py @@ -2,7 +2,9 @@ import shutil import subprocess from pathlib import Path -from http_host_lib.utils import download_if_size_differs +import requests + +from http_host_lib.utils import download_file_aria2, download_if_size_differs def download_fonts(assets_dir: Path): @@ -18,7 +20,7 @@ def download_fonts(assets_dir: Path): for font in ['ofm']: url = f'https://assets.openfreemap.com/fonts/{font}.tar.gz' - local_file = fonts_dir / f'{font}.tgz' + local_file = fonts_dir / f'{font}.tar.gz' if not download_if_size_differs(url, local_file): continue @@ -43,6 +45,36 @@ def download_fonts(assets_dir: Path): shutil.rmtree(fonts_temp, ignore_errors=True) +def download_sprites(assets_dir: Path): + """ + Download and extract sprites if their file size differ. + """ + + sprites_dir = assets_dir / 'sprites' + sprites_dir.mkdir(exist_ok=True, parents=True) + + r = requests.get('https://assets.openfreemap.com/index.txt', timeout=30) + r.raise_for_status() + + sprites_remote = [l for l in r.text.splitlines() if l.startswith('sprites/')] + + for sprite in sprites_remote: + sprite_name = sprite.split('/')[1].replace('.tar.gz', '') + + if (sprites_dir / sprite_name).is_dir(): + continue + + url = f'https://assets.openfreemap.com/sprites/{sprite_name}.tar.gz' + local_file = sprites_dir / 'temp.tar.gz' + download_file_aria2(url, local_file) + + subprocess.run( + ['tar', '-xzf', local_file, '-C', sprites_dir], + check=True, + ) + local_file.unlink() + + def download_natural_earth(assets_dir: Path): ne_dir = assets_dir / 'natural_earth' diff --git a/scripts/http_host/http_host_lib/templates/nginx_cf.conf b/scripts/http_host/http_host_lib/templates/nginx_cf.conf index 034be5a..95c753b 100644 --- a/scripts/http_host/http_host_lib/templates/nginx_cf.conf +++ b/scripts/http_host/http_host_lib/templates/nginx_cf.conf @@ -38,10 +38,10 @@ server { add_header Cache-Control public; } - location /ne/ { + location /ne2_shaded/ { # trailing slash - alias /data/ofm/http_host/assets/natural_earth/tiles/; # trailing slash + alias /data/ofm/http_host/assets/natural_earth/tiles/natural_earth_2_shaded_relief.raster/; # trailing slash try_files $uri =404; expires 1d; # target 10y diff --git a/scripts/http_host/http_host_lib/utils.py b/scripts/http_host/http_host_lib/utils.py index 0a9e311..d86daf8 100644 --- a/scripts/http_host/http_host_lib/utils.py +++ b/scripts/http_host/http_host_lib/utils.py @@ -39,7 +39,7 @@ def get_remote_file_size(url: str) -> int | None: def download_file_aria2(url: str, local_file: Path): - print(f' downloading: {url} into {local_file}') + print(f' downloading {url} into {local_file}') local_file.unlink(missing_ok=True) subprocess.run(