-
Notifications
You must be signed in to change notification settings - Fork 6
/
rest_console.py
160 lines (144 loc) · 5.96 KB
/
rest_console.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
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
# coding:utf-8
from flask import Flask
from console_erya.config import ip, port, debug
from flask_restful import reqparse, Api, Resource
from console_erya.global_var import globalvar
from console_erya import console
from flask_cors import *
import shutil
import os
import time
import re
r = '(?<!/)&'
app = Flask(__name__)
CORS(app, supports_credentials=True)
api = Api(app)
__author__ = 'bankroft'
__version__ = '0.34 debug'
__blog__ = 'https://www.bankroft.cn'
__web__ = 'https://www.bankroft.cn/erya-console'
class Main(Resource):
def get(self):
parser = reqparse.RequestParser()
parser.add_argument('op', type=str, location='args')
parser.add_argument('instance_name', type=str, location='args')
parser.add_argument('school_name', type=str, location='args')
# parser.add_argument('get_ver_code', type=bool, location='args')
parser.add_argument('author', type=str, location='args')
args = parser.parse_args()
con = globalvar.get(args['instance_name'])
if args['author']:
return [{'author': __author__}]
if args['op'] == 'search_school':
tmp = con.search_school(args['school_name'])
return [{'status': 100, 'message': '', 'data': tmp}]
elif args['op'] == 'get_course':
if con.status['login']:
tmp = con.get_course()
return [{'status': 100, 'message': '', 'data': tmp}]
return [{'status': 404, 'message': '参数错误'}]
def post(self):
parser = reqparse.RequestParser()
parser.add_argument('index', type=int, location='form')
parser.add_argument('instance_name', type=str, location='form')
parser.add_argument('student_num', type=str, location='form')
parser.add_argument('pwd', type=str, location='form')
parser.add_argument('ver_code', type=str, location='form')
parser.add_argument('op', type=str, location='form')
args = parser.parse_args()
con = globalvar.get(args['instance_name'])
if args['op'] == 'init':
if globalvar.get(args['instance_name']):
con.quit()
globalvar.add({args['instance_name']: console.Console()})
return [{'status': 100, 'message': ''}]
elif args['op'] == 'select_school':
if args['index'] is not None:
if con.status['search_school'] and not con.status['login']:
return [{'status': 100, 'message': '', 'data': con.select_school(args['index'])}]
else:
return [{'status': 404, 'message': '先搜索学校'}]
elif args['op'] == 'login':
if args['student_num'] and args['pwd'] and args['ver_code']:
tmp = con.login(args['student_num'], args['pwd'], args['ver_code'])
if tmp[1]:
return [{'status': 100, 'message': '', 'data': tmp[0]}]
else:
return [{'status': 404, 'message': tmp[0]}]
elif args['op'] == 'browse_watch':
if args['index'] is not None:
return [{'status': 100, 'message': '', 'data': con.browse_watch(args['index'])}]
return [{'status': 404, 'message': '参数错误'}]
class GetInfo(Resource):
def get(self):
parser = reqparse.RequestParser()
parser.add_argument('start', type=int, location='args')
parser.add_argument('status', type=str, location='args')
args = parser.parse_args()
if args['status']:
return [{'status': 100, 'message': '正在运行'}]
if args['start']:
start = int(args['start'])
data = []
shutil.copyfile('erya.log', 'erya.log.tmp')
with open('erya.log.tmp', encoding='utf-8') as f:
for x in f.readlines():
try:
t = time.mktime(time.strptime(x.split(',')[0], '%Y-%m-%d %H:%M:%S'))
if t > start:
data.append(re.split(r, x)[1:])
except:
continue
os.remove('erya.log.tmp')
return [{'status': 100, 'message': '', 'data': data}]
else:
return [{'status': 404, 'message': '参数错误'}]
class RESTExam(Resource):
ins = None
def get(self):
parser = reqparse.RequestParser()
parser.add_argument('init', type=str, location='args')
parser.add_argument('start', type=str, location='args')
args = parser.parse_args()
if args['init'] is not None:
globalvar.add({'exam': console.Exam()})
return [{'status': 100, 'message': ''}]
elif args['start'] is not None:
if globalvar.get('exam'):
globalvar.get('exam').start()
return [{'status': 100, 'message': ''}]
return [{'status': 404, 'message': '参数错误'}]
api.add_resource(Main, '/rest_console')
# 参数待定
api.add_resource(GetInfo, '/getinfo')
api.add_resource(RESTExam, '/exam')
if __name__ == '__main__':
print('='*60)
print('|{0}Author:%9s{1}|'.format(' '*20, ' '*22) % __author__)
print('|{0}Version:%10s{1}|'.format(' '*20, ' '*20) % __version__)
print('|{0}Blog:%24.24s{1}|'.format(' '*20, ' '*9) % __blog__)
print('|{0}Web:%38.38s{1}|'.format(' ' * 13, ' ' * 3) % __web__)
print('='*60)
app.run(host=ip, port=port, debug=debug)
# app.run(debug=True)
# from xmlrpc.server import SimpleXMLRPCServer
# from xmlrpc.server import SimpleXMLRPCRequestHandler
# from console_erya.console import Console
#
#
# # Restrict to a particular path.
# class RequestHandler(SimpleXMLRPCRequestHandler):
# rpc_paths = ('/RPC2',)
#
#
# from jsonrpc import JSONRPCResponseManager
#
#
# if __name__ == '__main__':
# with SimpleXMLRPCServer(("localhost", 8000),
# requestHandler=RequestHandler) as server:
#
# server.register_instance(Console())
#
# # Run the server's main loop
# server.serve_forever()