Archived
update: use Database to record request times
This commit is contained in:
9 files changed
+125
-47
No files matched your search
@@ -7,7 +7,7 @@
|
|||||||
</p>
|
</p>
|
||||||
|
|
||||||
## Python 版访问次数计数器使用了flask 作为服务端接收请求
|
## 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)
|
作者:[journey-ad](https://github.com/journey-ad/Moe-counter/commits?author=journey-ad)
|
||||||
|
|
||||||
### 开源
|
### 开源
|
||||||
@@ -25,7 +25,11 @@
|
|||||||
## 调用须知
|
## 调用须知
|
||||||
- API支持`GET` 和 `POST` 方法请求
|
- API支持`GET` 和 `POST` 方法请求
|
||||||
- 最大可以计数`10`位数, 超过则重置
|
- 最大可以计数`10`位数, 超过则重置
|
||||||
-
|
|
||||||
|
|
||||||
|
# 一些信息
|
||||||
|
- 使用了Python3.6 的标准库`Sqlite3`进行数据库操作
|
||||||
|
- 开启时占用了`32Mb`的内存
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
@@ -6,7 +6,7 @@
|
|||||||
# @File Name: app.py
|
# @File Name: app.py
|
||||||
|
|
||||||
|
|
||||||
import json
|
import time
|
||||||
from flask import Flask
|
from flask import Flask
|
||||||
from flask import request
|
from flask import request
|
||||||
from flask import Response
|
from flask import Response
|
||||||
@@ -14,44 +14,35 @@ from flask import make_response
|
|||||||
from flask import render_template
|
from flask import render_template
|
||||||
from flask import send_from_directory
|
from flask import send_from_directory
|
||||||
from bin.b64img import re_sort_number_image
|
from bin.b64img import re_sort_number_image
|
||||||
|
from db.db import (
|
||||||
|
fetch_data,
|
||||||
|
update_data
|
||||||
|
)
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
|
||||||
def is_new_user(name: str, reset: bool = False) -> int:
|
def is_new_user(name: str):
|
||||||
"""
|
"""
|
||||||
判断填入的参数是否为已有本地已有
|
判断名称是否在数据库内
|
||||||
如果有则该名称值加一
|
如果在则将该名称的值加1
|
||||||
如果没有则新建一个
|
如果不在数据库内则新加此名称到数据库
|
||||||
:param reset:
|
|
||||||
:param name:
|
:param name:
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
if not reset:
|
"""fetch_data 函数返回的是当前名称下访问次数的数值 为整型"""
|
||||||
with open('./static/data.json', 'r', encoding='utf-8') as fp:
|
count = fetch_data(name)
|
||||||
try:
|
if count == 0:
|
||||||
data = json.load(fp)
|
return [True, 0]
|
||||||
except json.decoder.JSONDecodeError:
|
elif len(str(count)) <= 10:
|
||||||
with open('./static/data.json', 'w', encoding='utf-8') as init_fp:
|
return [True, count]
|
||||||
json.dump({}, init_fp)
|
|
||||||
all_user = data.keys()
|
|
||||||
try:
|
|
||||||
if name in all_user: # 加次数
|
|
||||||
data[name] += 1
|
|
||||||
return data[name]
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
data[name] = 0 # 新加名称
|
update_data(name, 0)
|
||||||
return 0
|
response = make_response({'code': 200,
|
||||||
finally:
|
'message': '当前名称数值已经为最大, 已将数值重置',
|
||||||
with open('./static/data.json', 'w', encoding='utf-8') as dump:
|
'timestamp': time.time()})
|
||||||
json.dump(data, dump)
|
response.headers['Content-Type'] = 'application/json'
|
||||||
else:
|
return [False, response]
|
||||||
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)
|
|
||||||
|
|
||||||
|
|
||||||
def build_page(name: str) -> Response or str:
|
def build_page(name: str) -> Response or str:
|
||||||
@@ -60,8 +51,8 @@ def build_page(name: str) -> Response or str:
|
|||||||
:param name:
|
:param name:
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
count = is_new_user(name)
|
status, count = is_new_user(name)
|
||||||
if len(str(count)) <= 10:
|
if status:
|
||||||
"""
|
"""
|
||||||
计算出要在数字前加上几个0, 其实就是10减去数字位数
|
计算出要在数字前加上几个0, 其实就是10减去数字位数
|
||||||
假如当前访问数字是300, 先将整形转换为字符串, 再使用len()计算出长度
|
假如当前访问数字是300, 先将整形转换为字符串, 再使用len()计算出长度
|
||||||
@@ -87,9 +78,17 @@ def build_page(name: str) -> Response or str:
|
|||||||
svg_img_9=f'{sorted_image[9]}'
|
svg_img_9=f'{sorted_image[9]}'
|
||||||
) # 渲染模板
|
) # 渲染模板
|
||||||
else:
|
else:
|
||||||
is_new_user(name, reset=True)
|
return 'LONG'
|
||||||
|
|
||||||
|
|
||||||
|
def too_long_to_count() -> Response:
|
||||||
|
"""
|
||||||
|
数值太长显示此页面
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
response = make_response({'code': 200,
|
response = make_response({'code': 200,
|
||||||
'message': '当前名称数值已经为最大, 已将数值重置'})
|
'message': '当前数值超过了最大可计数范围(10位)已将此名称的数据重置'})
|
||||||
|
response.status_code = 200
|
||||||
response.headers['Content-Type'] = 'application/json'
|
response.headers['Content-Type'] = 'application/json'
|
||||||
return response
|
return response
|
||||||
|
|
||||||
@@ -100,25 +99,29 @@ def arg_not_be_full() -> Response:
|
|||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
response = make_response({'code': 200,
|
response = make_response({'code': 200,
|
||||||
'message': 'Incomplete parameters'})
|
'message': '参数填写不完整',
|
||||||
|
'timestamp': time.time()})
|
||||||
response.status_code = 200
|
response.status_code = 200
|
||||||
response.headers['Content-Type'] = 'application/json; charset=utf-8'
|
response.headers['Content-Type'] = 'application/json; charset=utf-8'
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
@app.route('/API', methods=['GET', 'POST'])
|
@app.route('/API', methods=['GET', 'POST']) # 允许 GET 和 POST 方法
|
||||||
def api_page() -> Response or str:
|
def api_page() -> Response or str:
|
||||||
"""
|
"""
|
||||||
API页面主函数
|
API 页面函数
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
if 'name' in request.args:
|
if 'name' in request.args:
|
||||||
name = request.args['name']
|
name = request.args['name']
|
||||||
if name != '':
|
if name != '': # 数据为空返回参数不完整界面
|
||||||
resp = build_page(name)
|
resp = build_page(name)
|
||||||
|
if resp != 'LONG':
|
||||||
response = make_response(resp)
|
response = make_response(resp)
|
||||||
response.headers['Content-Type'] = 'image/svg+xml; charset=utf-8'
|
response.headers['Content-Type'] = 'image/svg+xml; charset=utf-8'
|
||||||
return response
|
return response
|
||||||
|
else:
|
||||||
|
return too_long_to_count()
|
||||||
else:
|
else:
|
||||||
return arg_not_be_full()
|
return arg_not_be_full()
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -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
|
||||||
Binary file not shown.
Binary file not shown.
BIN
Binary file not shown.
@@ -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()
|
||||||
@@ -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}
|
|
||||||
Reference in New Issue
Block a user