From 9182597559d5ec5500afd1458f81dffd0f7daeda Mon Sep 17 00:00:00 2001 From: MarkusJoe Date: Fri, 11 Feb 2022 17:58:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BA=E4=BB=A3=E7=A0=81=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E4=BA=86=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.py | 22 ++++++++++++---------- bin/utils/b64img.py | 4 ++-- bin/utils/error.py | 1 + bin/utils/render_.py | 2 +- db/__init__.py | 14 ++++++++++++++ db/count.db | Bin 12288 -> 0 bytes db/db.py | 4 ++-- 7 files changed, 32 insertions(+), 15 deletions(-) delete mode 100644 db/count.db diff --git a/app.py b/app.py index 60040c4..bf6b02c 100644 --- a/app.py +++ b/app.py @@ -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: 程序已退出') diff --git a/bin/utils/b64img.py b/bin/utils/b64img.py index 4d26669..5ffdfc5 100644 --- a/bin/utils/b64img.py +++ b/bin/utils/b64img.py @@ -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]] diff --git a/bin/utils/error.py b/bin/utils/error.py index f63e16b..dae9f5b 100644 --- a/bin/utils/error.py +++ b/bin/utils/error.py @@ -13,6 +13,7 @@ from db.db import (update_data, fetch_table) class ErrorProcess: + """处理错误的页面""" def __init__(self): self.msg_template = {'code': -2, 'msg': '', diff --git a/bin/utils/render_.py b/bin/utils/render_.py index 963ab92..7568f45 100644 --- a/bin/utils/render_.py +++ b/bin/utils/render_.py @@ -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, diff --git a/db/__init__.py b/db/__init__.py index 840fd69..1915385 100644 --- a/db/__init__.py +++ b/db/__init__.py @@ -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数据库') diff --git a/db/count.db b/db/count.db deleted file mode 100644 index 1a714f5fc123c210ed88eefd6d62e05b3ab9482e..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI$!AiqG5C-7cjfPezYY(Mf0&`RlMSKBk$*mx^xrazr3^s{Lw$L8>YF>SWK81(g zil=T0d+xdN4@_n!%LeAVZDy~N%xHcu>pU_1NS7o8Juwr}#-?Y}g|Le=*Vx+j$6`Zw zFYBJ#kyI6_O8p!=fPDx+00Izz00bZa0SG_<0uX?}zYvIoKMVq~Hp$yuPjxje7lmD7guN~$fXt@=Ct0WkptAOHafKmY;|fB*y_009U<00IYrjyDkPF93P& I^}9{F2mhQ!AOHXW diff --git a/db/db.py b/db/db.py index c686466..ee60e0d 100644 --- a/db/db.py +++ b/db/db.py @@ -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]