2018-06-13 09:47:21 +08:00
|
|
|
|
import falcon
|
2018-06-16 18:28:43 +08:00
|
|
|
|
from falcon import uri
|
2018-06-13 09:47:21 +08:00
|
|
|
|
from wechatpy.utils import check_signature
|
|
|
|
|
|
from wechatpy.exceptions import InvalidSignatureException
|
|
|
|
|
|
from wechatpy import parse_message
|
|
|
|
|
|
from wechatpy.replies import TextReply, ImageReply
|
|
|
|
|
|
|
2018-06-16 18:28:43 +08:00
|
|
|
|
from utils import img_download, img_upload
|
|
|
|
|
|
from face_id import access_api
|
|
|
|
|
|
|
2018-06-13 09:47:21 +08:00
|
|
|
|
|
|
|
|
|
|
class Connect(object):
|
|
|
|
|
|
|
|
|
|
|
|
def on_get(self, req, resp):
|
|
|
|
|
|
query_string = req.query_string
|
|
|
|
|
|
query_list = query_string.split('&')
|
|
|
|
|
|
b = {}
|
|
|
|
|
|
for i in query_list:
|
|
|
|
|
|
b[i.split('=')[0]] = i.split('=')[1]
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
check_signature(token='lengxiao', signature=b['signature'], timestamp=b['timestamp'], nonce=b['nonce'])
|
|
|
|
|
|
resp.body = (b['echostr'])
|
|
|
|
|
|
except InvalidSignatureException:
|
|
|
|
|
|
pass
|
|
|
|
|
|
resp.status = falcon.HTTP_200
|
|
|
|
|
|
|
|
|
|
|
|
def on_post(self, req, resp):
|
|
|
|
|
|
xml = req.stream.read()
|
|
|
|
|
|
msg = parse_message(xml)
|
|
|
|
|
|
if msg.type == 'text':
|
|
|
|
|
|
reply = TextReply(content=msg.content, message=msg)
|
|
|
|
|
|
xml = reply.render()
|
|
|
|
|
|
resp.body = (xml)
|
|
|
|
|
|
resp.status = falcon.HTTP_200
|
|
|
|
|
|
elif msg.type == 'image':
|
2018-06-16 18:28:43 +08:00
|
|
|
|
name = img_download(msg.image, msg.source) # 下载图片
|
|
|
|
|
|
print(name)
|
|
|
|
|
|
r = access_api('images/' + name)
|
|
|
|
|
|
if r == '检测成功':
|
|
|
|
|
|
media_id = img_upload('image', 'faces/' + name) # 上传图片,得到 media_id
|
|
|
|
|
|
reply = ImageReply(media_id=media_id, message=msg)
|
|
|
|
|
|
else:
|
|
|
|
|
|
reply = TextReply(content='人脸检测失败,请上传1M以下人脸清晰的照片', message=msg)
|
2018-06-13 09:47:21 +08:00
|
|
|
|
xml = reply.render()
|
|
|
|
|
|
resp.body = (xml)
|
|
|
|
|
|
resp.status = falcon.HTTP_200
|
|
|
|
|
|
|
|
|
|
|
|
app = falcon.API()
|
|
|
|
|
|
connect = Connect()
|
|
|
|
|
|
app.add_route('/connect', connect)
|