1 # SPDX-License-Identifier: GPL-3.0-or-later
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2024 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Helper functions for accessing URL.
12 import urllib.request as urlrequest
14 from ..version import NOMINATIM_CORE_VERSION
16 LOG = logging.getLogger()
18 def get_url(url: str) -> str:
19 """ Get the contents from the given URL and return it as a UTF-8 string.
21 This version makes sure that an appropriate user agent is sent.
23 headers = {"User-Agent": f"Nominatim/{NOMINATIM_CORE_VERSION!s}"}
26 request = urlrequest.Request(url, headers=headers)
27 with urlrequest.urlopen(request) as response: # type: IO[bytes]
28 return response.read().decode('utf-8')
30 LOG.fatal('Failed to load URL: %s', url)