Extract geolocation with places api
parent
93fb24c138
commit
4877e84c41
|
@ -80,18 +80,24 @@ def extract_places(apikey):
|
|||
results = requests.get('https://maps.googleapis.com/maps/api/place/findplacefromtext/json', params={
|
||||
'input': data.entity_info,
|
||||
'inputtype': 'textquery',
|
||||
'fields': 'formatted_address',
|
||||
'fields': 'formatted_address,geometry',
|
||||
'key': apikey,
|
||||
}).json()
|
||||
|
||||
if results['status'] == 'OK':
|
||||
address = results['candidates'][0]['formatted_address']
|
||||
place = results['candidates'][0]
|
||||
address = place['formatted_address']
|
||||
re_result = re.findall(r'^(.*), (\d{2}-\d{3}) (.*), Polska$', address)
|
||||
if not re_result:
|
||||
print(' -> unparseable', address)
|
||||
else:
|
||||
data.shipping_street, data.shipping_postalcode, data.shipping_city = re_result[0]
|
||||
print(' -> saved', data.shipping_street, data.shipping_postalcode, data.shipping_city)
|
||||
|
||||
if place.get('geometry', {}).get('location'):
|
||||
data.shipping_latitude = place['geometry']['location']['lat']
|
||||
data.shipping_longitude = place['geometry']['location']['lng']
|
||||
print(' -> got geolocation %r %r' % (data.shipping_latitude, data.shipping_longitude))
|
||||
else:
|
||||
print(' -> not found (%s)' % (results['status'],))
|
||||
except Exception as exc:
|
||||
|
|
Loading…
Reference in New Issue