-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.py
More file actions
612 lines (507 loc) · 19 KB
/
main.py
File metadata and controls
612 lines (507 loc) · 19 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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
import asyncio
import datetime
import os
import signal
import pickle
import uuid
import sys
import traceback
from typing import Annotated, Any, Optional
from fastapi import Depends, FastAPI, Form, HTTPException, Request
from fastapi.responses import HTMLResponse, RedirectResponse
from fastapi.security import HTTPBasic, HTTPBasicCredentials
from fastapi.openapi.docs import get_swagger_ui_html
from fastapi.openapi.utils import get_openapi
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from jinja2 import pass_context
import uvicorn
from config import settings
import logging
from data import (
CACHE_PERIOD_DAY_1,
get_fund_hold_bond,
get_fund_hold_stack,
get_fund_info,
get_fund_info_xq,
get_fund_rate,
get_index,
get_index_new,
get_rt_evaluation,
get_rt_factor,
get_fund_share_cache,
)
app = FastAPI(
title=settings.app_name,
version="2.1.2",
docs_url=None,
redoc_url=None,
openapi_url=None,
)
app.state.favour = {}
app.state.share = {}
app.state.fail_login = {}
app.state.banip = []
GUEST_ACOUNT = "guest"
templates = Jinja2Templates(directory="templates")
# 通过识别base_url中的协议,决定静态文件的协议类型
@pass_context
def urlx_for(
context: dict,
name: str,
**path_params: Any,
) -> str:
request: Request = context["request"]
http_url = request.url_for(name, **path_params)
if "https" in settings.base_url:
return http_url.replace(scheme="https")
return http_url
templates.env.globals["url_for"] = urlx_for
basic_auth = HTTPBasic()
async def basic_authorization(
credentials: Annotated[HTTPBasicCredentials, Depends(basic_auth)], request: Request
) -> str:
"""Basic Authorization"""
remote_host = request.client.host
if remote_host in app.state.banip:
logging.error(f"Login failed. Banned IP: {remote_host}")
raise HTTPException(status_code=401, detail="IP address has been blocked")
username = credentials.username
password = credentials.password
auth_users = settings.users.split(",")
for user in auth_users:
try:
tmp = user.split(":")
a_u = tmp[0]
a_p = tmp[1]
if a_u == username and a_p == password:
return username
except:
continue
logging.error(
f"Login failed. IP: {request.client.host} Username: {credentials.username}"
)
if remote_host not in app.state.fail_login:
app.state.fail_login[remote_host] = 1
else:
app.state.fail_login[remote_host] = app.state.fail_login[remote_host] + 1
logging.info(f"ban ips: {app.state.banip} watch: {app.state.fail_login}")
if app.state.fail_login[remote_host] >= 5:
app.state.banip.append(remote_host)
logging.error(f"New Banned IP: {remote_host}")
raise HTTPException(status_code=401, detail="Unauthorized user")
app.mount("/static", StaticFiles(directory="static"), name="static")
@app.get("/health")
async def health():
"""网站状态接口"""
return {"status": "ok"}
@app.get("/docs", dependencies=[Depends(basic_authorization)])
async def get_documentation():
"""文档地址"""
return get_swagger_ui_html(openapi_url="/openapi.json", title="docs")
@app.get("/openapi.json", dependencies=[Depends(basic_authorization)])
async def openapi():
"""获取openapi接口"""
return get_openapi(title=settings.app_name, version="0.1.0", routes=app.routes)
@app.get("/api/v1/index", dependencies=[Depends(basic_authorization)])
async def api_index(request: Request):
"""获取当前指数动态"""
try:
now, index = await get_index_new()
except Exception as e:
logging.error(f"get index error: {e}")
raise HTTPException(400, detail=f"get index error: {e}")
return {"time": now, "index": index}
@app.get("/api/v1/fund_info/{code}", dependencies=[Depends(basic_authorization)])
async def api_fund_info(code: str, request: Request):
"""获取基金基本信息"""
try:
fund_info = await get_fund_info(code)
except Exception as e:
traceback.print_exc()
logging.error(f"get fund info error: {e}")
raise HTTPException(400, detail=f"get fund info error: {e}")
return fund_info
@app.get("/api/v1/realtime", dependencies=[Depends(basic_authorization)])
async def api_rt(request: Request):
"""获取股票/债券实时价格"""
try:
now, a_stocks, h_stocks, m_stocks, bond_index = await get_rt_factor()
except Exception as e:
traceback.print_exc()
logging.error(f"get real time price error: {e}")
raise HTTPException(400, detail=f"get real time price error: {e}")
print(a_stocks, h_stocks, m_stocks, bond_index)
return {
"time": now,
"a_stocks": len(a_stocks),
"h_stocks": len(h_stocks),
"m_stocks": len(m_stocks),
"bond_index": bond_index,
}
@app.get("/api/v1/evaluate/{code}", dependencies=[Depends(basic_authorization)])
async def api_fund_evaluate(code: str, request: Request):
"""获取基金估值"""
try:
now, fund_info, evaluate, _ = await get_rt_evaluation(code=code)
except Exception as e:
traceback.print_exc()
logging.error(f"get real time price error: {e}")
raise HTTPException(400, detail=f"get real time price error: {e}")
ret = {
"time": now,
"code": fund_info["基金代码"],
"name": fund_info["基金名称"],
"evaluate": evaluate,
}
return ret
@app.get("/api/v1/share/{code}")
async def api_fund_share(
code: str, request: Request, username: str = Depends(basic_authorization)
):
"""获取基金估值"""
now = datetime.datetime.now()
next_day = now + datetime.timedelta(days=1)
uuid_str = str(uuid.uuid4())
app.state.share[uuid_str] = {"code": code, "ddl": next_day}
ret_msg = f"分享链接为 {settings.base_url}/share/{uuid_str} ,有效期为 24 小时。"
response = await homepage(request, username, ret_msg, None)
return response
@app.get("/", response_class=HTMLResponse)
async def homepage(
request: Request,
username: str = Depends(basic_authorization),
succ_msg: Optional[str] = None,
fail_msg: Optional[str] = None,
):
"""主页"""
async def homepage_get_index():
try:
now, index = await get_index_new()
now_str = now.strftime("%H:%M:%S")
except Exception as e:
traceback.print_exc()
logging.error(f"get index error: {e}")
raise HTTPException(400, detail="get index error")
return now_str, index
async def homepage_get_rt():
if username not in app.state.favour:
app.state.favour[username] = settings.favour_init.copy()
favour_list = app.state.favour[username]
sem = asyncio.Semaphore(5)
async def get_fund_evaluate(code):
async with sem:
try:
now, fund_info, evaluate, _ = await get_rt_evaluation(code=code)
except Exception as e:
traceback.print_exc()
logging.error(f"get fund evaluation error: {e}")
return None
fund_info["基金估值"] = evaluate
fund_info["估值时间"] = now.strftime("%H:%M:%S")
return fund_info
tasks = [get_fund_evaluate(code) for code in favour_list]
favour_funds_info_list = await asyncio.gather(*tasks)
favour_funds_info_list = [f for f in favour_funds_info_list if f is not None]
return favour_funds_info_list
get_index_ret, get_rt_ret = await asyncio.gather(
homepage_get_index(), homepage_get_rt()
)
now_str, index = get_index_ret
favour_funds_info_list = get_rt_ret
context = {
"index_now": now_str,
"index": index,
"favour_funds": favour_funds_info_list,
"version": app.version
}
if succ_msg is not None:
context["alert"] = {"type": "success", "content": succ_msg}
if fail_msg is not None:
context["alert"] = {"type": "danger", "content": fail_msg}
return templates.TemplateResponse(
request=request, name="index.html", context=context
)
@app.get("/logout", response_class=HTMLResponse)
async def logout(request: Request, username: str = Depends(basic_authorization)):
"""退出用户登陆"""
logging.info(f"logout success. user: {username} from {request.client.host}")
return templates.TemplateResponse(
request=request, name="logout.html", status_code=401, context={"version": app.version}
)
@app.get("/fund/{code}", response_class=HTMLResponse)
async def fund_info(
request: Request, code: Optional[str], username: str = Depends(basic_authorization)
):
"""处理基金请求"""
if code is None:
response = await homepage(request, username, None, "基金代码为空")
return response
if len(code) != 6 or not code.isdigit():
response = await homepage(request, username, None, "基金代码格式错误")
return response
try:
now, fund_info, evaluate, detailed_price = await get_rt_evaluation(code=code)
except Exception as e:
logging.error(f"get fund evaluation error: {e}")
response = await homepage(
request, username, None, f"获取基金{code}估值失败,请稍后重试"
)
return response
rank_list = []
score_list = []
try:
for key, value in fund_info["基金评级"].items():
rank_list.append({"评级机构": key, "评级分数": value})
score_list.append(value)
except Exception as e:
logging.error(f"get fund rank error: {e}")
for key in [
"日增长率",
"月增长率",
"三月增长率",
"六月增长率",
"年增长率",
"三年增长率",
]:
f = fund_info["历史业绩"][key]
try:
int_f = float(f) if f != "N/A" and f != None else 0
except Exception as e:
int_f = 0
fund_info["历史业绩"][key] = int_f
try:
if len(fund_info["持有股票"]) > 0:
stock_season = fund_info["持有股票"][0]["season"]
stock_show_total = sum([f["share"] for f in fund_info["持有股票"]])
else:
stock_season = None
stock_show_total = 0
except Exception as e:
logging.error(f"get fund stock error: {e}")
stock_season = None
stock_show_total = 0
try:
if len(fund_info["持有债券"]) > 0:
bond_season = fund_info["持有债券"][0]["season"]
bond_show_total = sum([f["share"] for f in fund_info["持有债券"]])
else:
bond_season = None
bond_show_total = 0
except Exception as e:
logging.error(f"get fund bond error: {e}")
bond_season = None
bond_show_total = 0
avg_score = sum(score_list) / len(score_list) if len(score_list) > 0 else "N/A"
now_str = now.strftime("%H:%M:%S")
try:
favour = 0
if (
username in app.state.favour
and code in app.state.favour[username]
and username != GUEST_ACOUNT
):
favour = 2
elif username != GUEST_ACOUNT:
favour = 1
except Exception as e:
logging.error(f"get fund favour error: {e}")
favour = 0
return templates.TemplateResponse(
request=request,
name="fund.html",
context={
"fund": fund_info,
"evaluate": evaluate,
"now": now_str,
"avg_score": avg_score,
"rank_list": rank_list,
"stock_season": stock_season,
"bond_season": bond_season,
"stock_show_total": stock_show_total,
"bond_show_total": bond_show_total,
"detailed_price": detailed_price,
"favour": favour,
"version": app.version
},
)
@app.get("/share/{uuid}")
async def fund_share(uuid: str, request: Request):
"""获取基金估值"""
now = datetime.datetime.now()
try:
if uuid not in app.state.share:
return HTMLResponse("分享链接已失效", status_code=200)
ddl: datetime = app.state.share[uuid]["ddl"]
code = app.state.share[uuid]["code"]
if ddl < now:
return HTMLResponse("分享链接已失效", status_code=200)
except Exception as e:
logging.error(f"get share link error {e}")
return HTMLResponse("分享链接已失效", status_code=200)
response = await fund_info(request, code, GUEST_ACOUNT)
return response
@app.post("/fund", response_class=HTMLResponse)
async def fund_info_post(
request: Request,
code: Optional[str] = Form(default=None),
username: str = Depends(basic_authorization),
):
"""处理基金请求"""
if code is None:
response = await homepage(request, username, None, "基金代码为空")
return response
response = await fund_info(request, code, username)
return response
@app.get("/watch/add/{code}")
async def watch_add(
request: Request, code: str, username: str = Depends(basic_authorization)
):
"""添加关注基金"""
if username not in app.state.favour:
app.state.favour[username] = settings.favour_init.copy()
if code not in app.state.favour[username]:
if len(app.state.favour[username]) >= settings.max_favour:
logging.info(f"add favour fund failed for {username}: {code}")
response = await homepage(
request, username, None, f"最多只能关注 {settings.max_favour} 个基金"
)
return response
logging.info(f"add favour fund {username}: {code}")
app.state.favour[username].append(code)
response = RedirectResponse(url=f"/fund/{code}")
return response
@app.get("/watch/del/{code}")
async def watch_del(
request: Request,
code: str,
goback: Optional[str] = None,
username: str = Depends(basic_authorization),
):
"""删除关注基金"""
if username not in app.state.favour:
app.state.favour[username] = settings.favour_init.copy()
if code in app.state.favour[username]:
logging.info(f"delete favour fund for {username}: {code}")
app.state.favour[username].remove(code)
else:
logging.info(f"delete favour fund failed for {username}: {code}")
response = await homepage(request, username, None, "未关注该基金")
return response
if goback is None:
goback = "/"
response = RedirectResponse(url=goback)
return response
async def daily_refresh():
"""每日11点定时任务,用于预缓存被关注的基金信息,并清理ban_ip"""
await asyncio.sleep(15)
logging.info("enable daily update")
while True:
now = datetime.datetime.now()
today = now.strftime("%Y-%m-%d %H:%M:%S")
logging.info(f"daily update for {today}")
try:
fund_list = []
for username in app.state.favour:
for code in app.state.favour[username]:
if code not in fund_list:
fund_list.append(code)
sem = asyncio.Semaphore(5)
async def update_one(code: str):
async with sem:
try:
future_basic = get_fund_info_xq(code=code, _cache_refresh=True)
future_rate = get_fund_rate(code)
future_hold_stock = get_fund_hold_stack(code)
future_hold_bond = get_fund_hold_bond(code)
future_share = get_fund_share_cache(code)
await asyncio.gather(
future_basic,
future_rate,
future_hold_stock,
future_hold_bond,
future_share,
)
logging.info(f"pre-reload success for fund {code}")
except Exception as e:
logging.error(f"pre-reload failed for fund {code}: {e}")
tasks = [update_one(code) for code in fund_list]
await asyncio.gather(*tasks)
except Exception as e:
logging.error(f"daily update error: {e}")
logging.info(f"fresh banned ip successful")
app.state.fail_login.clear()
app.state.banip.clear()
try:
next = now + datetime.timedelta(days=1)
next = next.replace(hour=22, minute=59, second=59)
diff_sec = (next - now).total_seconds()
except Exception as e:
logging.error(f"daily update error: {e}")
diff_sec = CACHE_PERIOD_DAY_1
logging.info(f"daily update finished, next update: {next}")
await asyncio.sleep(diff_sec)
async def auto_store():
"""定时任务,每30分钟保存应用状态"""
INIT_AUTO_STORE_DELAY = 30
AUTO_STORE_PERIOD = 1800
if settings.state_db != "":
await asyncio.sleep(INIT_AUTO_STORE_DELAY)
logging.info("enable auto store")
while True:
logging.info(f"save state to {settings.state_db}")
try:
with open(settings.state_db, "wb") as f:
state = {
"favour": app.state.favour,
"share": app.state.share,
}
pickle.dump(state, f)
except Exception as e:
logging.error(f"save state error: {e}")
await asyncio.sleep(AUTO_STORE_PERIOD)
def init_state():
"""初始化应用状态"""
if settings.state_db != "" and os.path.exists(settings.state_db):
with open(settings.state_db, "rb") as f:
state = pickle.load(f)
app.state.favour = state["favour"]
app.state.share = state["share"]
logging.info(f"load state from {settings.state_db}")
for up in settings.users.split(","):
try:
u, _ = up.split(":")
if u not in app.state.favour:
app.state.favour[u] = settings.favour_init.copy()
except:
logging.error(f"Invalid user format: {up}")
sys.exit(1)
if __name__ == "__main__":
def exit_handler(sig, frame):
sys.exit(0)
signal.signal(signal.SIGINT, exit_handler)
signal.signal(signal.SIGTERM, exit_handler)
logging.basicConfig(
level=logging.INFO,
datefmt="%Y-%m-%d %H:%M:%S",
format="%(asctime)s - %(levelname)s - %(message)s",
)
cwd = os.getcwd()
init_state()
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
config = uvicorn.Config(
app=app,
host=settings.host,
port=settings.port,
reload=True,
loop=loop,
reload_dirs=[cwd],
reload_delay=3,
forwarded_allow_ips="*",
)
server = uvicorn.Server(config=config)
srv_f = server.serve()
update_f = daily_refresh()
store_f = auto_store()
loop.run_until_complete(asyncio.gather(srv_f, update_f, store_f))