import urllib.request, urllib.parse, urllib.error, urllib.request, urllib.error, urllib.parse
import json
import sys
import webbrowser

print(sys.argv)

api_key, userip = None, None
query = {'q' : 'great climbing in new york'}

# by default, you learn about climbing, but if you give an argument, you learn
# about other stuff.
if 1 < len(sys.argv):
  query['q'] = sys.argv[1]

url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&%s' % (
    urllib.parse.urlencode(query))

request = urllib.request.Request(url)
json = json.load(urllib.request.urlopen(request))

results = json['responseData']['results']
print("      ")
# print "results are: ", results
print("      ")
for r in results:
  print(r['title'] + ": " + r['url'])
  webbrowser.open(r['url'])
  print("----------")
