-
Notifications
You must be signed in to change notification settings - Fork 2
/
cleverwrap.py
103 lines (85 loc) · 3.71 KB
/
cleverwrap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"""
CleverWrap.py
Python wrapper for Cleverbot's API.
http://www.cleverbot.com/api
Copyright 2017 Andrew Edwards
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import requests
from threading import RLock
class CleverWrap:
""" A simple wrapper class for the www.cleverbot.com api. """
url = "https://www.cleverbot.com/getreply"
def __init__(self, api_key, name="CleverBot"):
""" Initialize the class with an api key and optional name
:type name: string
:type api_key: string
:type history: dict or maybe a list
:type convo_id: string
:type cs: string
:type count: int
:type time_elapsed: int
:type time_taken: int
:type output: string
"""
self.name = name
self.key = api_key
self.history = {}
self.convo_id = ""
self.cs = ""
self.count = 0
self.time_elapsed = 0
self.time_taken = 0
self.output = ""
self.lock = RLock()
def say(self, text):
with self.lock:
return self._say(text)
def _say(self, text):
"""
Say something to www.cleverbot.com
:type text: string
Returns: string
"""
params = {
"input": text,
"key": self.key,
"cs": self.cs,
"conversation_id": self.convo_id,
"wrapper": "CleverWrap.py"
}
reply = self._send(params)
self._process_reply(reply)
return self.output
def _send(self, params):
"""
Make the request to www.cleverbot.com
:type params: dict
Returns: dict
"""
for attempt in range(3):
# Get a response
try:
r = requests.get(self.url, params=params, timeout=20)
return r.json(strict=False) # Ignore possible control codes in returned data
# catch errors, print then exit.
except requests.exceptions.RequestException as e:
print(e)
def _process_reply(self, reply):
""" take the cleverbot.com response and populate properties. """
self.cs = reply.get("cs", None)
self.count = int(reply.get("interaction_count", None))
self.output = reply.get("output", None)
self.convo_id = reply.get("conversation_id", None)
self.history = {key:value for key, value in reply.items() if key.startswith("interaction")}
self.time_taken = int(reply.get("time_taken", None))
self.time_elapsed = int(reply.get("time_elapsed", None))
def reset(self):
"""
Drop values for self.cs and self.conversation_id
this will start a new conversation with the bot.
"""
self.cs = ""
self.convo_id = ""