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 render_template
|
|
|
|
|
from flask import send_from_directory
|
2022-02-04 11:08:34 +08:00
|
|
|
from bin.b64img import re_sort_number_image
|
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-04 19:49:52 +08:00
|
|
|
def is_new_user(name: str):
|
2022-02-03 20:02:38 +08:00
|
|
|
"""
|
2022-02-04 19:49:52 +08:00
|
|
|
判断名称是否在数据库内
|
|
|
|
|
如果在则将该名称的值加1
|
|
|
|
|
如果不在数据库内则新加此名称到数据库
|
2022-02-03 20:02:38 +08:00
|
|
|
:param name:
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
2022-02-04 19:49:52 +08:00
|
|
|
"""fetch_data 函数返回的是当前名称下访问次数的数值 为整型"""
|
|
|
|
|
count = fetch_data(name)
|
|
|
|
|
if count == 0:
|
|
|
|
|
return [True, 0]
|
|
|
|
|
elif len(str(count)) <= 10:
|
|
|
|
|
return [True, count]
|
2022-02-04 11:56:21 +08:00
|
|
|
else:
|
2022-02-04 19:49:52 +08:00
|
|
|
update_data(name, 0)
|
|
|
|
|
response = make_response({'code': 200,
|
|
|
|
|
'message': '当前名称数值已经为最大, 已将数值重置',
|
|
|
|
|
'timestamp': time.time()})
|
|
|
|
|
response.headers['Content-Type'] = 'application/json'
|
|
|
|
|
return [False, response]
|
2022-02-03 20:02:38 +08:00
|
|
|
|
|
|
|
|
|
2022-02-04 12:57:24 +08:00
|
|
|
def build_page(name: str) -> Response or str:
|
2022-02-03 20:02:38 +08:00
|
|
|
"""
|
|
|
|
|
渲染最终的页面
|
|
|
|
|
:param name:
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
2022-02-04 19:49:52 +08:00
|
|
|
status, count = is_new_user(name)
|
|
|
|
|
if status:
|
2022-02-04 11:56:21 +08:00
|
|
|
"""
|
|
|
|
|
计算出要在数字前加上几个0, 其实就是10减去数字位数
|
|
|
|
|
假如当前访问数字是300, 先将整形转换为字符串, 再使用len()计算出长度
|
|
|
|
|
使用10 - 去长度, 剩下的结果就是要在前面加上几个0
|
|
|
|
|
"""
|
|
|
|
|
zero_counter = 10 - len(str(count))
|
2022-02-04 12:57:24 +08:00
|
|
|
if count != 0: # 不是新名称时直接计算要添加几个零
|
2022-02-04 11:56:21 +08:00
|
|
|
final_number = '0' * zero_counter + str(count) # 拼接
|
2022-02-04 12:57:24 +08:00
|
|
|
else: # 是新名称直接设置为10个零
|
2022-02-04 11:56:21 +08:00
|
|
|
final_number = '0000000000'
|
|
|
|
|
sorted_image = re_sort_number_image(final_number)
|
|
|
|
|
return render_template('index.html',
|
|
|
|
|
title=name,
|
|
|
|
|
svg_img_0=f'{sorted_image[0]}',
|
|
|
|
|
svg_img_1=f'{sorted_image[1]}',
|
|
|
|
|
svg_img_2=f'{sorted_image[2]}',
|
|
|
|
|
svg_img_3=f'{sorted_image[3]}',
|
|
|
|
|
svg_img_4=f'{sorted_image[4]}',
|
|
|
|
|
svg_img_5=f'{sorted_image[5]}',
|
|
|
|
|
svg_img_6=f'{sorted_image[6]}',
|
|
|
|
|
svg_img_7=f'{sorted_image[7]}',
|
|
|
|
|
svg_img_8=f'{sorted_image[8]}',
|
|
|
|
|
svg_img_9=f'{sorted_image[9]}'
|
|
|
|
|
) # 渲染模板
|
2022-02-03 20:02:38 +08:00
|
|
|
else:
|
2022-02-04 19:49:52 +08:00
|
|
|
return 'LONG'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def too_long_to_count() -> Response:
|
|
|
|
|
"""
|
|
|
|
|
数值太长显示此页面
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
|
|
|
|
response = make_response({'code': 200,
|
|
|
|
|
'message': '当前数值超过了最大可计数范围(10位)已将此名称的数据重置'})
|
|
|
|
|
response.status_code = 200
|
|
|
|
|
response.headers['Content-Type'] = 'application/json'
|
|
|
|
|
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-04 19:49:52 +08:00
|
|
|
'message': '参数填写不完整',
|
|
|
|
|
'timestamp': time.time()})
|
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-03 20:02:38 +08:00
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
2022-02-04 19:49:52 +08:00
|
|
|
@app.route('/API', methods=['GET', 'POST']) # 允许 GET 和 POST 方法
|
2022-02-03 20:02:38 +08:00
|
|
|
def api_page() -> Response or str:
|
|
|
|
|
"""
|
2022-02-04 19:49:52 +08:00
|
|
|
API 页面函数
|
2022-02-03 20:02:38 +08:00
|
|
|
:return:
|
|
|
|
|
"""
|
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-04 12:17:28 +08:00
|
|
|
resp = build_page(name)
|
2022-02-04 19:49:52 +08:00
|
|
|
if resp != 'LONG':
|
|
|
|
|
response = make_response(resp)
|
|
|
|
|
response.headers['Content-Type'] = 'image/svg+xml; charset=utf-8'
|
|
|
|
|
return response
|
|
|
|
|
else:
|
|
|
|
|
return too_long_to_count()
|
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 11:08:34 +08:00
|
|
|
app.run(host='0.0.0.0', port=5000, threaded=True)
|