summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2010-05-24 21:55:15 +0200
committerJon Bergli Heier <snakebite@jvnv.net>2010-05-24 21:55:15 +0200
commit070ede1ae77a89728341f7d084e882e04ffd2d2a (patch)
tree53a7276788f6bfd00a392c369419b1e2246ee3cd
parentc5b0c5910f6fe6c325ce3ed80975e6d07e7c2860 (diff)
Added a simple google web search module.
-rw-r--r--modules/google_search.py49
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.'