forked from coldnight/pual_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_simsimi.py
110 lines (92 loc) · 3.36 KB
/
_simsimi.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
104
105
106
107
108
109
110
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#
# Copyright 2013 cold
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# Author : cold
# E-mail : [email protected]
# Date : 13/11/11 15:10:28
# Desc :
#
import config
import json
from tornadohttpclient import TornadoHTTPClient
class SimSimiTalk(object):
""" 模拟浏览器与SimSimi交流
:params http: HTTP 客户端实例
:type http: ~tornadhttpclient.TornadoHTTPClient instance
"""
def __init__(self, http = None):
self.http = http or TornadoHTTPClient()
if not http:
self.http.set_user_agent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/28.0.1500.71 Chrome/28.0.1500.71 Safari/537.36")
self.http.debug = getattr(config, "TRACE", False)
self.http.validate_cert = False
self.http.set_global_headers({"Accept-Charset": "UTF-8,*;q=0.5"})
self.url = "http://www.simsimi.com/func/req"
self.params = {"lc":"zh"}
self.ready = False
self.fetch_kwargs = {}
if config.SimSimi_Proxy:
self.fetch_kwargs.update(proxy_host = config.SimSimi_Proxy[0],
proxy_port = config.SimSimi_Proxy[1])
self._setup_cookie()
def _setup_cookie(self):
def callback(resp):
self.ready = True
self.http.get("http://www.simsimi.com", callback = callback)
def talk(self, msg, callback):
""" 聊天
:param msg: 信息
:param callback: 接收响应的回调
"""
headers = {"Referer":"http://www.simsimi.com/talk.htm",
"Accept":"application/json, text/javascript, */*; q=0.01",
"Accept-Language":"zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3",
"Content-Type":"application/json; charset=utf-8",
"X-Requested-With":"XMLHttpRequest",
}
if not msg.strip():
return callback("小的在")
params = {"msg":msg.encode("utf-8")}
params.update(self.params)
def _talk(resp):
data = {}
if resp.body:
try:
data = json.loads(resp.body)
except ValueError:
pass
callback(data.get("response", "Server respond nothing!"))
self.http.get(self.url, params, headers = headers,
callback = _talk)
if __name__ == "__main__":
import threading,time
simsimi = SimSimiTalk()
def callback(response):
print response
simsimi.http.stop()
def talk():
while 1:
if simsimi.ready:
simsimi.talk("nice to meet you", callback)
break
else:
time.sleep(1)
t = threading.Thread(target = talk)
t.setDaemon(True)
t.start()
simsimi.http.start()