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

133 lines
4.0 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 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-06 13:02:01 +08:00
from gevent import pywsgi
2022-02-05 17:43:04 +08:00
from bin.core.render_ import render_temp_
from bin.core.b64img import re_sort_number_image
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-06 13:02:01 +08:00
response = make_response({'code': -2,
2022-02-05 16:45:35 +08:00
'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-05 17:43:04 +08:00
def too_lang_to_count(name: str) -> Response:
"""
2022-02-05 16:45:35 +08:00
数据过长重置数据
2022-02-05 17:43:04 +08:00
:param name:
:return:
"""
2022-02-05 17:43:04 +08:00
update_data(name, 0)
2022-02-06 13:02:01 +08:00
response = make_response({'code': -2,
2022-02-05 17:43:04 +08:00
'message': f'当前长度超过了最大可计数长度:10. 已将重置该名称的计数器'})
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-03 20:02:38 +08:00
def arg_not_be_full() -> Response:
"""
参数不完整
:return:
"""
2022-02-06 13:02:01 +08:00
response = make_response({'code': -2,
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'
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-07 17:22:02 +08:00
def build_page(name: str, length: int, theme: str) -> list[Response] | list[bool | str] | list[bool]:
2022-02-05 16:45:35 +08:00
"""
渲染最终的页面
2022-02-07 17:22:02 +08:00
:param theme:
2022-02-05 16:45:35 +08:00
:param name:
:param length:
:return:
"""
count = fetch_data(name)
if len(str(count)) > length:
2022-02-05 17:43:04 +08:00
return [False, too_lang_to_count(name)]
2022-02-05 16:45:35 +08:00
if 7 <= length <= 10:
zero_count = '0' * (length - len(str(count))) + str(count)
2022-02-07 17:22:02 +08:00
sorted_image = re_sort_number_image(zero_count, theme)
2022-02-07 22:11:56 +08:00
return [True, render_temp_(length, name, sorted_image, theme)]
2022-02-05 16:45:35 +08:00
else:
2022-02-05 17:43:04 +08:00
return [False, 'BadLength']
2022-02-05 16:45:35 +08:00
2022-02-07 22:11:56 +08:00
@app.route('/get', 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
"""
API 页面函数
2022-02-03 20:02:38 +08:00
:return:
"""
2022-02-07 17:22:02 +08:00
args = request.args
name = str(args.get('name')).replace('None', 'null')
length = args.get('length')
theme = args.get('theme')
if name and name != 'null':
if not length:
2022-02-07 22:11:56 +08:00
length = 7
2022-02-07 17:22:02 +08:00
else:
length = int(length)
if not theme:
2022-02-07 22:11:56 +08:00
theme = 'moebooru'
2022-02-07 17:22:02 +08:00
build_page_result = build_page(name, length, theme)
if build_page_result[0]:
response = make_response(build_page_result[1])
response.headers['Content-Type'] = 'image/svg+xml; charset=utf-8'
response.headers['cache-control'] = 'max-age=0, no-cache, no-store, must-revalidate'
response.headers['date'] = time.ctime()
return response
elif build_page_result[1] == 'BadLength':
return error_length(length)
2022-02-04 12:17:28 +08:00
else:
2022-02-07 17:22:02 +08:00
return build_page_result[1]
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-07 17:22:02 +08:00
print('服务器已在 http://127.0.0.1:5000 运行')
2022-02-06 13:02:01 +08:00
try:
server = pywsgi.WSGIServer(('0.0.0.0', 5000), app)
server.serve_forever()
except OSError:
print('5000 端口被占用')