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
|
|
|
|
|
|
|
|
2013-09-20 21:04:58 +00:00
|
|
|
def api_get(query, key, is_image=None, num=1):
|
|
|
|
url = ('https://www.googleapis.com/customsearch/v1?cx=007629729846476161907:ud5nlxktgcw'
|
|
|
|
'&fields=items(title,link,snippet)&safe=off' + ('&searchType=image' if is_image else ''))
|
|
|
|
return http.get_json(url, key=key, q=query, num=num)
|
2010-01-17 04:24:36 +00:00
|
|
|
|
|
|
|
|
2013-09-20 21:04:58 +00:00
|
|
|
@hook.api_key('google')
|
2010-01-17 04:24:36 +00:00
|
|
|
@hook.command
|
2013-09-20 21:04:58 +00:00
|
|
|
def gis(inp, api_key=None):
|
|
|
|
'''.gis <term> -- finds an image using google images (safesearch off)'''
|
|
|
|
|
|
|
|
parsed = api_get(inp, api_key, is_image=True, num=10)
|
|
|
|
if 'items' not in parsed:
|
2010-01-17 04:24:36 +00:00
|
|
|
return 'no images found'
|
2013-09-20 21:04:58 +00:00
|
|
|
return random.choice(parsed['items'])['link']
|
2010-01-17 04:24:36 +00:00
|
|
|
|
|
|
|
|
2013-09-20 21:04:58 +00:00
|
|
|
@hook.api_key('google')
|
2010-01-17 04:24:36 +00:00
|
|
|
@hook.command('g')
|
2010-05-07 23:16:44 +00:00
|
|
|
@hook.command
|
2013-09-20 21:04:58 +00:00
|
|
|
def google(inp, api_key=None):
|
2010-01-17 04:24:36 +00:00
|
|
|
'''.g/.google <query> -- returns first google search result'''
|
|
|
|
|
2013-09-20 21:04:58 +00:00
|
|
|
parsed = api_get(inp, api_key)
|
|
|
|
if 'items' not in parsed:
|
2010-01-17 04:24:36 +00:00
|
|
|
return 'no results found'
|
|
|
|
|
2013-09-20 21:04:58 +00:00
|
|
|
out = u'{link} -- \x02{title}\x02: "{snippet}"'.format(**parsed['items'][0])
|
2010-01-17 04:24:36 +00:00
|
|
|
out = ' '.join(out.split())
|
|
|
|
|
|
|
|
if len(out) > 300:
|
|
|
|
out = out[:out.rfind(' ')] + '..."'
|
|
|
|
|
|
|
|
return out
|