Cleverbot: Add Python 3 support.

master
Valentin Lorentz 2013-06-16 11:05:02 +02:00
parent bd1367ee91
commit b27a11f947
2 changed files with 41 additions and 17 deletions

View File

@ -31,10 +31,27 @@ Example of how to use the bindings:
""" """
import urllib2
import hashlib import hashlib
import sys
import re import re
if sys.version_info[0] >= 3:
import urllib
Request = urllib.request.Request
urlopen = urllib.request.urlopen
def u(s):
return s
def b(s):
return s.encode('utf-8')
else:
import urllib2
from urllib import urlencode
Request = urllib2.Request
def u(s):
return unicode(s, "unicode_escape")
def b(s):
return s
class ServerFullError(Exception): class ServerFullError(Exception):
pass pass
@ -59,12 +76,14 @@ class Session(object):
def Send(self): def Send(self):
data=encode(self.keylist,self.arglist) data=encode(self.keylist,self.arglist)
digest_txt=data[9:29] digest_txt=data[9:29]
hash=hashlib.md5(digest_txt).hexdigest() hash=hashlib.md5(b(digest_txt)).hexdigest()
self.arglist[self.keylist.index('icognocheck')]=hash self.arglist[self.keylist.index('icognocheck')]=hash
data=encode(self.keylist,self.arglist) data=encode(self.keylist,self.arglist)
req=urllib2.Request("http://www.cleverbot.com/webservicemin",data,self.headers) req=Request("http://www.cleverbot.com/webservicemin",b(data),self.headers)
f=urllib2.urlopen(req, timeout=9) #Needed to prevent supybot errors f=urlopen(req, timeout=9) #Needed to prevent supybot errors
reply=f.read() reply=f.read()
if sys.version_info[0] >= 3:
reply = reply.decode()
return reply return reply
def Ask(self,q): def Ask(self,q):
@ -73,7 +92,7 @@ class Session(object):
asw=self.Send() asw=self.Send()
self.MsgList.append(q) self.MsgList.append(q)
answer = parseAnswers(asw) answer = parseAnswers(asw)
for k,v in answer.iteritems(): for k,v in answer.items():
try: try:
self.arglist[self.keylist.index(k)] = v self.arglist[self.keylist.index(k)] = v
except ValueError: except ValueError:
@ -124,11 +143,14 @@ def main():
q = '' q = ''
while q != 'bye': while q != 'bye':
try: try:
q = raw_input("> ") if sys.version_info[0] < 3:
q = raw_input("> ")
else:
q = input("> ")
except KeyboardInterrupt: except KeyboardInterrupt:
print print()
sys.exit() sys.exit()
print cb.Ask(q) print(cb.Ask(q))
if __name__ == "__main__": if __name__ == "__main__":
main() main()

View File

@ -20,9 +20,14 @@ from supybot.commands import *
import supybot.plugins as plugins import supybot.plugins as plugins
import supybot.ircutils as ircutils import supybot.ircutils as ircutils
import supybot.callbacks as callbacks import supybot.callbacks as callbacks
import cleverbot import re, random, time, sys
import re, random, time
from htmlentitydefs import name2codepoint if sys.version_info[0] >= 3:
from html.entities import name2codepoint
else:
from htmlentitydefs import name2codepoint
from . import cleverbot
class Cleverbot(callbacks.Plugin): class Cleverbot(callbacks.Plugin):
"""This plugin replies using the Cleverbot API upon intercepting an invalid command.""" """This plugin replies using the Cleverbot API upon intercepting an invalid command."""
@ -52,10 +57,7 @@ class Cleverbot(callbacks.Plugin):
@classmethod @classmethod
def _post(cls,bot,hash,line,sess): def _post(cls,bot,hash,line,sess):
try: m = sess.Ask(line)
m = sess.Ask(line)
except:
return None
if m: if m:
return m return m
return None return None
@ -101,7 +103,7 @@ class Cleverbot(callbacks.Plugin):
if reply is not None: if reply is not None:
self.log.debug("Reply is: "+str(reply)) self.log.debug("Reply is: "+str(reply))
if self.registryValue('enable', channel): if self.registryValue('enable', channel):
irc.reply(reply) irc.reply(reply)
else: else:
irc.reply("My AI is down, sorry! :( I couldn't process what you said... blame it on a brain fart. :P") irc.reply("My AI is down, sorry! :( I couldn't process what you said... blame it on a brain fart. :P")
elif (msg.args[0] == irc.nick) and self.registryValue('reactprivate',msg.args[0]): elif (msg.args[0] == irc.nick) and self.registryValue('reactprivate',msg.args[0]):
@ -112,7 +114,7 @@ class Cleverbot(callbacks.Plugin):
if reply is not None: if reply is not None:
self.log.debug("Reply is: "+str(reply)) self.log.debug("Reply is: "+str(reply))
if self.registryValue('enable', channel): if self.registryValue('enable', channel):
irc.reply(reply) irc.reply(reply)
else: else:
irc.reply("My AI is down, sorry! :( I couldn't process what you said... blame it on a brain fart. :P", err, None, True, None, None) irc.reply("My AI is down, sorry! :( I couldn't process what you said... blame it on a brain fart. :P", err, None, True, None, None)