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

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]
referrer = "http://cs.nyu.edu/q/3900610"

if userip:
    query.update(userip=userip)
if api_key:
    query.update(key=api_key)

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

request = urllib.request.Request(url, headers=dict(Referer=referrer))
json = json.load(urllib.request.urlopen(request))

results = json['responseData']['results']
for r in results:
  print(r['title'] + ": " + r['url'])
  print("----------")
