修复bug

This commit is contained in:
MarkusJoe
2022-02-07 22:11:56 +08:00
parent 4479fbb3ea
commit 4f18fdcbdb
21 files changed
+212 -78

No files matched your search

+4 -6
View File
@@ -15,7 +15,6 @@ from flask import send_from_directory
from gevent import pywsgi
from bin.core.render_ import render_temp_
from bin.core.b64img import re_sort_number_image
from bin.core.length import make_html
from db.db import (fetch_data, update_data)
app = Flask(__name__)
@@ -77,15 +76,14 @@ def build_page(name: str, length: int, theme: str) -> list[Response] | list[bool
if len(str(count)) > length:
return [False, too_lang_to_count(name)]
if 7 <= length <= 10:
make_html(length)
zero_count = '0' * (length - len(str(count))) + str(count)
sorted_image = re_sort_number_image(zero_count, theme)
return [True, render_temp_(length, name, sorted_image)]
return [True, render_temp_(length, name, sorted_image, theme)]
else:
return [False, 'BadLength']
@app.route('/API', methods=['GET', 'POST']) # 允许 GET 和 POST 方法
@app.route('/get', methods=['GET', 'POST']) # 允许 GET 和 POST 方法
def api_page() -> Response | str:
"""
API 页面函数
@@ -97,11 +95,11 @@ def api_page() -> Response | str:
theme = args.get('theme')
if name and name != 'null':
if not length:
length = 8
length = 7
else:
length = int(length)
if not theme:
theme = 't1'
theme = 'moebooru'
build_page_result = build_page(name, length, theme)
if build_page_result[0]:
response = make_response(build_page_result[1])
Binary file not shown.
+2 -1
View File
@@ -1 +1,2 @@
# 此文件夹内的所有文件全部来自
# 此文件夹内的所有文件全部来自 https://github.com/journey-ad/Moe-counter
# 原作者: journey-ad
+15 -6
View File
@@ -16,15 +16,24 @@ def re_sort_number_image(origin_number: str, theme: str) -> list:
:param origin_number:
:return:
"""
print(theme)
if theme == 't1':
cursor.execute('select * from theme1')
data = cursor.fetchall()
elif theme == 't2':
cursor.execute('select * from theme2')
data = cursor.fetchall()
elif theme == 'gelbooru':
cursor.execute('select * from gelbooru')
elif theme == 'gelbooru-h':
cursor.execute('select * from gelbooruh')
elif theme == 'moebooru':
cursor.execute('select * from moebooru')
elif theme == 'moebooru-h':
cursor.execute('select * from moebooruh')
elif theme == 'rule34':
cursor.execute('select * from rule34')
else:
cursor.execute('select * from theme1')
data = cursor.fetchall()
cursor.execute('select * from rule34')
data = cursor.fetchall()
temp_dict = {}
for k, v in data:
temp_dict.setdefault(k, []).append(v)
@@ -63,5 +72,5 @@ def re_sort_number_image(origin_number: str, theme: str) -> list:
if __name__ != '__main__':
conn = sqlite3.connect('./bin/res/theme.db')
cursor = conn.cursor()
conn = sqlite3.connect('./bin/assets/theme.db')
cursor = conn.cursor()
-29
View File
@@ -1,29 +0,0 @@
#!/usr/bin/env python3
# -- coding:utf-8 --
# @Author: markushammered@gmail.com
# @Development Tool: PyCharm
# @Create Time: 2022/2/5
# @File Name: length.py
def make_html(length: int, svg_width: int = 45, svg_height: int = 50) -> None:
"""
生成html
:param svg_height:
:param svg_width:
:param length:
:return:
"""
html_head = """<?xml version="1.0" encoding="UTF-8"?>
<svg width="%(w)s" height="%(h)s" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>Flask Counter - {{ title }}</title>
<g>\n"""
svg_tag = """ <image x="%(x)s" y="0" width="%(w)s" height="%(h)s" xlink:href="{{ svg_img_%(i)s }}"></image>\n"""
html_feet = """</g></svg>"""
html_head = html_head % {'w': length * svg_width, 'h': svg_height} # 总体宽度
for i in range(length): # 拼接出完整的html
html_head += svg_tag % {'x': i * svg_width, 'w': svg_width, 'h': svg_height, 'i': i}
html_head += html_feet # 加上结尾标签
with open('./templates/index.html', 'w', encoding='utf-8') as template_fp: # 写入文件
template_fp.write(html_head)
+11 -6
View File
@@ -9,18 +9,23 @@
from flask import render_template
def render_temp_(length: int, name: str, image_list: list) -> str:
def render_temp_(length: int, name: str, image_list: list, theme: str) -> str:
"""
代码很烂写到隐蔽的地方就看不见了
:param theme:
:param length:
:param name:
:param image_list:
:return:
"""
if 'gelbooru' in theme:
file_name = f'index{length}-spec.html'
else:
file_name = f'index{length}.html'
# 自己写的都看不下去了
if length == 7:
return render_template('index.html',
return render_template(file_name,
title=name,
svg_img_0=f'{image_list[0]}',
svg_img_1=f'{image_list[1]}',
@@ -31,7 +36,7 @@ def render_temp_(length: int, name: str, image_list: list) -> str:
svg_img_6=f'{image_list[6]}'
)
elif length == 8:
return render_template('index.html',
return render_template(file_name,
title=name,
svg_img_0=f'{image_list[0]}',
svg_img_1=f'{image_list[1]}',
@@ -43,7 +48,7 @@ def render_temp_(length: int, name: str, image_list: list) -> str:
svg_img_7=f'{image_list[7]}'
)
elif length == 9:
return render_template('index.html',
return render_template(file_name,
title=name,
svg_img_0=f'{image_list[0]}',
svg_img_1=f'{image_list[1]}',
@@ -55,8 +60,8 @@ def render_temp_(length: int, name: str, image_list: list) -> str:
svg_img_7=f'{image_list[7]}',
svg_img_8=f'{image_list[8]}',
)
else:
return render_template('index.html',
elif length == 10:
return render_template(file_name,
title=name,
svg_img_0=f'{image_list[0]}',
svg_img_1=f'{image_list[1]}',
BIN
View File
Binary file not shown.
View File
Whitespace-only changes.
+26 -13
View File
@@ -1,6 +1,15 @@
# 这里是`Request Counter`的文档
虽然说项目很小根本不需要文档
# 这里是Request Counter的文档
> 虽然说项目很小根本不需要文档
# 灵感来源
[Moe-Counter](https://github.com/MarkusJoe/Moe-counter)
> 看到这个项目感觉挺有意思就用Python写了一个 此项目中添加了原项目中没有的功能 `Length` `设置长度`
# 工作原理
使用Flask建立服务器接收请求分析获取到的数据写入数据库
# 部署
> 在部署之前你需要先安装`python3.9.x`以上的版本
## 部署到本地服务器
```shell
$ git clone https://git@github.com:MarkusJoe/RequestCounter.git
@@ -9,9 +18,8 @@
$ python3 app.py
```
> 程序还支持自定义图片的大小需要在`./bin/core/length.py` 中修改 `make_html`函数的参数
### 基本信息
# 基本信息
- Python版本: `3.9.9`
- 请求方法: `GET` `POST`
- 请求地址: `/API`
@@ -20,18 +28,13 @@
- 可选参数: `theme` : `str`
### 注意事项
# 注意事项
- Python版能最新版就最新版
- 项目使用了标准库`Sqlite3`进行了数据库操作某些免费部署的服务器可能不支持
- 项目使用了标准库`Sqlite3`进行了数据库操作某些可以部署项目的服务器可能不支持例如`vercel`(可能是我不会)
### 开源
- 本项目以Apache-2.0许可开源, 即:
- 你可以直接使用该项目提供的功能, 无需任何授权
- 你可以在**注明来源版权信息**的情况下对源代码进行任意分发和修改以及衍生
### 快速响应:
# 快速响应
测试用代码如下
```python
import requests
@@ -146,4 +149,14 @@ for i in range(100):
17557<br>
16587<br>
</details>
<br>
# 关于
## 开源
- 本项目以Apache-2.0许可开源, 即:
- 你可以直接使用该项目提供的功能, 无需任何授权
- 你可以在**注明来源版权信息**的情况下对源代码进行任意分发和修改以及衍生
## 关于此页面
此文档由 [docsify](https://github.com/docsifyjs/docsify) 生成. docsify 是一个动态生成文档网站的工具。不同于 GitBook、Hexo 的地方是它不会生成将 .md 转成 .html 文件,所有转换工作都是在运行时进行。
+13
View File
@@ -0,0 +1,13 @@
![logo](./icon.svg)
# RequestCounter
> Python版的计数器
> 使用Python Flask 库完成的访问的次数计数器
* 响应速度快
* 使用Sqlite3数据库操作
[GitHub](https://github.com/MarkusJoe/RequestCounter.git)
[Get Started](#部署)
+23
View File
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="description" content="Description">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/docsify@4/lib/themes/vue.css">
</head>
<body>
<div id="app"></div>
<script>
window.$docsify = {
name: 'RequestCounter',
repo: 'https://github.com/MarkusJoe/RequestCounter',
coverpage: true
}
</script>
<!-- Docsify v4 -->
<script src="//cdn.jsdelivr.net/npm/docsify@4"></script>
</body>
</html>
+2 -2
View File
@@ -16,9 +16,9 @@
<div class="intro">
<br>
<br>
<i>看到这句话就说明程序正常运行啦!! <a href="https://markusjoe.github.io/counterdocs/">文档</a></i>
<i>看到这句话就说明程序正常运行啦!! <a href="https://markusjoe.github.io/RequestCounter/">文档</a></i>
<br>
<i>本服务使用Python-Flask开发(Flask 官方地址:<a href="https://flask.palletsprojects.com/en/2.0.x/">Flask</a> )</i>
<i>本服务使用Python-Flask开发(Flask 官方地址:<a href="https://flask.palletsprojects.com/en/2.0.x/">Flask</a>)</i>
<br>
<i>调用示例: <a href="http://127.0.0.1:5000/API?name=main">127.0.0.1:5000/API?name=main</a> 注: 调用支持POST 和 GET 方法请求</i>
<br>
-15
View File
@@ -1,15 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<svg width="450" height="50" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>Flask Counter - {{ title }}</title>
<g>
<image x="0" y="0" width="45" height="50" xlink:href="{{ svg_img_0 }}"></image>
<image x="45" y="0" width="45" height="50" xlink:href="{{ svg_img_1 }}"></image>
<image x="90" y="0" width="45" height="50" xlink:href="{{ svg_img_2 }}"></image>
<image x="135" y="0" width="45" height="50" xlink:href="{{ svg_img_3 }}"></image>
<image x="180" y="0" width="45" height="50" xlink:href="{{ svg_img_4 }}"></image>
<image x="225" y="0" width="45" height="50" xlink:href="{{ svg_img_5 }}"></image>
<image x="270" y="0" width="45" height="50" xlink:href="{{ svg_img_6 }}"></image>
<image x="315" y="0" width="45" height="50" xlink:href="{{ svg_img_7 }}"></image>
<image x="360" y="0" width="45" height="50" xlink:href="{{ svg_img_8 }}"></image>
<image x="405" y="0" width="45" height="50" xlink:href="{{ svg_img_9 }}"></image>
</g></svg>

Before

Width:  |  Height:  |  Size: 1.0 KiB

+16
View File
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<svg width="680" height="150" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>{{ title }}</title>
<g>
<image x="0" y="0" width="68" height="150" xlink:href="{{ svg_img_0 }}"/>
<image x="68" y="0" width="68" height="150" xlink:href="{{ svg_img_1 }}"/>
<image x="136" y="0" width="68" height="150" xlink:href="{{ svg_img_2 }}"/>
<image x="204" y="0" width="68" height="150" xlink:href="{{ svg_img_3 }}"/>
<image x="272" y="0" width="68" height="150" xlink:href="{{ svg_img_4 }}"/>
<image x="340" y="0" width="68" height="150" xlink:href="{{ svg_img_5 }}"/>
<image x="408" y="0" width="68" height="150" xlink:href="{{ svg_img_6 }}"/>
<image x="476" y="0" width="68" height="150" xlink:href="{{ svg_img_7 }}"/>
<image x="544" y="0" width="68" height="150" xlink:href="{{ svg_img_8 }}"/>
<image x="612" y="0" width="68" height="150" xlink:href="{{ svg_img_9 }}"/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 1.0 KiB

+16
View File
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<svg width="450" height="100" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>{{ title }}</title>
<g>
<image x="0" y="0" width="45" height="100" xlink:href="{{ svg_img_0 }}"/>
<image x="45" y="0" width="45" height="100" xlink:href="{{ svg_img_1 }}"/>
<image x="90" y="0" width="45" height="100" xlink:href="{{ svg_img_2 }}"/>
<image x="135" y="0" width="45" height="100" xlink:href="{{ svg_img_3 }}"/>
<image x="180" y="0" width="45" height="100" xlink:href="{{ svg_img_4 }}"/>
<image x="225" y="0" width="45" height="100" xlink:href="{{ svg_img_5 }}"/>
<image x="270" y="0" width="45" height="100" xlink:href="{{ svg_img_6 }}"/>
<image x="315" y="0" width="45" height="100" xlink:href="{{ svg_img_7 }}"/>
<image x="360" y="0" width="45" height="100" xlink:href="{{ svg_img_8 }}"/>
<image x="405" y="0" width="45" height="100" xlink:href="{{ svg_img_9 }}"/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 1.0 KiB

+13
View File
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<svg width="476" height="150" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>{{ title }}</title>
<g>
<image x="0" y="0" width="68" height="150" xlink:href="{{ svg_img_0 }}"/>
<image x="68" y="0" width="68" height="150" xlink:href="{{ svg_img_1 }}"/>
<image x="136" y="0" width="68" height="150" xlink:href="{{ svg_img_2 }}"/>
<image x="204" y="0" width="68" height="150" xlink:href="{{ svg_img_3 }}"/>
<image x="272" y="0" width="68" height="150" xlink:href="{{ svg_img_4 }}"/>
<image x="340" y="0" width="68" height="150" xlink:href="{{ svg_img_5 }}"/>
<image x="408" y="0" width="68" height="150" xlink:href="{{ svg_img_6 }}"/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 801 B

+13
View File
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<svg width="315" height="100" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>{{ title }}</title>
<g>
<image x="0" y="0" width="45" height="100" xlink:href="{{ svg_img_0 }}"/>
<image x="45" y="0" width="45" height="100" xlink:href="{{ svg_img_1 }}"/>
<image x="90" y="0" width="45" height="100" xlink:href="{{ svg_img_2 }}"/>
<image x="135" y="0" width="45" height="100" xlink:href="{{ svg_img_3 }}"/>
<image x="180" y="0" width="45" height="100" xlink:href="{{ svg_img_4 }}"/>
<image x="225" y="0" width="45" height="100" xlink:href="{{ svg_img_5 }}"/>
<image x="270" y="0" width="45" height="100" xlink:href="{{ svg_img_6 }}"/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 801 B

+14
View File
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<svg width="544" height="150" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>{{ title }}</title>
<g>
<image x="0" y="0" width="68" height="150" xlink:href="{{ svg_img_0 }}"/>
<image x="68" y="0" width="68" height="150" xlink:href="{{ svg_img_1 }}"/>
<image x="136" y="0" width="68" height="150" xlink:href="{{ svg_img_2 }}"/>
<image x="204" y="0" width="68" height="150" xlink:href="{{ svg_img_3 }}"/>
<image x="272" y="0" width="68" height="150" xlink:href="{{ svg_img_4 }}"/>
<image x="340" y="0" width="68" height="150" xlink:href="{{ svg_img_5 }}"/>
<image x="408" y="0" width="68" height="150" xlink:href="{{ svg_img_6 }}"/>
<image x="476" y="0" width="68" height="150" xlink:href="{{ svg_img_7 }}"/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 885 B

+14
View File
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<svg width="360" height="100" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>{{ title }}</title>
<g>
<image x="0" y="0" width="45" height="100" xlink:href="{{ svg_img_0 }}"/>
<image x="45" y="0" width="45" height="100" xlink:href="{{ svg_img_1 }}"/>
<image x="90" y="0" width="45" height="100" xlink:href="{{ svg_img_2 }}"/>
<image x="135" y="0" width="45" height="100" xlink:href="{{ svg_img_3 }}"/>
<image x="180" y="0" width="45" height="100" xlink:href="{{ svg_img_4 }}"/>
<image x="225" y="0" width="45" height="100" xlink:href="{{ svg_img_5 }}"/>
<image x="270" y="0" width="45" height="100" xlink:href="{{ svg_img_6 }}"/>
<image x="315" y="0" width="45" height="100" xlink:href="{{ svg_img_7 }}"/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 885 B

+15
View File
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<svg width="612" height="150" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>{{ title }}</title>
<g>
<image x="0" y="0" width="68" height="150" xlink:href="{{ svg_img_0 }}"/>
<image x="68" y="0" width="68" height="150" xlink:href="{{ svg_img_1 }}"/>
<image x="136" y="0" width="68" height="150" xlink:href="{{ svg_img_2 }}"/>
<image x="204" y="0" width="68" height="150" xlink:href="{{ svg_img_3 }}"/>
<image x="272" y="0" width="68" height="150" xlink:href="{{ svg_img_4 }}"/>
<image x="340" y="0" width="68" height="150" xlink:href="{{ svg_img_5 }}"/>
<image x="408" y="0" width="68" height="150" xlink:href="{{ svg_img_6 }}"/>
<image x="476" y="0" width="68" height="150" xlink:href="{{ svg_img_7 }}"/>
<image x="544" y="0" width="68" height="150" xlink:href="{{ svg_img_8 }}"/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 969 B

+15
View File
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<svg width="405" height="100" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>{{ title }}</title>
<g>
<image x="0" y="0" width="45" height="100" xlink:href="{{ svg_img_0 }}"/>
<image x="45" y="0" width="45" height="100" xlink:href="{{ svg_img_1 }}"/>
<image x="90" y="0" width="45" height="100" xlink:href="{{ svg_img_2 }}"/>
<image x="135" y="0" width="45" height="100" xlink:href="{{ svg_img_3 }}"/>
<image x="180" y="0" width="45" height="100" xlink:href="{{ svg_img_4 }}"/>
<image x="225" y="0" width="45" height="100" xlink:href="{{ svg_img_5 }}"/>
<image x="270" y="0" width="45" height="100" xlink:href="{{ svg_img_6 }}"/>
<image x="315" y="0" width="45" height="100" xlink:href="{{ svg_img_7 }}"/>
<image x="360" y="0" width="45" height="100" xlink:href="{{ svg_img_8 }}"/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 969 B