def detect(text): uri = 'http://ajax.googleapis.com/ajax/services/language/detect' q = urllib.quote(text) bytes = web.get(uri + '?q=' + q + '&v=1.0') result = web.json(bytes) try: return result['responseData']['language'] except Exception: return None
def translate(text, input, output): uri = 'http://ajax.googleapis.com/ajax/services/language/translate' q = urllib.quote(text) pair = input + '%7C' + output bytes = web.get(uri + '?q=' + q + '&v=1.0&langpair=' + pair) result = web.json(bytes) try: return result['responseData']['translatedText'].encode('cp1252') except Exception: return None
def search(query): """Search using AjaxSearch, and return its JSON.""" uri = 'http://ajax.googleapis.com/ajax/services/search/web' args = '?v=1.0&safe=off&q=' + web.urllib.quote(query.encode('utf-8')) handler = web.urllib._urlopener web.urllib._urlopener = Grab() bytes = web.get(uri + args) web.urllib._urlopener = handler return web.json(bytes)
def location(name): name = urllib.quote(name.encode('utf-8')) uri = 'http://ws.geonames.org/searchJSON?q=%s&maxRows=1' % name for i in xrange(10): u = urllib.urlopen(uri) if u is not None: break bytes = u.read() u.close() results = web.json(bytes) try: name = results['geonames'][0]['name'] except IndexError: return '?', '?', '0', '0' countryName = results['geonames'][0]['countryName'] lat = results['geonames'][0]['lat'] lng = results['geonames'][0]['lng'] return name, countryName, lat, lng