diff --git a/Cleverbot/cleverbot.py b/Cleverbot/cleverbot.py index 0ecf7fd..7b3b089 100644 --- a/Cleverbot/cleverbot.py +++ b/Cleverbot/cleverbot.py @@ -31,10 +31,27 @@ Example of how to use the bindings: """ -import urllib2 import hashlib +import sys 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): pass @@ -59,12 +76,14 @@ class Session(object): def Send(self): data=encode(self.keylist,self.arglist) 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 data=encode(self.keylist,self.arglist) - req=urllib2.Request("http://www.cleverbot.com/webservicemin",data,self.headers) - f=urllib2.urlopen(req, timeout=9) #Needed to prevent supybot errors + req=Request("http://www.cleverbot.com/webservicemin",b(data),self.headers) + f=urlopen(req, timeout=9) #Needed to prevent supybot errors reply=f.read() + if sys.version_info[0] >= 3: + reply = reply.decode() return reply def Ask(self,q): @@ -73,7 +92,7 @@ class Session(object): asw=self.Send() self.MsgList.append(q) answer = parseAnswers(asw) - for k,v in answer.iteritems(): + for k,v in answer.items(): try: self.arglist[self.keylist.index(k)] = v except ValueError: @@ -124,11 +143,14 @@ def main(): q = '' while q != 'bye': try: - q = raw_input("> ") + if sys.version_info[0] < 3: + q = raw_input("> ") + else: + q = input("> ") except KeyboardInterrupt: - print + print() sys.exit() - print cb.Ask(q) + print(cb.Ask(q)) if __name__ == "__main__": main() diff --git a/Cleverbot/plugin.py b/Cleverbot/plugin.py index 60791bd..4eff608 100644 --- a/Cleverbot/plugin.py +++ b/Cleverbot/plugin.py @@ -20,9 +20,14 @@ from supybot.commands import * import supybot.plugins as plugins import supybot.ircutils as ircutils import supybot.callbacks as callbacks -import cleverbot -import re, random, time -from htmlentitydefs import name2codepoint +import re, random, time, sys + +if sys.version_info[0] >= 3: + from html.entities import name2codepoint +else: + from htmlentitydefs import name2codepoint + +from . import cleverbot class Cleverbot(callbacks.Plugin): """This plugin replies using the Cleverbot API upon intercepting an invalid command.""" @@ -52,10 +57,7 @@ class Cleverbot(callbacks.Plugin): @classmethod def _post(cls,bot,hash,line,sess): - try: - m = sess.Ask(line) - except: - return None + m = sess.Ask(line) if m: return m return None @@ -101,7 +103,7 @@ class Cleverbot(callbacks.Plugin): if reply is not None: self.log.debug("Reply is: "+str(reply)) if self.registryValue('enable', channel): - irc.reply(reply) + irc.reply(reply) else: 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]): @@ -112,7 +114,7 @@ class Cleverbot(callbacks.Plugin): if reply is not None: self.log.debug("Reply is: "+str(reply)) if self.registryValue('enable', channel): - irc.reply(reply) + irc.reply(reply) 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)