-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
61 lines (49 loc) · 1.75 KB
/
main.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
# -*- encoding:utf-8 -*-
import ctypes
import os, sys
import json
from os.path import join
from gevent import monkey, pywsgi
monkey.patch_all() # 打上猴子补丁
from Ptilopsis import Bot, Classify, Event, PluginManager
from Ptilopsis.util import log_output
from flask import Flask, request, Response, make_response, send_file
app = Flask(__name__)
main_dir = os.getcwd()
# 根目录
@app.route('/', methods=['POST', 'GET'])
def bot():
data = request.data.decode('utf-8')
data_json = json.loads(data)
# 把区获取到的数据转为JSON格式。
event = Classify(data_json).result()
result = Manager.plugin_event(event, bot)
return result
@app.route("/data/<path:imageId>", methods=['POST', 'GET'])
def plugin_image(imageId):
try:
response = make_response(send_file(join(main_dir, "data", imageId)))
response.headers["Content-Type"] = "image/jpeg"
return response
except BaseException as error:
return {"code": '503', "data": str(error), "message": "图片不存在"}
def server_start(mode="", host="127.0.0.1", port=5900):
if mode == "debug":
app.run(debug=True)
log_output("测试环境")
else:
try:
server = pywsgi.WSGIServer((host, port), app, log=None)
log_output("post服务器已启动:http://" + host + ":" + str(port))
server.serve_forever()
except OSError:
log_output("端口被占用,请修改端口", "WARNING")
input("回车关闭")
if __name__ == "__main__":
version = "version:0.0.7(7)"
log_output(version)
ctypes.windll.kernel32.SetConsoleTitleW("Ptilopsis " + version)
Manager = PluginManager()
Manager.plugin_registered()
bot = Bot("127.0.0.1", 5700)
server_start()