diff options
-rw-r--r-- | modules/google_search.py | 49 |
1 files changed, 49 insertions, 0 deletions
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.' |