Browse code

Merge branch 'pr/20'

Pierre-David BeĢlanger authored on 21/09/2015 19:36:14
Showing 1 changed files
... ...
@@ -1,7 +1,17 @@
1
+import re
2
+import sys
1 3
 import hashlib
2
-import urllib
3
-import urllib2
4
-import cookielib
4
+
5
+if sys.version_info >= (3, 0):
6
+    from urllib.request import build_opener, HTTPCookieProcessor, urlopen
7
+    from urllib.parse import urlencode
8
+    import http.cookiejar as cookielib
9
+
10
+else:
11
+    from urllib import urlencode, urlopen
12
+    from urllib2 import build_opener, HTTPCookieProcessor
13
+    import cookielib
14
+
5 15
 import uuid
6 16
 import xml.dom.minidom
7 17
 
... ...
@@ -91,18 +101,18 @@ class _CleverbotSession(ChatterBotSession):
91 101
         self.vars['islearning'] = '1'
92 102
         self.vars['cleanslate'] = 'false'
93 103
         self.cookieJar = cookielib.CookieJar()
94
-        self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cookieJar))
104
+        self.opener = build_opener(HTTPCookieProcessor(self.cookieJar))
95 105
         self.opener.open(self.bot.baseUrl)
96 106
 
97 107
     def think_thought(self, thought):
98 108
         self.vars['stimulus'] = thought.text
99
-        data = urllib.urlencode(self.vars)
109
+        data = urlencode(self.vars)
100 110
         data_to_digest = data[9:self.bot.endIndex]
101
-        data_digest = hashlib.md5(data_to_digest).hexdigest()
111
+        data_digest = hashlib.md5(data_to_digest.encode('utf-8')).hexdigest()
102 112
         data = data + '&icognocheck=' + data_digest
103
-        url_response = self.opener.open(self.bot.serviceUrl, data)
104
-        response = url_response.read()
105
-        response_values = response.split('\r')
113
+        url_response = self.opener.open(self.bot.serviceUrl, data.encode('utf-8'))
114
+        response = str(url_response.read())
115
+        response_values = re.split(r'\\r|\r', response)
106 116
         #self.vars['??'] = _utils_string_at_index(response_values, 0)
107 117
         self.vars['sessionid'] = _utils_string_at_index(response_values, 1)
108 118
         self.vars['logurl'] = _utils_string_at_index(response_values, 2)
... ...
@@ -152,8 +162,8 @@ class _PandorabotsSession(ChatterBotSession):
152 162
 
153 163
     def think_thought(self, thought):
154 164
         self.vars['input'] = thought.text
155
-        data = urllib.urlencode(self.vars)
156
-        url_response = urllib2.urlopen('http://www.pandorabots.com/pandora/talk-xml', data)
165
+        data = urlencode(self.vars)
166
+        url_response = urlopen('http://www.pandorabots.com/pandora/talk-xml', data)
157 167
         response = url_response.read()
158 168
         response_dom = xml.dom.minidom.parseString(response)
159 169
         response_thought = ChatterBotThought()
... ...
@@ -177,4 +187,4 @@ def _utils_string_at_index(strings, index):
177 187
     if len(strings) > index:
178 188
         return strings[index]
179 189
     else:
180
-        return ''
190
+        return ''
181 191
\ No newline at end of file