X-Git-Url: https://git.openstreetmap.org./nominatim.git/blobdiff_plain/5b46fcad8ec166d1b8e35cfbc58405b71e27caa3..d78f0ba80470a33a7a76edfe3ace5108684873cd:/nominatim/tools/exec_utils.py?ds=sidebyside diff --git a/nominatim/tools/exec_utils.py b/nominatim/tools/exec_utils.py index 9e16e293..0d3db204 100644 --- a/nominatim/tools/exec_utils.py +++ b/nominatim/tools/exec_utils.py @@ -3,8 +3,13 @@ Helper functions for executing external programs. """ import logging import subprocess +import urllib.request as urlrequest from urllib.parse import urlencode +from ..version import NOMINATIM_VERSION + +LOG = logging.getLogger() + def run_legacy_script(script, *args, nominatim_env=None, throw_on_fail=False): """ Run a Nominatim PHP script with the given arguments. @@ -80,3 +85,16 @@ def run_api_script(endpoint, project_dir, extra_env=None, phpcgi_bin=None, print(result[content_start + 4:].replace('\\n', '\n')) return 0 + + +def get_url(url): + """ Get the contents from the given URL and return it as a UTF-8 string. + """ + headers = {"User-Agent" : "Nominatim/" + NOMINATIM_VERSION} + + try: + with urlrequest.urlopen(urlrequest.Request(url, headers=headers)) as response: + return response.read().decode('utf-8') + except: + LOG.fatal('Failed to load URL: %s', url) + raise