Google: add --snippet option to lucky command, which shows the text snippet for the page.

This commit is contained in:
Daniel Folkinshteyn 2010-07-14 15:56:48 -04:00 committed by Valentin Lorentz
parent 5395000e76
commit 33c6eabf82

View File

@ -163,18 +163,27 @@ class Google(callbacks.PluginRegexp):
return format('; '.join(results)) return format('; '.join(results))
@internationalizeDocstring @internationalizeDocstring
def lucky(self, irc, msg, args, text): def lucky(self, irc, msg, args, opts, text):
"""<search> """[--snippet] <search>
Does a google search, but only returns the first result. Does a google search, but only returns the first result.
If option --snippet is given, returns also the page text snippet.
""" """
opts = dict(opts)
data = self.search(text, msg.args[0], {'smallsearch': True}) data = self.search(text, msg.args[0], {'smallsearch': True})
if data['responseData']['results']: if data['responseData']['results']:
url = data['responseData']['results'][0]['unescapedUrl'] url = data['responseData']['results'][0]['unescapedUrl']
irc.reply(url.encode('utf-8')) if opts.has_key('snippet'):
snippet = " | " + data['responseData']['results'][0]['content']
snippet = snippet.replace('<b>', '')
snippet = snippet.replace('</b>', '')
else:
snippet = ""
result = url + snippet
irc.reply(result.encode('utf-8'))
else: else:
irc.reply(_('Google found nothing.')) irc.reply(_('Google found nothing.'))
lucky = wrap(lucky, ['text']) lucky = wrap(lucky, [getopts({'snippet':'',}), 'text'])
@internationalizeDocstring @internationalizeDocstring
def google(self, irc, msg, args, optlist, text): def google(self, irc, msg, args, optlist, text):