diff --git a/README.md b/README.md index d031c38..56355ec 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@

## Python 版访问次数计数器使用了flask 作为服务端接收请求 -## 借鉴于: [Moe Counter](https://github.com/journey-ad/Moe-counter) +## 借鉴于(仿制): [Moe Counter](https://github.com/journey-ad/Moe-counter) (应该算借鉴吧) 作者:[journey-ad](https://github.com/journey-ad/Moe-counter/commits?author=journey-ad) ### 开源 @@ -25,7 +25,11 @@ ## 调用须知 - API支持`GET` 和 `POST` 方法请求 - 最大可以计数`10`位数, 超过则重置 -- + + +# 一些信息 +- 使用了Python3.6 的标准库`Sqlite3`进行数据库操作 +- 开启时占用了`32Mb`的内存 diff --git a/__pycache__/app.cpython-310.pyc b/__pycache__/app.cpython-310.pyc index 1a20d30..3bf705f 100644 Binary files a/__pycache__/app.cpython-310.pyc and b/__pycache__/app.cpython-310.pyc differ diff --git a/app.py b/app.py index 9dce3f2..ae01f45 100644 --- a/app.py +++ b/app.py @@ -6,7 +6,7 @@ # @File Name: app.py -import json +import time from flask import Flask from flask import request from flask import Response @@ -14,44 +14,35 @@ from flask import make_response from flask import render_template from flask import send_from_directory from bin.b64img import re_sort_number_image +from db.db import ( + fetch_data, + update_data +) app = Flask(__name__) -def is_new_user(name: str, reset: bool = False) -> int: +def is_new_user(name: str): """ - 判断填入的参数是否为已有本地已有 - 如果有则该名称值加一 - 如果没有则新建一个 - :param reset: + 判断名称是否在数据库内 + 如果在则将该名称的值加1 + 如果不在数据库内则新加此名称到数据库 :param name: :return: """ - 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) + """fetch_data 函数返回的是当前名称下访问次数的数值 为整型""" + count = fetch_data(name) + if count == 0: + return [True, 0] + elif len(str(count)) <= 10: + return [True, count] else: - with open('./static/data.json', 'r', encoding='utf-8') as reset_fp: - data = json.load(reset_fp) - data[name] = 0 # 重置该名称的值 - with open('./static/data.json', 'w', encoding='utf-8') as reset_dump: # - json.dump(data, reset_dump) + update_data(name, 0) + response = make_response({'code': 200, + 'message': '当前名称数值已经为最大, 已将数值重置', + 'timestamp': time.time()}) + response.headers['Content-Type'] = 'application/json' + return [False, response] def build_page(name: str) -> Response or str: @@ -60,8 +51,8 @@ def build_page(name: str) -> Response or str: :param name: :return: """ - count = is_new_user(name) - if len(str(count)) <= 10: + status, count = is_new_user(name) + if status: """ 计算出要在数字前加上几个0, 其实就是10减去数字位数 假如当前访问数字是300, 先将整形转换为字符串, 再使用len()计算出长度 @@ -87,11 +78,19 @@ def build_page(name: str) -> Response or str: svg_img_9=f'{sorted_image[9]}' ) # 渲染模板 else: - is_new_user(name, reset=True) - response = make_response({'code': 200, - 'message': '当前名称数值已经为最大, 已将数值重置'}) - response.headers['Content-Type'] = 'application/json' - return response + 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 def arg_not_be_full() -> Response: @@ -100,25 +99,29 @@ def arg_not_be_full() -> Response: :return: """ response = make_response({'code': 200, - 'message': 'Incomplete parameters'}) + 'message': '参数填写不完整', + 'timestamp': time.time()}) response.status_code = 200 response.headers['Content-Type'] = 'application/json; charset=utf-8' return response -@app.route('/API', methods=['GET', 'POST']) +@app.route('/API', methods=['GET', 'POST']) # 允许 GET 和 POST 方法 def api_page() -> Response or str: """ - API页面主函数 + API 页面函数 :return: """ if 'name' in request.args: name = request.args['name'] - if name != '': + if name != '': # 数据为空返回参数不完整界面 resp = build_page(name) - response = make_response(resp) - response.headers['Content-Type'] = 'image/svg+xml; charset=utf-8' - return response + 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() else: return arg_not_be_full() else: diff --git a/db/__init__.py b/db/__init__.py new file mode 100644 index 0000000..c022d54 --- /dev/null +++ b/db/__init__.py @@ -0,0 +1,6 @@ +#!/usr/bin/env python3 +# -- coding:utf-8 -- +# @Author: markushammered@gmail.com +# @Development Tool: PyCharm +# @Create Time: 2022/2/4 +# @File Name: __init__.py.py diff --git a/db/__pycache__/__init__.cpython-310.pyc b/db/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 0000000..97fca6f Binary files /dev/null and b/db/__pycache__/__init__.cpython-310.pyc differ diff --git a/db/__pycache__/db.cpython-310.pyc b/db/__pycache__/db.cpython-310.pyc new file mode 100644 index 0000000..c683e1e Binary files /dev/null and b/db/__pycache__/db.cpython-310.pyc differ diff --git a/db/data.db b/db/data.db new file mode 100644 index 0000000..938e482 Binary files /dev/null and b/db/data.db differ diff --git a/db/db.py b/db/db.py new file mode 100644 index 0000000..3ae6ca3 --- /dev/null +++ b/db/db.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python3 +# -- coding:utf-8 -- +# @Author: markushammered@gmail.com +# @Development Tool: PyCharm +# @Create Time: 2022/2/4 +# @File Name: db.py + +import sqlite3 + +""" +使用python3自带的Sqlite3进行数据库操作 +""" + + +def insert_data(name: str): + """ + 插入数据 + :param name: + :return: + """ + cursor.execute('insert into ReqCount values(?, ?)', (name, 0)) + conn.commit() + + +def fetch_data(name: str, mode: bool = False) -> int: + """ + 获取数据 + :param name: + :return: + """ + cursor.execute('select * from ReqCount') + conn.commit() + data = cursor.fetchall() + + # 将返回的元组转换为字典 + temp_dict = {} + 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] + + if name in temp_dict.keys(): + count = temp_dict[name] # 获取原数字 为 整型 + update_data(name, count) + return temp_dict[name] + else: + insert_data(name) + return 0 + + +def update_data(name: str, times: int): + """ + 更新数据 + :param name: + :param times: + :return: + """ + times += 1 + cursor.execute('update ReqCount set times=? where name=?', (times, name)) + conn.commit() + + +if __name__ != '__main__': + conn = sqlite3.connect('./db/data.db', check_same_thread=False) + cursor = conn.cursor() diff --git a/static/data.json b/static/data.json deleted file mode 100644 index 0b5772f..0000000 --- a/static/data.json +++ /dev/null @@ -1 +0,0 @@ -{"MarkusJoe": 27, "Mar": 4, "M": 2, "": 23, "oo": 103, "sdhi": 13, "sdsdsdsdsdsdawioruvniovenyrui": 0, "rui": 11, "ru": 0, "r": 0, "rr": 0, "Ma": 0, "Mark": 0, "Marku": 0, "Markus": 0, "MarkusK": 0, "MarkusKe": 0, "MarkusJ": 0, "MarkusJo": 0, "sd": 75, "1": 2, "87": 1, "20": 2, "49": 2, "36": 2, "7": 2, "88": 1, "68": 1, "65": 1, "5": 2, "91": 1, "58": 2, "89": 1, "78": 1, "40": 2, "31": 2, "33": 2, "48": 2, "47": 2, "8": 2, "55": 2, "24": 2, "81": 1, "50": 2, "83": 1, "28": 2, "13": 2, "35": 2, "85": 1, "76": 1, "0": 1, "2": 1, "3": 1, "4": 1, "6": 1, "9": 1, "10": 1, "11": 1, "12": 1, "14": 1, "15": 1, "16": 1, "17": 1, "18": 1, "19": 1, "21": 1, "22": 1, "23": 1, "25": 1, "26": 1, "27": 1, "29": 1, "30": 1, "32": 1, "34": 1, "37": 1, "38": 1, "39": 1, "41": 1, "42": 1, "43": 1, "44": 1, "45": 1, "46": 1, "51": 1, "52": 1, "53": 1, "54": 1, "56": 1, "57": 1, "59": 1, "60": 1, "61": 0, "62": 0, "63": 0, "64": 0, "66": 0, "67": 0, "69": 0, "70": 0, "71": 0, "72": 0, "73": 0, "74": 0, "75": 0, "77": 0, "79": 0, "80": 0, "82": 0, "84": 0, "86": 0, "90": 0, "92": 0, "93": 0, "94": 0, "95": 0, "96": 0, "97": 0, "98": 0, "99": 0} \ No newline at end of file