From 070ede1ae77a89728341f7d084e882e04ffd2d2a Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Mon, 24 May 2010 21:55:15 +0200 Subject: Added a simple google web search module. --- modules/google_search.py | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 modules/google_search.py diff --git a/modules/google_search.py b/modules/google_search.py new file mode 100644 index 0000000..625cdf2 --- /dev/null +++ b/modules/google_search.py @@ -0,0 +1,49 @@ +info = { + 'author': 'Jon Bergli Heier', + 'title': 'Google Search', + 'description': 'Google Search', +} + +import urllib, urllib2, simplejson + +class Module: + def __init__(self, bot): + self.irc = bot + + 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 __call__(self, nick, channel, msg): + if msg.startswith('!g'): + target = channel if not channel == self.irc.nickname else nick.split('!')[0] + args = msg.split() + if len(args) == 1: + self.irc.msg(target, 'Usage: !g search') + return + results = self.search(' '.join(args[1:])) + 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.' -- cgit v1.2.3