-
-
Save joskid/1281109 to your computer and use it in GitHub Desktop.
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
"""ajaxgoogle.py - Simple bindings to the AJAX Google Search API | |
(Just the JSON-over-HTTP bit of it, nothing to do with AJAX per se) | |
http://code.google.com/apis/ajaxsearch/documentation/reference.html#_intro_fonje | |
brendan o'connor - gist.github.com/28405 - anyall.org""" | |
try: | |
import json | |
except ImportError: | |
import simplejson as json | |
import urllib, urllib2 | |
import os.path | |
TEMPLATE = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0" | |
if os.path.exists(os.environ['HOME'] + '/.ajaxgooglekey'): | |
KEY = open(os.environ['HOME']+'/.ajaxgooglekey').read().strip() | |
else: | |
KEY = None | |
if KEY: | |
TEMPLATE += "&key=" + KEY | |
def search_url(q, **kwds): | |
url = TEMPLATE | |
url += "&q=" + urllib.quote(q) | |
if kwds: | |
url += "&" + urllib.urlencode(kwds) | |
return url | |
def search(q, **kwds): | |
"""See options at http://code.google.com/apis/ajaxsearch/documentation/reference.html#_intro_fonje including: | |
rsz= large | small (8 vs 4) | |
start= <0-indexed offset> | |
hl= <language of searcher> | |
lr= <language of results> | |
safe= active | moderate | off | |
""" | |
f = urllib2.urlopen(search_url(q, **kwds)) | |
ret = json.load(f) | |
if ret['responseStatus']==200 and ret['responseData']: return ret['responseData'] | |
raise Exception("Google API error %s: %s\n (%s)" % (ret['responseStatus'],ret['responseDetails'],repr(ret))) | |
if __name__=='__main__': | |
import sys | |
from pprint import pprint | |
if len(sys.argv) > 1: | |
q = " ".join(sys.argv[1:]) | |
else: q = "query a question to whom in what sense" | |
pprint(search(q)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment