2010-01-17 04:24:36 +00:00
|
|
|
import random
|
2010-04-23 03:47:41 +00:00
|
|
|
|
|
|
|
from util import hook, http
|
2010-01-17 04:24:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
def api_get(kind, query):
|
2010-04-23 03:47:41 +00:00
|
|
|
url = 'http://ajax.googleapis.com/ajax/services/search/%s?' \
|
|
|
|
'v=1.0&safe=off'
|
|
|
|
return http.get_json(url % kind, q=query)
|
2010-01-17 04:24:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
@hook.command
|
|
|
|
def gis(inp):
|
|
|
|
'''.gis <term> -- returns first google image result (safesearch off)'''
|
|
|
|
|
|
|
|
parsed = api_get('images', inp)
|
|
|
|
if not 200 <= parsed['responseStatus'] < 300:
|
|
|
|
raise IOError('error searching for images: %d: %s' % (
|
|
|
|
parsed['responseStatus'], ''))
|
|
|
|
if not parsed['responseData']['results']:
|
|
|
|
return 'no images found'
|
2010-03-01 02:32:41 +00:00
|
|
|
return random.choice(parsed['responseData']['results'][:10]) \
|
|
|
|
['unescapedUrl'] # squares is dumb
|
2010-01-17 04:24:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
@hook.command('g')
|
2010-05-07 23:16:44 +00:00
|
|
|
@hook.command
|
2010-01-17 04:24:36 +00:00
|
|
|
def google(inp):
|
|
|
|
'''.g/.google <query> -- returns first google search result'''
|
|
|
|
|
|
|
|
parsed = api_get('web', inp)
|
|
|
|
if not 200 <= parsed['responseStatus'] < 300:
|
|
|
|
raise IOError('error searching for pages: %d: %s' % (
|
|
|
|
parsed['responseStatus'], ''))
|
|
|
|
if not parsed['responseData']['results']:
|
|
|
|
return 'no results found'
|
|
|
|
|
|
|
|
result = parsed['responseData']['results'][0]
|
|
|
|
|
2010-07-09 01:18:25 +00:00
|
|
|
|
|
|
|
title = result['titleNoFormatting']
|
|
|
|
content = result['content']
|
|
|
|
|
|
|
|
if len(content) == 0:
|
|
|
|
content = "No description available"
|
|
|
|
else:
|
|
|
|
content = http.html.fromstring(content).text_content()
|
2010-01-17 04:24:36 +00:00
|
|
|
|
|
|
|
out = '%s -- \x02%s\x02: "%s"' % (result['unescapedUrl'], title, content)
|
|
|
|
out = ' '.join(out.split())
|
|
|
|
|
|
|
|
if len(out) > 300:
|
|
|
|
out = out[:out.rfind(' ')] + '..."'
|
|
|
|
|
|
|
|
return out
|