1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
info = {
'author': 'Jon Bergli Heier',
'title': 'Google Search',
'description': 'Google Search',
}
import urllib, urllib2, json
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 = json.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.'
|