Archived
为代码添加了注释
This commit is contained in:
7 files changed
+32
-15
No files matched your search
@@ -30,7 +30,7 @@ def miss(reason) -> Response:
|
||||
:param reason:
|
||||
:return:
|
||||
"""
|
||||
return jsonify({'code': 404, 'msg': f'{reason}', 'data': None})
|
||||
return jsonify({'code': 404, 'msg': '没有定义的页面', 'data': None})
|
||||
|
||||
|
||||
@app.errorhandler(500)
|
||||
@@ -40,7 +40,7 @@ def error(reason) -> Response:
|
||||
:param reason:
|
||||
:return:
|
||||
"""
|
||||
return jsonify({'code': 500, 'msg': f'{reason}', 'data': None})
|
||||
return jsonify({'code': 500, 'msg': '服务器内部错误', 'data': None})
|
||||
|
||||
|
||||
def build_page(name: str, length: int, theme: str) -> list[bool or Response] or list[bool or str] or bool:
|
||||
@@ -52,9 +52,9 @@ def build_page(name: str, length: int, theme: str) -> list[bool or Response] or
|
||||
:return:
|
||||
"""
|
||||
count = fetch_data(name)
|
||||
if len(str(count)) > length:
|
||||
if len(str(count)) > length: # 判断在数据库内的长度是否超过了设定的(或预设的)长度
|
||||
return [False, ErrorProcess().too_lang_to_count(name)]
|
||||
if 7 <= length <= 10:
|
||||
if 7 <= length <= 10: # 判断设定的长度是否超过阈值
|
||||
zero_count = '0' * (length - len(str(count))) + str(count)
|
||||
status, sorted_image, width, height = re_sort_number_image(zero_count, theme)
|
||||
if status:
|
||||
@@ -66,7 +66,7 @@ def build_page(name: str, length: int, theme: str) -> list[bool or Response] or
|
||||
|
||||
|
||||
@app.route('/get', methods=['GET', 'POST']) # 允许 GET 和 POST 方法
|
||||
def api_page() -> Response or str:
|
||||
def main() -> Response or str:
|
||||
"""
|
||||
API 页面函数
|
||||
:return:
|
||||
@@ -81,12 +81,12 @@ def api_page() -> Response or str:
|
||||
if not length:
|
||||
length = 7
|
||||
else:
|
||||
length = int(length)
|
||||
length = int(length) # 将类型转换为整型
|
||||
if not theme:
|
||||
theme = 'lewd'
|
||||
build_page_result = build_page(name, length, theme)
|
||||
build_page_result = build_page(name, length, theme) # 开始处理整体页面
|
||||
if build_page_result[0]:
|
||||
response = make_response(build_page_result[1])
|
||||
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()
|
||||
@@ -120,9 +120,11 @@ def index() -> Response:
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print('服务器已在 http://127.0.0.1:5000 运行')
|
||||
print('I: 服务器已在 http://127.0.0.1:5000 运行')
|
||||
try:
|
||||
server = pywsgi.WSGIServer(('0.0.0.0', 5000), app)
|
||||
server.serve_forever()
|
||||
except OSError:
|
||||
print('5000 端口被占用')
|
||||
print('E: 5000 端口被占用')
|
||||
except KeyboardInterrupt:
|
||||
print('I: 程序已退出')
|
||||
+2
-2
@@ -36,7 +36,7 @@ def re_sort_number_image(origin_number: str, theme: str) -> tuple[bool, bool, bo
|
||||
|
||||
final_b64_img_code = []
|
||||
if origin_number != '0000000000':
|
||||
for i in origin_number:
|
||||
for i in origin_number: # 通过elif语句依次判断数字
|
||||
if i == '0':
|
||||
final_b64_img_code.append(b_64_list[0])
|
||||
elif i == '1':
|
||||
@@ -61,7 +61,7 @@ def re_sort_number_image(origin_number: str, theme: str) -> tuple[bool, bool, bo
|
||||
return [True, final_b64_img_code, width_list[0], height_list[0]]
|
||||
else:
|
||||
for i in range(10):
|
||||
final_b64_img_code.append(b_64_list[0])
|
||||
final_b64_img_code.append(b_64_list[0]) # 直接将0返回
|
||||
return [True, final_b64_img_code, width_list[0], height_list[0]]
|
||||
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ from db.db import (update_data, fetch_table)
|
||||
|
||||
|
||||
class ErrorProcess:
|
||||
"""处理错误的页面"""
|
||||
def __init__(self):
|
||||
self.msg_template = {'code': -2,
|
||||
'msg': '',
|
||||
|
||||
@@ -24,7 +24,7 @@ def render_temp_(length: int, name: str, image_list: list, width: int, height: i
|
||||
position_list.append(i * width)
|
||||
file_name = f'index{length}.html'
|
||||
|
||||
# 自己写的都看不下去了
|
||||
# 下面的代码超级烂
|
||||
if length == 7:
|
||||
return render_template(file_name,
|
||||
title=name,
|
||||
|
||||
@@ -4,3 +4,17 @@
|
||||
# @Development Tool: PyCharm
|
||||
# @Create Time: 2022/2/4
|
||||
# @File Name: __init__.py.py
|
||||
|
||||
|
||||
import os
|
||||
|
||||
if not os.path.exists('./db/count.db'):
|
||||
print('未检测到数据库')
|
||||
import sqlite3
|
||||
conn = sqlite3.connect('./db/count.db', check_same_thread=False)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute('create table ReqCount (name text primary key, times int)')
|
||||
conn.commit()
|
||||
cursor.close()
|
||||
conn.close()
|
||||
print('已在./db 目录下创建了count.db数据库')
|
||||
BIN
Binary file not shown.
@@ -34,7 +34,7 @@ def fetch_data(name: str) -> int:
|
||||
:param name:
|
||||
:return:
|
||||
"""
|
||||
conn = sqlite3.connect('./db/d.db', check_same_thread=False)
|
||||
conn = sqlite3.connect('./db/count.db', check_same_thread=False)
|
||||
cursor = conn.cursor()
|
||||
try:
|
||||
cursor.execute('select * from ReqCount')
|
||||
@@ -42,7 +42,7 @@ def fetch_data(name: str) -> int:
|
||||
data = cursor.fetchall()
|
||||
|
||||
temp_dict = {}
|
||||
for k, v in data:
|
||||
for k, v in data: # 遍历数据将元组数据转换为字典类型
|
||||
temp_dict.setdefault(k, []).append(v)
|
||||
for i, c in zip(temp_dict.keys(), temp_dict.values()):
|
||||
temp_dict[i] = c[0]
|
||||
|
||||
Reference in New Issue
Block a user