Skip to content

Commit

Permalink
Nominatim: warn for various sample user_agents too
Browse files Browse the repository at this point in the history
  • Loading branch information
KostyaEsmukov committed Jun 21, 2020
1 parent 04957b7 commit b4e98ee
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
14 changes: 11 additions & 3 deletions geopy/geocoders/osm.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
__all__ = ("Nominatim", )

_DEFAULT_NOMINATIM_DOMAIN = 'nominatim.openstreetmap.org'
_REJECTED_USER_AGENTS = (
# Various sample user-agent strings mentioned in docs:
"my-application",
"my_app/1",
"my_user_agent/1.0",
"specify_your_app_name_here",
_DEFAULT_USER_AGENT,
)


class Nominatim(Geocoder):
Expand Down Expand Up @@ -175,9 +183,9 @@ def __init__(
self.domain = domain.strip('/')

if (self.domain == _DEFAULT_NOMINATIM_DOMAIN
and self.headers['User-Agent'] == _DEFAULT_USER_AGENT):
and self.headers['User-Agent'] in _REJECTED_USER_AGENTS):
warnings.warn(
'Using Nominatim with the default "%s" `user_agent` is '
'Using Nominatim with default or sample `user_agent` "%s" is '
'strongly discouraged, as it violates Nominatim\'s ToS '
'https://operations.osmfoundation.org/policies/nominatim/ '
'and may possibly cause 403 and 429 HTTP errors. '
Expand All @@ -186,7 +194,7 @@ def __init__(
'overriding the default `user_agent`: '
'`geopy.geocoders.options.default_user_agent = "my-application"`. '
'In geopy 2.0 this will become an exception.'
% _DEFAULT_USER_AGENT,
% self.headers['User-Agent'],
DeprecationWarning,
stacklevel=2
)
Expand Down
11 changes: 9 additions & 2 deletions test/geocoders/nominatim.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ def test_user_agent_default(self):

def test_user_agent_custom(self):
geocoder = self.make_geocoder(
user_agent='my_user_agent/1.0'
user_agent='my_test_application'
)
assert geocoder.headers['User-Agent'] == 'my_user_agent/1.0'
assert geocoder.headers['User-Agent'] == 'my_test_application'

def test_reverse(self):
location = self.reverse_run(
Expand Down Expand Up @@ -348,6 +348,13 @@ def test_default_user_agent_warning(self):
Nominatim()
assert 1 == len(w)

def test_example_user_agent_warning(self):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always')
Nominatim(user_agent="specify_your_app_name_here")
assert 1 == len(w)

def test_custom_user_agent_works(self):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always')
Nominatim(user_agent='my_application')
Expand Down

0 comments on commit b4e98ee

Please sign in to comment.