2022-02-03 20:02:38 +08:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
# -- coding:utf-8 --
|
|
|
|
|
# @Author: markushammered@gmail.com
|
|
|
|
|
# @Development Tool: PyCharm
|
|
|
|
|
# @Create Time: 2022/2/3
|
|
|
|
|
# @File Name: app.py
|
|
|
|
|
|
|
|
|
|
|
2022-02-04 19:49:52 +08:00
|
|
|
import time
|
2022-02-03 20:02:38 +08:00
|
|
|
from flask import Flask
|
|
|
|
|
from flask import request
|
|
|
|
|
from flask import Response
|
|
|
|
|
from flask import make_response
|
|
|
|
|
from flask import send_from_directory
|
2022-02-05 16:45:35 +08:00
|
|
|
from bin.render_ import render_temp_
|
2022-02-04 11:08:34 +08:00
|
|
|
from bin.b64img import re_sort_number_image
|
2022-02-05 16:45:35 +08:00
|
|
|
from bin.length import make_html
|
2022-02-04 20:53:44 +08:00
|
|
|
from wsgiref.simple_server import make_server
|
2022-02-04 19:49:52 +08:00
|
|
|
from db.db import (
|
|
|
|
|
fetch_data,
|
|
|
|
|
update_data
|
|
|
|
|
)
|
2022-02-03 20:02:38 +08:00
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
|
|
2022-02-05 16:45:35 +08:00
|
|
|
def error_length(length: int) -> Response:
|
2022-02-03 20:02:38 +08:00
|
|
|
"""
|
2022-02-05 16:45:35 +08:00
|
|
|
数值太长显示此页面
|
|
|
|
|
:param length:
|
2022-02-03 20:02:38 +08:00
|
|
|
:return:
|
|
|
|
|
"""
|
2022-02-05 16:45:35 +08:00
|
|
|
response = make_response({'code': 200,
|
|
|
|
|
'message': f'错误的长度: {length}'})
|
|
|
|
|
response.status_code = 200
|
|
|
|
|
response.headers['Content-Type'] = 'application/json; charset=utf-8'
|
|
|
|
|
response.headers['cache-control'] = 'max-age=0, no-cache, no-store, must-revalidate'
|
|
|
|
|
response.headers['date'] = time.ctime()
|
|
|
|
|
return response
|
2022-02-04 19:49:52 +08:00
|
|
|
|
|
|
|
|
|
2022-02-05 16:45:35 +08:00
|
|
|
def too_lang_to_count() -> Response:
|
2022-02-04 19:49:52 +08:00
|
|
|
"""
|
2022-02-05 16:45:35 +08:00
|
|
|
数据过长重置数据
|
2022-02-04 19:49:52 +08:00
|
|
|
:return:
|
|
|
|
|
"""
|
|
|
|
|
response = make_response({'code': 200,
|
2022-02-05 16:45:35 +08:00
|
|
|
'message': f'当前长度超过了预设的值'})
|
2022-02-04 20:27:06 +08:00
|
|
|
response.headers['Content-Type'] = 'application/json; charset=utf-8'
|
|
|
|
|
response.headers['cache-control'] = 'max-age=0, no-cache, no-store, must-revalidate'
|
|
|
|
|
response.headers['date'] = time.ctime()
|
2022-02-04 19:49:52 +08:00
|
|
|
return response
|
2022-02-03 20:02:38 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def arg_not_be_full() -> Response:
|
|
|
|
|
"""
|
|
|
|
|
参数不完整
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
2022-02-04 12:17:28 +08:00
|
|
|
response = make_response({'code': 200,
|
2022-02-05 16:45:35 +08:00
|
|
|
'message': '参数填写不完整或填写错误'})
|
2022-02-03 20:02:38 +08:00
|
|
|
response.status_code = 200
|
2022-02-04 12:17:28 +08:00
|
|
|
response.headers['Content-Type'] = 'application/json; charset=utf-8'
|
2022-02-04 20:27:06 +08:00
|
|
|
response.headers['cache-control'] = 'max-age=0, no-cache, no-store, must-revalidate'
|
|
|
|
|
response.headers['date'] = time.ctime()
|
2022-02-03 20:02:38 +08:00
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
2022-02-05 16:45:35 +08:00
|
|
|
def build_page(name: str, length: int) -> Response | str | bool:
|
|
|
|
|
"""
|
|
|
|
|
渲染最终的页面
|
|
|
|
|
:param name:
|
|
|
|
|
:param length:
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
|
|
|
|
count = fetch_data(name)
|
|
|
|
|
if len(str(count)) > length:
|
|
|
|
|
return too_lang_to_count()
|
|
|
|
|
if 7 <= length <= 10:
|
|
|
|
|
make_html(length)
|
|
|
|
|
zero_count = '0' * (length - len(str(count))) + str(count)
|
|
|
|
|
sorted_image = re_sort_number_image(zero_count)
|
|
|
|
|
return render_temp_(length, name, sorted_image)
|
|
|
|
|
else:
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
2022-02-04 19:49:52 +08:00
|
|
|
@app.route('/API', methods=['GET', 'POST']) # 允许 GET 和 POST 方法
|
2022-02-05 16:45:35 +08:00
|
|
|
def api_page() -> Response | str:
|
2022-02-03 20:02:38 +08:00
|
|
|
"""
|
2022-02-04 19:49:52 +08:00
|
|
|
API 页面函数
|
2022-02-03 20:02:38 +08:00
|
|
|
:return:
|
|
|
|
|
"""
|
2022-02-05 16:45:35 +08:00
|
|
|
if 'length' in request.args:
|
|
|
|
|
try:
|
|
|
|
|
length = int(request.args['length'])
|
|
|
|
|
except ValueError:
|
|
|
|
|
return arg_not_be_full()
|
|
|
|
|
else:
|
|
|
|
|
length = 8
|
2022-02-04 11:56:21 +08:00
|
|
|
if 'name' in request.args:
|
2022-02-04 12:17:28 +08:00
|
|
|
name = request.args['name']
|
2022-02-04 19:49:52 +08:00
|
|
|
if name != '': # 数据为空返回参数不完整界面
|
2022-02-05 16:45:35 +08:00
|
|
|
resp = build_page(name, length)
|
|
|
|
|
if resp:
|
2022-02-04 19:49:52 +08:00
|
|
|
response = make_response(resp)
|
|
|
|
|
response.headers['Content-Type'] = 'image/svg+xml; charset=utf-8'
|
2022-02-04 20:27:06 +08:00
|
|
|
response.headers['cache-control'] = 'max-age=0, no-cache, no-store, must-revalidate'
|
|
|
|
|
response.headers['date'] = time.ctime()
|
2022-02-04 19:49:52 +08:00
|
|
|
return response
|
|
|
|
|
else:
|
2022-02-05 16:45:35 +08:00
|
|
|
return error_length(length)
|
2022-02-04 12:17:28 +08:00
|
|
|
else:
|
|
|
|
|
return arg_not_be_full()
|
2022-02-03 20:02:38 +08:00
|
|
|
else:
|
2022-02-04 11:56:21 +08:00
|
|
|
return arg_not_be_full()
|
2022-02-03 20:02:38 +08:00
|
|
|
|
|
|
|
|
|
2022-02-04 11:08:34 +08:00
|
|
|
@app.route('/', methods=['GET', 'POST'])
|
2022-02-03 20:02:38 +08:00
|
|
|
def index():
|
|
|
|
|
"""
|
|
|
|
|
索引页面
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
2022-02-04 11:08:34 +08:00
|
|
|
return send_from_directory('./static', 'index.html')
|
2022-02-03 20:02:38 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2022-02-04 20:53:44 +08:00
|
|
|
server = make_server('', 5000, app)
|
|
|
|
|
server.serve_forever()
|