Created
August 29, 2012 23:54
-
-
Save stav/3520611 to your computer and use it in GitHub Desktop.
Google Places API Search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: