Advertisement
Guest User

Cleverbot vs. Cleverbot

a guest
Feb 2nd, 2011
5,474
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.05 KB | None | 0 0
  1. import urllib2
  2. import md5
  3. #import hashlib
  4. import re
  5.  
  6. class ServerFullError(Exception):
  7.     pass
  8.  
  9. ReplyFlagsRE = re.compile('<INPUT NAME=(.+?) TYPE=(.+?) VALUE="(.*?)">', re.IGNORECASE | re.MULTILINE)
  10.  
  11. class Session:
  12.     keylist=['stimulus','start','sessionid','vText8','vText7','vText6','vText5','vText4','vText3','vText2','icognoid','icognocheck','prevref','emotionaloutput','emotionalhistory','asbotname','ttsvoice','typing','lineref','sub','islearning','cleanslate']
  13.     headers={}
  14.     headers['User-Agent']='Mozilla/5.0 (X11; U; Linux x86_64; it; rv:1.9.1.8) Gecko/20100214 Ubuntu/9.10 (karmic) Firefox/3.5.8'
  15.     headers['Accept']='text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
  16.     headers['Accept-Language']='it-it,it;q=0.8,en-us;q=0.5,en;q=0.3'
  17.     headers['X-Moz']='prefetch'
  18.     headers['Accept-Charset']='ISO-8859-1,utf-8;q=0.7,*;q=0.7'
  19.     headers['Referer']='http://www.cleverbot.com'
  20.     headers['Cache-Control']='no-cache, no-cache'
  21.     headers['Pragma']='no-cache'
  22.  
  23.     def __init__(self):
  24.         self.arglist=['','y','','','','','','','','','wsf','','','','','','','','','Say','1','false']
  25.         self.MsgList=[]
  26.  
  27.     def Send(self):
  28.         data=encode(self.keylist,self.arglist)
  29.         digest_txt=data[9:29]
  30.         hash=md5.new(digest_txt).hexdigest()
  31.         self.arglist[self.keylist.index('icognocheck')]=hash
  32.         data=encode(self.keylist,self.arglist)
  33.         req=urllib2.Request("http://www.cleverbot.com/webservicefrm",data,self.headers)
  34.         f=urllib2.urlopen(req)
  35.         reply=f.read()
  36.         return reply
  37.  
  38.     def Ask(self,q):
  39.         self.arglist[self.keylist.index('stimulus')]=q
  40.         if self.MsgList: self.arglist[self.keylist.index('lineref')]='!0'+str(len(self.MsgList)/2)
  41.         asw=self.Send()
  42.         if '<meta name="description" content="Jabberwacky server maintenance">' in asw: raise ServerFullError, "The Cleverbot server answered with full load error"
  43.         self.MsgList.append(q)
  44.         answ_dict=GetAnswerArgs(asw)
  45.         for k in self.keylist:
  46.             if k in answ_dict: self.arglist[self.keylist.index(k)]=answ_dict[k]
  47.         self.arglist[self.keylist.index('emotionaloutput')]=''
  48.         reply_i=asw.find('<!-- Begin Response !-->')+25
  49.         reply_s=asw.find('<!-- End Response !-->')-1
  50.         text=asw[reply_i:reply_s]
  51.         self.MsgList.append(text)
  52.         return text
  53.  
  54. def GetAnswerArgs(text):
  55.     results=ReplyFlagsRE.findall(text)
  56.     list={}
  57.     for r in results: list[r[0]]=r[2]
  58.     return list
  59.  
  60. def encode(keylist,arglist):
  61.     text=''
  62.     for i in range(len(keylist)):
  63.         k=keylist[i]; v=quote(arglist[i])
  64.         text+='&'+k+'='+v
  65.     text=text[1:]
  66.     return text
  67.  
  68. always_safe = ('ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  69.                'abcdefghijklmnopqrstuvwxyz'
  70.                '0123456789' '_.-')
  71. def quote(s, safe = '/'):   #quote('abc def') -> 'abc%20def'
  72.     safe += always_safe
  73.     safe_map = {}
  74.     for i in range(256):
  75.         c = chr(i)
  76.         safe_map[c] = (c in safe) and c or  ('%%%02X' % i)
  77.     res = map(safe_map.__getitem__, s)
  78.     return ''.join(res)
  79.  
  80.  
  81. cb1 = Session()
  82. cb2 = Session()
  83.  
  84. begin = cb1.Ask("Hello")
  85. print "- " + begin
  86. it1 = cb2.Ask(begin)
  87. print "- " + it1
  88.  
  89. while True:
  90.     it2 = cb1.Ask(it1)
  91.     print "- " + it2
  92.     it1 = cb2.Ask(it2)
  93.     print "- " + it1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement