DenisCarriere / geocoder

:earth_asia: Python Geocoder
http://geocoder.readthedocs.org
MIT License
1.63k stars 288 forks source link

KeyError on call to geocoder.google #294

Closed MattX closed 7 years ago

MattX commented 7 years ago

Hello,

I'm getting a KeyError when looking up a specific string with the Google geocoder backend:

>>> geocoder.google("Cerro Torre Mountain")
Requested https://maps.googleapis.com/maps/api/geocode/json?address=Cerro+Torre+Mountain&bounds=&components=&region=&language=

(lots of traceback)

~/src/earth/env/lib/python3.6/site-packages/geocoder/base.py in _parse_json_with_fieldnames(self)
    117             if not key.startswith('_') and key not in self._TO_EXCLUDE:
    118                 self.fieldnames.append(key)
--> 119                 value = getattr(self, key)
    120                 if value:
    121                     self.json[key] = value

~/src/earth/env/lib/python3.6/site-packages/geocoder/google.py in country(self)
    132     @property
    133     def country(self):
--> 134         return self.raw['country'].get('short_name')
    135 
    136     @property

KeyError: 'country'

This doesn't seem to happen with the other place names I've tried, and the JSON response I get looks valid. Is this expected?

Sorry if I've missed something in the docs and this is not a bug!

ebreton commented 7 years ago

Hi @MattX ,

it is indeed a bug. (most of the) providers failed when they tried to access a JSON attribute that was actually not in the JSON response.

Should be fixed in the branch of PR #293

Could you confirm ?

MattX commented 7 years ago

Yeah, it works for me when using the fix_opencage branch. Thanks!

ebreton commented 7 years ago

Great !

@DenisCarriere, I assign the issue to you... to let you close it once you have merged the PR :)

DenisCarriere commented 7 years ago

👍 @ebreton Awesome thanks for the efforts, i'll publish a new release with the merged PR fix.