Skip to content

Commit

Permalink
added fixed user agent to headers for every geocoder call, configurab…
Browse files Browse the repository at this point in the history
…le for Nominatim

added tests for custom and default user agents

removed caveman debugging print artifact ;-)

moved __version__ to own module in order to avoid circular imports

refactored the default user agent into a constant

added customizable user agent to arcgis

added customizable user agent to baidu

added customizable user agent to bing

added customizable user agent to databc

added customizable user agent to dot_us

added customizable user agent to geocodefarm

added customizable user agent to geonames

added customizable user agent to googleV3

added customizable user agent to ignfrance

added customizable user agent to navidata

added customizable user agent to opencage

added customizable user agent to openmapquest

added customizable user agent to placefinder

added customizable user agent to smartystreets

added customizable user agent to what3words

added customizable user agent to yandex

a pep improvement mostly a dummy commit for a travis retriggering...

fixed some bad merges
  • Loading branch information
sebastianneubauer authored and ijl committed Aug 31, 2015
1 parent b2fee93 commit 7fda9f1
Show file tree
Hide file tree
Showing 41 changed files with 233 additions and 36 deletions.
2 changes: 1 addition & 1 deletion geopy/__init__.py
Expand Up @@ -13,4 +13,4 @@
from geopy.geocoders import * # pylint: disable=W0401


__version__ = "1.10.0"
from geopy.version import GEOPY_VERSION as __version__
5 changes: 3 additions & 2 deletions geopy/geocoders/arcgis.py
Expand Up @@ -29,7 +29,8 @@ class ArcGIS(Geocoder): # pylint: disable=R0921,R0902,W0223

def __init__(self, username=None, password=None, referer=None, # pylint: disable=R0913
token_lifetime=60, scheme=DEFAULT_SCHEME,
timeout=DEFAULT_TIMEOUT, proxies=None):
timeout=DEFAULT_TIMEOUT, proxies=None,
user_agent=None):
"""
Create a ArcGIS-based geocoder.
Expand Down Expand Up @@ -63,7 +64,7 @@ def __init__(self, username=None, password=None, referer=None, # pylint: disable
:class:`urllib2.ProxyHandler`.
"""
super(ArcGIS, self).__init__(
scheme=scheme, timeout=timeout, proxies=proxies
scheme=scheme, timeout=timeout, proxies=proxies, user_agent=user_agent
)
if username or password or referer:
if not (username and password and referer):
Expand Down
3 changes: 2 additions & 1 deletion geopy/geocoders/baidu.py
Expand Up @@ -28,6 +28,7 @@ def __init__(
scheme='http',
timeout=DEFAULT_TIMEOUT,
proxies=None,
user_agent=None
):
"""
Initialize a customized Baidu geocoder using the v2 API.
Expand All @@ -47,7 +48,7 @@ def __init__(
:class:`urllib2.ProxyHandler`.
"""
super(Baidu, self).__init__(
scheme=scheme, timeout=timeout, proxies=proxies
scheme=scheme, timeout=timeout, proxies=proxies, user_agent=user_agent
)
self.api_key = api_key
self.scheme = scheme
Expand Down
16 changes: 11 additions & 5 deletions geopy/geocoders/base.py
Expand Up @@ -15,6 +15,7 @@
ProxyHandler,
URLError,
install_opener,
Request,
)
from geopy.point import Point
from geopy.exc import (
Expand All @@ -28,7 +29,7 @@
GeocoderUnavailable,
GeocoderParseError,
)
from geopy.util import decode_page
import geopy.util as gu


