Advertisement
flypip

ClevertBot

Oct 6th, 2013
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.29 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. """---Flypip---"""
  4.  
  5. import urllib2
  6. import hashlib
  7. import re
  8.  
  9. class ServerFullError(Exception):
  10.         pass
  11.  
  12. ReplyFlagsRE = re.compile('<INPUT NAME=(.+?) TYPE=(.+?) VALUE="(.*?)">', re.IGNORECASE | re.MULTILINE)
  13.  
  14. class Session(object):
  15.         keylist=['stimulus','start','sessionid','vText8','vText7','vText6','vText5','vText4','vText3','vText2','icognoid','icognocheck','prevref','emotionaloutput','emotionalhistory','asbotname','ttsvoice','typing','lineref','fno','sub','islearning','cleanslate']
  16.         headers={}
  17.         headers['User-Agent']='Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20100101 Firefox/7.0'
  18.         headers['Accept']='text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
  19.         headers['Accept-Language']='en-us;q=0.8,en;q=0.5'
  20.         headers['X-Moz']='prefetch'
  21.         headers['Accept-Charset']='ISO-8859-1,utf-8;q=0.7,*;q=0.7'
  22.         headers['Referer']='http://www.cleverbot.com'
  23.         headers['Cache-Control']='no-cache, no-cache'
  24.         headers['Pragma']='no-cache'
  25.  
  26.         def __init__(self):
  27.                 self.arglist=['','y','','','','','','','','','wsf','','','','','','','','','0','Say','1','false']
  28.                 self.MsgList=[]
  29.  
  30.         def Send(self):
  31.                 data=encode(self.keylist,self.arglist)
  32.                 digest_txt=data[9:29]
  33.                 hash=hashlib.md5(digest_txt).hexdigest()
  34.                 self.arglist[self.keylist.index('icognocheck')]=hash
  35.                 data=encode(self.keylist,self.arglist)
  36.                 req=urllib2.Request("http://www.cleverbot.com/webservicemin",data,self.headers)
  37.                 f=urllib2.urlopen(req)
  38.                 reply=f.read()
  39.                 return reply
  40.  
  41.         def Ask(self,q):
  42.                 self.arglist[self.keylist.index('stimulus')]=q
  43.                 if self.MsgList: self.arglist[self.keylist.index('lineref')]='!0'+str(len(self.MsgList)/2)
  44.                 asw=self.Send()
  45.                 self.MsgList.append(q)
  46.                 answer = parseAnswers(asw)
  47.                 for k,v in answer.iteritems():
  48.                         try:
  49.                                 self.arglist[self.keylist.index(k)] = v
  50.                         except ValueError:
  51.                                 pass
  52.                 self.arglist[self.keylist.index('emotionaloutput')]=''
  53.                 text = answer['ttsText']
  54.                 self.MsgList.append(text)
  55.                 return text
  56.  
  57. def parseAnswers(text):
  58.         d = {}
  59.         keys = ["text", "sessionid", "logurl", "vText8", "vText7", "vText6", "vText5", "vText4", "vText3",
  60.                         "vText2", "prevref", "foo", "emotionalhistory", "ttsLocMP3", "ttsLocTXT",
  61.                         "ttsLocTXT3", "ttsText", "lineRef", "lineURL", "linePOST", "lineChoices",
  62.                         "lineChoicesAbbrev", "typingData", "divert"]
  63.         values = text.split("\r")
  64.         i = 0
  65.         for key in keys:
  66.                 d[key] = values[i]
  67.                 i += 1
  68.         return d
  69.  
  70. def encode(keylist,arglist):
  71.         text=''
  72.         for i in range(len(keylist)):
  73.                 k=keylist[i]; v=quote(arglist[i])
  74.                 text+='&'+k+'='+v
  75.         text=text[1:]
  76.         return text
  77.  
  78. always_safe = ('ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  79.                'abcdefghijklmnopqrstuvwxyz'
  80.                '0123456789' '_.-')
  81. def quote(s, safe = '/'): #  quote('abc def') -> 'abc%20def'
  82.         try:
  83.                 safe += always_safe
  84.                 safe_map = {}
  85.                 for i in range(256):
  86.                         c = chr(i)
  87.                         safe_map[c] = (c in safe) and c or  ('%%%02X' % i)
  88.                 res = map(safe_map.__getitem__, s)
  89.                 return ''.join(res)
  90.         except KeyError:
  91.                 return 'fail'
  92.  
  93.  
  94. def main():
  95.         import sys
  96.         cb = Session()
  97.  
  98.         q = ''
  99.         print "flybot: on"
  100.         while True:
  101.                 try:
  102.                         q = raw_input("> ")
  103.                 except KeyboardInterrupt:
  104.                         print "fatal error"
  105.                         sys.exit()
  106.                 if q != 'que vois tu quand tu ferme les yeux':
  107.                         print cb.Ask(q).decode("iso-8859-1")
  108.                 else:
  109.                         sys.exit()
  110.  
  111. if __name__ == "__main__":
  112.         main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement