-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path__init__.py
43 lines (36 loc) · 1.39 KB
/
__init__.py
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
# -*- coding: utf8 -*-
import urllib
import pipobot.lib.utils
import simplejson
from pipobot.lib.module_test import ModuleTest
from pipobot.lib.modules import SyncModule, defaultcmd
class CmdGoogle(SyncModule):
def __init__(self, bot):
desc = u"!google mot-clé : recherche le mot clé dans google"
SyncModule.__init__(self,
bot,
desc=desc,
name="google")
@defaultcmd
def answer(self, sender, message):
if message == '':
return self.desc
else:
query = urllib.urlencode({'q': message})
url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&%s' % query
search_results = urllib.urlopen(url)
json = simplejson.loads(search_results.read())
results = json['responseData']['results']
ans = ''
ans_xhtml = ''
for i in results:
ans += '\n' + i['url'] + ' --- ' + i['title']
ans_xhtml += '<br/>\n<a href="' + i['url'] + '" >' + i['title'] + '</a>'
ans_xhtml = ans_xhtml.replace("b>", "strong>")
rep = {}
rep["text"] = pipobot.lib.utils.xhtml2text(ans)
rep["xhtml"] = ans_xhtml
return rep
class GoogleTest(ModuleTest):
def test_google(self):
self.bot_answer("!google test")