Skip to content

Commit

Permalink
Fix Algolia breaking py2.7 support
Browse files Browse the repository at this point in the history
  • Loading branch information
KostyaEsmukov committed May 10, 2020
1 parent ea3d3b8 commit 7eff5c7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
26 changes: 13 additions & 13 deletions geopy/geocoders/algolia.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,6 @@ def geocode(
'query': self.format_string % query,
}

_parse_json_kwargs = {}

if type is not None:
params['type'] = type

Expand All @@ -173,7 +171,6 @@ def geocode(

if language is not None:
params['language'] = language.lower()
_parse_json_kwargs['language'] = language

if countries is not None:
params['countries'] = ','.join([c.lower() for c in countries])
Expand Down Expand Up @@ -202,7 +199,7 @@ def geocode(
return self._parse_json(
self._call_geocoder(request, timeout=timeout),
exactly_one,
**_parse_json_kwargs,
language=language,
)

def reverse(
Expand Down Expand Up @@ -250,14 +247,11 @@ def reverse(
'aroundLatLng': '%s,%s' % (lat, lng),
}

_parse_json_kwargs = {}

if limit is not None:
params['hitsPerPage'] = limit

if language is not None:
params['language'] = language
_parse_json_kwargs['language'] = language

url = '?'.join((self.reverse_api, urlencode(params)))
request = Request(url)
Expand All @@ -270,21 +264,27 @@ def reverse(
return self._parse_json(
self._call_geocoder(request, timeout=timeout),
exactly_one,
**_parse_json_kwargs,
language=language,
)

@staticmethod
def _parse_feature(feature, language="default"):
def _parse_feature(feature, language):
# Parse each resource.
latitude = feature.get('_geoloc', {}).get('lat')
longitude = feature.get('_geoloc', {}).get('lng')
placename = feature['locale_names'].get(language)[0] \
if isinstance(feature['locale_names'], dict) \
else feature['locale_names'][0]

if isinstance(feature['locale_names'], dict):
if language in feature['locale_names']:
placename = feature['locale_names'][language][0]
else:
placename = feature['locale_names']["default"][0]
else:
placename = feature['locale_names'][0]

return Location(placename, (latitude, longitude), feature)

@classmethod
def _parse_json(self, response, exactly_one, language='default'):
def _parse_json(self, response, exactly_one, language):
if response is None or 'hits' not in response:
return None
features = response['hits']
Expand Down
2 changes: 2 additions & 0 deletions test/geocoders/algolia.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# coding: utf-8
from __future__ import unicode_literals
import unittest

from geopy.geocoders import AlgoliaPlaces
Expand Down

0 comments on commit 7eff5c7

Please sign in to comment.