Skip to content

Instantly share code, notes, and snippets.

@stav
Created August 29, 2012 23:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save stav/3520611 to your computer and use it in GitHub Desktop.
Save stav/3520611 to your computer and use it in GitHub Desktop.
Google Places API Search
#!/usr/bin/python
# Google Places Search
#
# Use the Google Places API to text search for the supplied keywords and output
# the first result to standard out.
import sys
import json
import argparse
from pprint import pprint
from urllib import urlencode
from urllib2 import urlopen
apikey = 'AIzx...7wtI'
command_line = argparse.ArgumentParser(prog=sys.argv[0],
description='Get some Google Places data.')
command_line.add_argument(dest='keywords', help='Search keywords')
options = command_line.parse_args(sys.argv[1:])
print 'Keywords:', options.keywords
url = 'https://maps.googleapis.com/maps/api/place/textsearch/json?' + urlencode(dict(
query=options.keywords,
types='establishment',
language='language',
# maxResults=1,
sensor='false',
key=apikey,
))
print 'Url:', url
rp = urlopen(url)
json_str = rp.read()
json_data = json.loads(json_str)
results = json_data.get('results', [])
print 'Status:', json_data.get('status')
print 'Results', len(results)
if results:
print 'First result:'
pprint(results[0])
@stav
Copy link
Author

stav commented Aug 29, 2012

Output:

stav@maia:~$ ./google.py "github"
Keywords: github
Url: https://maps.googleapis.com/maps/api/place/textsearch/json?key=AIzx...7wtI&query=github&sensor=false&language=language&types=establishment
Status: OK
Results 20
First result:
{u'formatted_address': u'548 4th Street, San Francisco, CA, United States',
 u'geometry': {u'location': {u'lat': 37.778896, u'lng': -122.397438}},
 u'icon': u'http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png',
 u'id': u'02253533fb4e075742a640367ffc283fd15cafed',
 u'name': u'GitHub, Inc',
 u'rating': 4.6,
 u'reference': u'CnRo...EpC0',
 u'types': [u'establishment']}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment