This repository has been archived on 2026-08-27. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
RequestCounter/app.py
T

138 lines
4.5 KiB
Python
Raw Normal View History

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
import json
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-03 20:02:38 +08:00
app = Flask(__name__)
2022-02-04 11:56:21 +08:00
def is_new_user(name: str, reset: bool = False) -> int:
2022-02-03 20:02:38 +08:00
"""
判断填入的参数是否为已有本地已有
如果有则该名称值加一
如果没有则新建一个
2022-02-04 11:56:21 +08:00
:param reset:
2022-02-03 20:02:38 +08:00
:param name:
:return:
"""
2022-02-04 11:56:21 +08:00
if not reset:
with open('./static/data.json', 'r', encoding='utf-8') as fp:
try:
data = json.load(fp)
except json.decoder.JSONDecodeError:
with open('./static/data.json', 'w', encoding='utf-8') as init_fp:
json.dump({}, init_fp)
all_user = data.keys()
try:
if name in all_user: # 加次数
data[name] += 1
return data[name]
else:
data[name] = 0 # 新加名称
return 0
finally:
with open('./static/data.json', 'w', encoding='utf-8') as dump:
json.dump(data, dump)
else:
with open('./static/data.json', 'r', encoding='utf-8') as reset_fp:
data = json.load(reset_fp)
data[name] = 0 # 重置该名称的值
2022-02-04 12:57:24 +08:00
with open('./static/data.json', 'w', encoding='utf-8') as reset_dump: #
2022-02-04 11:56:21 +08:00
json.dump(data, reset_dump)
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:
"""
count = is_new_user(name)
2022-02-04 11:56:21 +08:00
if len(str(count)) <= 10:
"""
计算出要在数字前加上几个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 11:56:21 +08:00
is_new_user(name, reset=True)
2022-02-04 12:57:24 +08:00
response = make_response({'code': 200,
'message': '当前名称数值已经为最大, 已将数值重置'})
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,
'message': 'Incomplete parameters'})
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 11:08:34 +08:00
@app.route('/API', methods=['GET', 'POST'])
2022-02-03 20:02:38 +08:00
def api_page() -> Response or str:
"""
API页面主函数
: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']
if name != '':
resp = build_page(name)
response = make_response(resp)
response.headers['Content-Type'] = 'image/svg+xml; charset=utf-8'
return response
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)