__all__ = (
Expand All @@ -44,6 +45,8 @@
DEFAULT_SCHEME = 'https'
DEFAULT_TIMEOUT = 1
DEFAULT_WKID = 4326
DEFAULT_USER_AGENT = "geopy/" + gu.get_version()


ERROR_CODE_MAP = {
400: GeocoderQueryError,
Expand All @@ -70,7 +73,8 @@ def __init__(
format_string=DEFAULT_FORMAT_STRING,
scheme=DEFAULT_SCHEME,
timeout=DEFAULT_TIMEOUT,
proxies=None
proxies=None,
user_agent=None
):
"""
Mostly-common geocoder validation, proxies, &c. Not all geocoders
Expand All @@ -84,6 +88,7 @@ def __init__(
)
self.proxies = proxies
self.timeout = timeout
self.headers = {'User-Agent': user_agent or DEFAULT_USER_AGENT}

if self.proxies:
install_opener(
Expand Down Expand Up @@ -129,7 +134,8 @@ def _call_geocoder(
requester = requester or self.urlopen

try:
page = requester(url, timeout=(timeout or self.timeout), **kwargs)
req = Request(url=url, headers=self.headers)
page = requester(req, timeout=(timeout or self.timeout), **kwargs)
except Exception as error: # pylint: disable=W0703
message = (
str(error) if not py3k
Expand Down Expand Up @@ -166,12 +172,12 @@ def _call_geocoder(
else:
status_code = None
if status_code in ERROR_CODE_MAP:
raise ERROR_CODE_MAP[page.status_code]("\n%s" % decode_page(page))
raise ERROR_CODE_MAP[page.status_code]("\n%s" % gu.decode_page(page))

if raw:
return page

page = decode_page(page)
page = gu.decode_page(page)

if deserializer is not None:
try:
Expand Down
3 changes: 2 additions & 1 deletion geopy/geocoders/bing.py
Expand Up @@ -40,6 +40,7 @@ def __init__(
scheme=DEFAULT_SCHEME,
timeout=DEFAULT_TIMEOUT,
proxies=None,
user_agent=None,
): # pylint: disable=R0913
"""Initialize a customized Bing geocoder with location-specific
address information and your Bing Maps API key.
Expand Down Expand Up @@ -70,7 +71,7 @@ def __init__(
.. versionadded:: 0.96
"""
super(Bing, self).__init__(format_string, scheme, timeout, proxies)
super(Bing, self).__init__(format_string, scheme, timeout, proxies, user_agent=user_agent)
self.api_key = api_key
self.api = "%s://dev.virtualearth.net/REST/v1/Locations" % self.scheme

Expand Down
4 changes: 2 additions & 2 deletions geopy/geocoders/databc.py
Expand Up @@ -19,7 +19,7 @@ class DataBC(Geocoder):
http://www.data.gov.bc.ca/dbc/geographic/locate/geocoding.page
"""

def __init__(self, scheme=DEFAULT_SCHEME, timeout=DEFAULT_TIMEOUT, proxies=None):
def __init__(self, scheme=DEFAULT_SCHEME, timeout=DEFAULT_TIMEOUT, proxies=None, user_agent=None):
"""
Create a DataBC-based geocoder.
Expand All @@ -35,7 +35,7 @@ def __init__(self, scheme=DEFAULT_SCHEME, timeout=DEFAULT_TIMEOUT, proxies=None)
:class:`urllib2.ProxyHandler`.
"""
super(DataBC, self).__init__(
scheme=scheme, timeout=timeout, proxies=proxies
scheme=scheme, timeout=timeout, proxies=proxies, user_agent=user_agent
)
self.api = '%s://apps.gov.bc.ca/pub/geocoder/addresses.geojson' % self.scheme

Expand Down
3 changes: 2 additions & 1 deletion geopy/geocoders/dot_us.py
Expand Up @@ -33,6 +33,7 @@ def __init__(
format_string=DEFAULT_FORMAT_STRING,
timeout=DEFAULT_TIMEOUT,
proxies=None,
user_agent=None,
): # pylint: disable=R0913
"""
:param string username:
Expand All @@ -58,7 +59,7 @@ def __init__(
.. versionadded:: 0.96
"""
super(GeocoderDotUS, self).__init__(
format_string=format_string, timeout=timeout, proxies=proxies
format_string=format_string, timeout=timeout, proxies=proxies, user_agent=user_agent
)
if username or password:
if not (username and password):
Expand Down
3 changes: 2 additions & 1 deletion geopy/geocoders/geocodefarm.py
Expand Up @@ -26,6 +26,7 @@ def __init__(
format_string=DEFAULT_FORMAT_STRING,
timeout=DEFAULT_TIMEOUT,
proxies=None,
user_agent=None,
): # pylint: disable=R0913
"""
Create a geocoder for GeocodeFarm.
Expand All @@ -46,7 +47,7 @@ def __init__(
:class:`urllib2.ProxyHandler`.
"""
super(GeocodeFarm, self).__init__(
format_string, 'https', timeout, proxies
format_string, 'https', timeout, proxies, user_agent=user_agent
)
self.api_key = api_key
self.format_string = format_string
Expand Down
5 changes: 3 additions & 2 deletions geopy/geocoders/geonames.py
Expand Up @@ -31,7 +31,8 @@ def __init__(
country_bias=None,
username=None,
timeout=DEFAULT_TIMEOUT,
proxies=None
proxies=None,
user_agent=None,
):
"""
:param string country_bias:
Expand All @@ -52,7 +53,7 @@ def __init__(
.. versionadded:: 0.96
"""
super(GeoNames, self).__init__(
scheme='http', timeout=timeout, proxies=proxies
scheme='http', timeout=timeout, proxies=proxies, user_agent=user_agent
)
if username == None:
raise ConfigurationError(
Expand Down
5 changes: 3 additions & 2 deletions geopy/geocoders/googlev3.py
Expand Up @@ -44,7 +44,8 @@ def __init__(
client_id=None,
secret_key=None,
timeout=DEFAULT_TIMEOUT,
proxies=None
proxies=None,
user_agent=None,
): # pylint: disable=R0913
"""
Initialize a customized Google geocoder.
Expand Down Expand Up @@ -80,7 +81,7 @@ def __init__(
.. versionadded:: 0.96
"""
super(GoogleV3, self).__init__(
scheme=scheme, timeout=timeout, proxies=proxies
scheme=scheme, timeout=timeout, proxies=proxies, user_agent=user_agent
)
if client_id and not secret_key:
raise ConfigurationError('Must provide secret_key with client_id.')
Expand Down
3 changes: 2 additions & 1 deletion geopy/geocoders/ignfrance.py
Expand Up @@ -50,6 +50,7 @@ def __init__(
scheme=DEFAULT_SCHEME,
timeout=DEFAULT_TIMEOUT,
proxies=None,
user_agent=None,
): # pylint: disable=R0913
"""
Initialize a customized IGN France geocoder.
Expand Down Expand Up @@ -88,7 +89,7 @@ def __init__(
"""
super(IGNFrance, self).__init__(
scheme=scheme, timeout=timeout, proxies=proxies
scheme=scheme, timeout=timeout, proxies=proxies, user_agent=user_agent
)

# Catch if no api key with username and password
Expand Down
3 changes: 2 additions & 1 deletion geopy/geocoders/navidata.py
Expand Up @@ -29,6 +29,7 @@ def __init__(
domain='api.navidata.pl',
timeout=DEFAULT_TIMEOUT,
proxies=None,
user_agent=None,
):
"""
.. versionadded:: 1.8.0
Expand All @@ -49,7 +50,7 @@ def __init__(
"""
super(NaviData, self).__init__(
scheme="http", timeout=timeout, proxies=proxies
scheme="http", timeout=timeout, proxies=proxies, user_agent=user_agent
)

self.api_key = api_key
Expand Down
3 changes: 2 additions & 1 deletion geopy/geocoders/opencage.py
Expand Up @@ -30,6 +30,7 @@ def __init__(
scheme=DEFAULT_SCHEME,
timeout=DEFAULT_TIMEOUT,
proxies=None,
user_agent=None,
): # pylint: disable=R0913
"""
Initialize a customized Open Cage Data geocoder.
Expand All @@ -52,7 +53,7 @@ def __init__(
"""
super(OpenCage, self).__init__(
scheme=scheme, timeout=timeout, proxies=proxies
scheme=scheme, timeout=timeout, proxies=proxies, user_agent=user_agent
)

self.api_key = api_key
Expand Down
3 changes: 2 additions & 1 deletion geopy/geocoders/openmapquest.py
Expand Up @@ -29,6 +29,7 @@ def __init__(
scheme=DEFAULT_SCHEME,
timeout=DEFAULT_TIMEOUT,
proxies=None,
user_agent=None,
): # pylint: disable=R0913
"""
Initialize an Open MapQuest geocoder with location-specific
Expand Down Expand Up @@ -60,7 +61,7 @@ def __init__(
.. versionadded:: 0.96
"""
super(OpenMapQuest, self).__init__(
format_string, scheme, timeout, proxies
format_string, scheme, timeout, proxies, user_agent=user_agent
)
self.api_key = api_key or ''
self.api = "%s://open.mapquestapi.com/nominatim/v1/search" \
Expand Down
8 changes: 4 additions & 4 deletions geopy/geocoders/osm.py
Expand Up @@ -42,7 +42,8 @@ def __init__(
timeout=DEFAULT_TIMEOUT,
proxies=None,
domain='nominatim.openstreetmap.org',
scheme=DEFAULT_SCHEME
scheme=DEFAULT_SCHEME,
user_agent=None
): # pylint: disable=R0913
"""
:param string format_string: String containing '%s' where the
Expand Down Expand Up @@ -74,7 +75,7 @@ def __init__(
.. versionadded:: 1.8.2
"""
super(Nominatim, self).__init__(
format_string, scheme, timeout, proxies
format_string, scheme, timeout, proxies, user_agent=user_agent
)
self.country_bias = country_bias
self.format_string = format_string
Expand All @@ -84,7 +85,6 @@ def __init__(
self.api = "%s://%s/search" % (self.scheme, self.domain)
self.reverse_api = "%s://%s/reverse" % (self.scheme, self.domain)


def geocode(
self,
query,
Expand Down Expand Up @@ -157,7 +157,7 @@ def geocode(
params.update({
'format': 'json'
})

# `viewbox` apparently replaces `view_box`
if self.view_box:
params['viewbox'] = ','.join(self.view_box)
Expand Down
5 changes: 3 additions & 2 deletions geopy/geocoders/placefinder.py
Expand Up @@ -29,7 +29,8 @@ def __init__(
consumer_key,
consumer_secret,
timeout=DEFAULT_TIMEOUT,
proxies=None
proxies=None,
user_agent=None,
): # pylint: disable=R0913
"""
:param string consumer_key: Key provided by Yahoo.
Expand All @@ -54,7 +55,7 @@ def __init__(
' Install with `pip install geopy -e ".[placefinder]"`.'
)
super(YahooPlaceFinder, self).__init__(
timeout=timeout, proxies=proxies
timeout=timeout, proxies=proxies, user_agent=user_agent
)
self.consumer_key = (
unicode(consumer_key)
Expand Down
5 changes: 3 additions & 2 deletions geopy/geocoders/smartystreets.py
Expand Up @@ -25,7 +25,8 @@ def __init__(
candidates=1,
scheme=DEFAULT_SCHEME,
timeout=DEFAULT_TIMEOUT,
proxies=None
proxies=None,
user_agent=None,
): # pylint: disable=R0913
"""
Initialize a customized SmartyStreets LiveAddress geocoder.
Expand Down Expand Up @@ -64,7 +65,7 @@ def __init__(
.. versionadded:: 0.96
"""
super(LiveAddress, self).__init__(
timeout=timeout, proxies=proxies
timeout=timeout, proxies=proxies, user_agent=user_agent
)
if scheme == "http":
raise ConfigurationError("LiveAddress now requires `https`.")
Expand Down
4 changes: 3 additions & 1 deletion geopy/geocoders/what3words.py
Expand Up @@ -36,6 +36,7 @@ def __init__(
scheme=DEFAULT_SCHEME,
timeout=DEFAULT_TIMEOUT,
proxies=None,
user_agent=None,
):
"""
Initialize a What3Words geocoder with 3-word or OneWord-address and
Expand Down Expand Up @@ -69,7 +70,8 @@ def __init__(
format_string,
scheme,
timeout,
proxies
proxies,
user_agent=user_agent,
)
self.api_key = api_key
self.api = (
Expand Down
5 changes: 3 additions & 2 deletions geopy/geocoders/yandex.py
Expand Up @@ -27,7 +27,8 @@ def __init__(
api_key=None,
lang=None,
timeout=DEFAULT_TIMEOUT,
proxies=None
proxies=None,
user_agent=None,
):
"""
Create a Yandex-based geocoder.
Expand All @@ -50,7 +51,7 @@ def __init__(
:class:`urllib2.ProxyHandler`.
"""
super(Yandex, self).__init__(
scheme='http', timeout=timeout, proxies=proxies
scheme='http', timeout=timeout, proxies=proxies, user_agent=user_agent
)
self.api_key = api_key
self.lang = lang
Expand Down

0 comments on commit 7fda9f1

Please sign in to comment.