info = { 'author': 'Jon Bergli Heier', 'title': 'Google Search', 'description': 'Google Search', } import urllib, urllib2, simplejson class Module: def __init__(self, bot): self.irc = bot self.irc.register_keyword('!g', self) def search(self, s): try: url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&%s' % urllib.urlencode({'q': s}) u = urllib2.urlopen(url) except URLError as e: print 'error', e, str(e) raise data = simplejson.loads(u.read()) response = data['responseData'] if not data['responseStatus'] == 200 or not len(response['results']): return return (response['results'][0]['titleNoFormatting'], response['results'][0]['url']) def keyword(self, nick, channel, kw, msg): target = channel if not channel == self.irc.nickname else nick.split('!')[0] args = msg.split() if len(args) == 0: self.irc.msg(target, 'Usage: !g search') return results = self.search(' '.join(args)) if results: results = '\002%s\002 %s' % results self.irc.msg(target, results.encode('utf-8')) else: self.irc.msg(target, 'No results.') if __name__ == '__main__': import sys m = Module(None) search = ' '.join(sys.argv[1:]) results = m.search(search) if results: print '%s: %s' % results else: print 'No results.'