-
Notifications
You must be signed in to change notification settings - Fork 1
/
web.py
141 lines (128 loc) · 4.47 KB
/
web.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
import tornado.ioloop
import tornado.web
import time
import json
import requests
import arrow
import logging
import os
import base64
from logging.handlers import TimedRotatingFileHandler
import gs
from tornado.web import StaticFileHandler
if not os.path.isdir("./log"):
os.mkdir("./log")
fn = str(arrow.now())
fn = fn.replace(":","_")
log_file_handler = TimedRotatingFileHandler(filename="./log/request.log", when="midnight", interval=1, backupCount=365)
log_file_handler.suffix = "%Y-%m-%d_%H-%M.log"
logger = logging.getLogger("request")
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s")
log_file_handler.setFormatter(formatter)
logger.addHandler(log_file_handler)
gs.logger = logger
web_all_data = {}
class BaseHandler(tornado.web.RequestHandler):
def _get(self):
pass
def get(self):
self.set_header("Access-Control-Allow-Origin", 'http://localhost:8081')
self.set_header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, DELETE, PUT')
self.set_header("Access-Control-Allow-Headers", "token, content-type, user-token")
self.set_header("Access-Control-Allow-Credentials", 'true')
ret = self._get()
self.write(json.dumps(ret))
# print("dumps : "+self.request.path)
# print(ret)
def _post(self):
pass
def post(self):
psd = json.loads(self.request.body)
user_agent = self.request.headers["User-Agent"]
ip = self.request.remote_ip
li = {}
li['uuid'] = psd.get('uuid','')
li['UA'] = user_agent
li['ip'] = ip
li['path'] = self.request.path
logger.info(json.dumps(li))
ret = self._post(psd)
self.write(json.dumps(ret))
# print("dumps:")
# print(ret)
class IndexHandler(BaseHandler):
def _post(self,psd):
ret = {}
sc = 1
# mc的意思是major config.配置是否购买会员相关的信息
mc = {}
mc["freq"] = 3000 # 请求频率
mc["sc"] = sc # 可请求的股票数量
ret['mc'] = mc
global web_all_data
text = psd['t']
datas=[]
gs.parse_sina_text(datas,text)
if len(datas)==0:
logger.error(text)
ret['warning'] = "请求数据有误,请重置软件"
else:
if sc==1:
datas = datas[:sc]
ret['warning'] = '''
免费版目前只支持一只股票。请在设置页面购买会员。<a onclick="$(\'#warnalert\').remove();ret_window_height();" href="#">忽略</a>
'''
# "免费版目前只支持一只股票"
ret['datas'] = datas
newversion = '''
新版本v1.1.2发布.<a onclick="cm.open_url(\'https://luckyhu.top/gs\');" href="#">去下载</a>或
<a onclick="$(\'#warnalert\').remove();ret_window_height();" href="#">忽略</a>
'''
if psd['v']!="1.1.2":
ret['warning'] = newversion
gs.save_today_his(web_all_data,datas)
gs.sort_ret(ret,psd.get('sort',''))
return ret
class ChartHandler(BaseHandler):
def _post(self,user_data):
global web_all_data
key = user_data['key']
if key in web_all_data:
ret = web_all_data[key]
else:
ret = {'quote':{'per':"0.00%",'name':key},'hist':[]}
return ret
class UpdateHandler(BaseHandler):
def _post(self,user_data):
global web_all_data
key = user_data['key']
ret = web_all_data.get(key,{})
ret = ret.get('his',[])
if len(ret)>0:
return ret[-1]
else:
return {"err":0}
class HotHandler(BaseHandler):
def _get(self):
global web_all_data
ret = []
for k,v in web_all_data.items():
t = {}
t['hot'] = v['hot']
t['name'] = v['quote']['name']
t['per'] = v['quote']['per']
ret.append(t)
return ret
if __name__ == "__main__":
pid = (os.getpid())
os.system("echo %d > pid.log" % pid)
application = tornado.web.Application([
(r"/stock/index", IndexHandler),
(r"/chart", ChartHandler),
(r"/update", UpdateHandler),
(r"/hot", HotHandler),
(r'/h5/(.*)', StaticFileHandler, dict(path=os.path.join(os.path.dirname(__file__), 'commu/dist/'), default_filename='index.html')),
])
application.listen(8082)
tornado.ioloop.IOLoop.current().start()