forked from coldnight/pual_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.py
More file actions
332 lines (283 loc) · 11.7 KB
/
command.py
File metadata and controls
332 lines (283 loc) · 11.7 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/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 : wh_linux@126.com
# Date : 13/04/19 13:57:17
# Desc : 命令
#
import re
import gzip
import json
import urllib2
import httplib
import logging
import traceback
from functools import partial
from cStringIO import StringIO
from lxml import etree
from http_stream import HTTPStream, Form
from config import YOUDAO_KEY, YOUDAO_KEYFROM, MAX_LENGTH, SimSimi_Proxy
def upload_file(filename, path):
""" 上传文件
- `path` 文件路径
"""
form = Form()
filename = filename.encode("utf-8")
form.add_file(fieldname='img', filename=filename,
fileHandle=open(path))
req = urllib2.Request("http://paste.linuxzen.com")
req.add_header("Content-Type", form.get_content_type())
req.add_header("Content-Length", len(str(form)))
req.add_data(str(form))
return urllib2.urlopen(req)
black_words = [u"免费", u"微信", u"微 信", u"泡妞", u"会员", u"功能", u"体验",
u"技巧", u"必看", u"必学", u"加我", u"搜索", u"新型"]
def is_black_msg(content):
coe = 0
for bw in black_words:
if bw in content:
coe += 1
if coe >= 2:
return True
class Command(object):
http_stream = HTTPStream.instance()
_sim_try = {}
simsimi_proxy = False
def url_info(self, url, callback, isredirect = False):
""" 获取url信息
Arguments:
`url` - 链接
`callback` - 发送消息的回调
`isredirect` - 是否是重定向
"""
_url_info = partial(self._url_info, callback = callback, url = url,
isredirect = isredirect)
_eurl_info = partial(self._eurl_info, callback = callback, url = url)
self.http_stream.get(url, readback = _url_info, errorback = _eurl_info)
def _url_info(self, resp, callback, url, isredirect = False):
""" 读取url_info的回调 """
meta_charset = re.compile(br'<meta\s+http-equiv="?content-type"?'
'\s+content="?[^;]+;\s*charset=([^">]+'
')"?\s*/?>|<meta\s+charset="?([^">/"]+'
')"?\s*/?>', re.IGNORECASE)
body = ""
content = resp.read()
c_type = resp.headers.get("Context-Type", "text/html")
if resp.code in [200]:
if c_type == "text/html":
charset = meta_charset.findall(content)
logging.info("Found charset {0!r} in url {1}".format(charset, url))
if len(charset) == 1 and len(charset[0]) == 2:
charset = charset[0][0] if charset[0][0] else charset[0][1]
else:
charset = ""
if charset.lower().strip() == "gb2312":
charset = "gbk"
if charset:
ucont = content.lower().decode(charset).encode("utf-8").decode("utf-8")
else:
ucont = content.lower().decode("utf-8")
parser = etree.HTML(ucont)
title = parser.xpath(u"//title")
title = title[0].text if len(title) >= 1 else None
if title:
body += u"网页标题: "+title.replace("\r", "").replace("\n", "")
if isredirect:
body += u"(重定向到:{0})".format(url)
elif resp.code in [302, 301]:
dst = resp.headers.get("Location")
self.url_info(dst, callback, True)
else:
body = u"({0} {1} {2})".format(url, resp.code,
httplib.responses[resp.code])
if body:
callback(body)
def _eurl_info(self, errcode, errmsg, url, callback):
""" 处理url_info错误 """
body = u"({0} {1})".format(url, errmsg)
callback(body)
def py(self, code, callback):
""" 执行Python代码
Arguments:
`code` - 要执行的代码
`callback` - 发送消息的回调
"""
url = "http://pythonec.appspot.com/run"
#url = "http://localhost:8080/run"
params = [("code", code.encode("utf-8"))]
read_py = partial(self.read_py, callback = callback)
self.http_stream.post(url, params, readback = read_py)
def read_py(self, resp, callback):
""" 读取执行Python代码的返回 """
data = resp.read()
try:
result = json.loads(data)
status = result.get("status")
if status:
content = u"OK: " + result.get("out")
else:
content = u"ERR: " + result.get("err")
except ValueError:
logging.warn(traceback.format_exc())
content = u"我出错了, 没办法执行, 我正在改"
callback(content)
def shell(self, session, statement, callback):
""" 实现Python Shell
Arguments:
`session` - 区别用户的shell
`statement` - Python语句
`callback` - 发送结果的回调
"""
if statement.strip() in ["cls", "clear"]:
url = "http://pythonec.appspot.com/drop"
params = [("session", session),]
else:
url = "http://pythonec.appspot.com/shell"
#url = "http://localhost:8080/shell"
params = [("session", session),
("statement", statement.encode("utf-8"))]
def read_shell(resp, callback):
data = resp.read()
if not data:
data = "OK"
callback(data.decode("utf-8"))
return
read_back = partial(read_shell, callback = callback)
self.http_stream.get(url, params, readback = read_back)
def paste(self, code, callback, typ = "text"):
""" 贴代码 """
url = "http://paste.linuxzen.com"
params = [("class", typ), ("code", code.encode("utf-8")), ("paste", "ff")]
read_back = partial(self.read_paste, oldurl = url, callback = callback)
self.http_stream.post(url, params, readback = read_back)
def read_paste(self, resp, oldurl, callback):
""" 读取贴代码结果, 并发送消息 """
if resp.code == 302:
url = resp.headers.get("Location")
else:
url = resp.url
if url != oldurl:
content = url
callback(content)
def teach(self, say, response):
url = "http://paste.linuxzen.com/bot/teach"
params = (("say", say.encode("utf-8")), ("res", response.encode("utf-8")))
logging.info(u"Teach our bot {0}/{1}".format(say, response))
self.http_stream.get(url, params)
def simsimi(self, content, callback):
""" simsimi 小黄鸡 """
msg_url = "http://www.simsimi.com/func/req"
msg_params = (("msg", content.encode("utf-8")), ("lc", "ch"))
headers = {"Referer": "http://www.simsimi.com/talk.htm?lc=ch",
"X-Requested-With": "XMLHttpRequest"}
def read_simsimi(resp):
result = resp.read()
if result:
try:
response = json.loads(result)
res = response.get("response")
if is_black_msg(res):
return self.simsimi(content, callback)
if not res or (res and res.startswith("Unauthorized access!.")):
if not self._sim_try.has_key(content):
self._sim_try[content] = 0
if self._sim_try.get(content) < 10:
logging.warn("SimSimi error with response {0}".format(res))
self._sim_try[content] += 1
self.simsimi(content, callback)
else:
self._sim_try[content] = 0
callback(u"T^T ip被SimSimi封了, 无法应答")
return
else:
self._sim_try[content] = 0
callback(res)
self.teach(content, res)
except ValueError:
logging.warn(traceback.format_exc())
logging.warn("SimSimi error with response {0}".format(result))
self.simsimi(content, callback)
kw = {"headers":headers, "readback":read_simsimi}
if SimSimi_Proxy:
kw.update(proxy=SimSimi_Proxy)
self.http_stream.get(msg_url, msg_params, **kw)
def cetr(self, source, callback, web = False):
""" 调用有道接口进行英汉互译 """
key = YOUDAO_KEY
keyfrom = YOUDAO_KEYFROM
source = source.encode("utf-8")
url = "http://fanyi.youdao.com/openapi.do"
params = [("keyfrom", keyfrom), ("key", key),("type", "data"),
("doctype", "json"), ("version",1.1), ("q", source)]
read_back = partial(self.read_cetr, callback = callback, web = web)
self.http_stream.get(url, params, readback =read_back)
def read_cetr(self, resp, callback, web):
""" 读取英汉翻译的结果 """
source = resp.read()
body = None
try:
buf = StringIO(source)
with gzip.GzipFile(mode = "rb", fileobj = buf) as gf:
data = gf.read()
except:
logging.warn(traceback.format_exc())
data = source
try:
result = json.loads(data)
except ValueError:
logging.warn(traceback.format_exc())
body = u"error"
else:
errorCode = result.get("errorCode")
if errorCode == 0:
query = result.get("query")
r = " ".join(result.get("translation"))
basic = result.get("basic", {})
body = u"{0}\n{1}".format(query, r)
phonetic = basic.get("phonetic")
if phonetic:
ps = phonetic.split(",")
if len(ps) == 2:
pstr = u"读音: 英 [{0}] 美 [{1}]".format(*ps)
else:
pstr = u"读音: {0}".format(*ps)
body += u"\n" + pstr
exp = basic.get("explains")
if exp:
body += u"\n其他释义:\n\t{0}".format(u"\n\t".join(exp))
if web:
body += u"\n网络释义:\n"
web = result.get("web", [])
if web:
for w in web:
body += u"\t{0}\n".format(w.get("key"))
vs = u"\n\t\t".join(w.get("value"))
body += u"\t\t{0}\n".format(vs)
if errorCode == 50:
body = u"无效的有道key"
if not body:
body = u"没有结果"
callback(body)
def send_msg(self, msg, callback, nick = None):
if len(msg) <= MAX_LENGTH:
body = nick + msg if nick else msg
callback(body)
else:
callback = partial(self.send_msg, callback = callback, nick = nick)
self.paste(msg, callback)