plugins/google: use custom search API instead of (deprecated) AJAX api
This commit is contained in:
parent
80548ca8f9
commit
52b90119a6
|
@ -3,49 +3,34 @@ import random
|
||||||
from util import hook, http
|
from util import hook, http
|
||||||
|
|
||||||
|
|
||||||
def api_get(kind, query):
|
def api_get(query, key, is_image=None, num=1):
|
||||||
url = 'http://ajax.googleapis.com/ajax/services/search/%s?' \
|
url = ('https://www.googleapis.com/customsearch/v1?cx=007629729846476161907:ud5nlxktgcw'
|
||||||
'v=1.0&safe=off'
|
'&fields=items(title,link,snippet)&safe=off' + ('&searchType=image' if is_image else ''))
|
||||||
return http.get_json(url % kind, q=query)
|
return http.get_json(url, key=key, q=query, num=num)
|
||||||
|
|
||||||
|
|
||||||
|
@hook.api_key('google')
|
||||||
@hook.command
|
@hook.command
|
||||||
def gis(inp):
|
def gis(inp, api_key=None):
|
||||||
'''.gis <term> -- returns first google image result (safesearch off)'''
|
'''.gis <term> -- finds an image using google images (safesearch off)'''
|
||||||
|
|
||||||
parsed = api_get('images', inp)
|
parsed = api_get(inp, api_key, is_image=True, num=10)
|
||||||
if not 200 <= parsed['responseStatus'] < 300:
|
if 'items' not in parsed:
|
||||||
raise IOError('error searching for images: %d: %s' % (
|
|
||||||
parsed['responseStatus'], ''))
|
|
||||||
if not parsed['responseData']['results']:
|
|
||||||
return 'no images found'
|
return 'no images found'
|
||||||
return random.choice(parsed['responseData']['results'][:10]) \
|
return random.choice(parsed['items'])['link']
|
||||||
['unescapedUrl'] # squares is dumb
|
|
||||||
|
|
||||||
|
|
||||||
|
@hook.api_key('google')
|
||||||
@hook.command('g')
|
@hook.command('g')
|
||||||
@hook.command
|
@hook.command
|
||||||
def google(inp):
|
def google(inp, api_key=None):
|
||||||
'''.g/.google <query> -- returns first google search result'''
|
'''.g/.google <query> -- returns first google search result'''
|
||||||
|
|
||||||
parsed = api_get('web', inp)
|
parsed = api_get(inp, api_key)
|
||||||
if not 200 <= parsed['responseStatus'] < 300:
|
if 'items' not in parsed:
|
||||||
raise IOError('error searching for pages: %d: %s' % (
|
|
||||||
parsed['responseStatus'], ''))
|
|
||||||
if not parsed['responseData']['results']:
|
|
||||||
return 'no results found'
|
return 'no results found'
|
||||||
|
|
||||||
result = parsed['responseData']['results'][0]
|
out = u'{link} -- \x02{title}\x02: "{snippet}"'.format(**parsed['items'][0])
|
||||||
|
|
||||||
title = http.unescape(result['titleNoFormatting'])
|
|
||||||
content = http.unescape(result['content'])
|
|
||||||
|
|
||||||
if len(content) == 0:
|
|
||||||
content = "No description available"
|
|
||||||
else:
|
|
||||||
content = http.html.fromstring(content).text_content()
|
|
||||||
|
|
||||||
out = '%s -- \x02%s\x02: "%s"' % (result['unescapedUrl'], title, content)
|
|
||||||
out = ' '.join(out.split())
|
out = ' '.join(out.split())
|
||||||
|
|
||||||
if len(out) > 300:
|
if len(out) > 300:
|
||||||
|
|
Loading…
Reference in New Issue