Archived
update: 将代码中的单引号更换为双引号
This commit is contained in:
8 files changed
+67
-67
No files matched your search
@@ -11,5 +11,5 @@ from src import create_app
|
||||
|
||||
app = create_app()
|
||||
|
||||
if __name__ == '__main__':
|
||||
uvicorn.run(app, host='0.0.0.0')
|
||||
if __name__ == "__main__":
|
||||
uvicorn.run(app, host="0.0.0.0")
|
||||
+1
-1
@@ -5,4 +5,4 @@ aiofiles
|
||||
requests~=2.28.1
|
||||
PyMySQL
|
||||
cryptography
|
||||
deta
|
||||
deta[async]==1.1.0a2
|
||||
+1
-1
@@ -18,5 +18,5 @@ def create_app():
|
||||
"""
|
||||
app = FastAPI()
|
||||
app.include_router(main)
|
||||
app.include_router(api, prefix='/api')
|
||||
app.include_router(api, prefix="/api")
|
||||
return app
|
||||
+28
-28
@@ -15,62 +15,62 @@ from fastapi.responses import JSONResponse
|
||||
|
||||
|
||||
async def _time():
|
||||
return time.time()
|
||||
return str(time.time()).split(".")[0]
|
||||
|
||||
|
||||
@api.get('/query/times/{name}')
|
||||
@api.get("/query/times/{name}")
|
||||
async def query(name: str):
|
||||
data = await Database().query(name)
|
||||
name = data[0]
|
||||
times = data[1]
|
||||
response = {
|
||||
'code': 200,
|
||||
'time': await _time(),
|
||||
'data': {
|
||||
'name': name,
|
||||
'times': times
|
||||
"code": 200,
|
||||
"time": await _time(),
|
||||
"data": {
|
||||
"name": name,
|
||||
"times": times
|
||||
}
|
||||
}
|
||||
return response
|
||||
|
||||
|
||||
@api.get('/query/alldata')
|
||||
@api.get("/query/alldata")
|
||||
async def query_all(limit: int = 30):
|
||||
result = await Database().query_all()
|
||||
data = result[:limit]
|
||||
response = {
|
||||
'code': 200,
|
||||
'time': await _time(),
|
||||
'data': []
|
||||
"code": 200,
|
||||
"time": await _time(),
|
||||
"data": []
|
||||
}
|
||||
data = result[:limit]
|
||||
for i in data:
|
||||
response['data'].append({
|
||||
'name': i[0],
|
||||
'times': i[1]
|
||||
response["data"].append({
|
||||
"name": i[0],
|
||||
"times": i[1]
|
||||
})
|
||||
return response
|
||||
|
||||
|
||||
@api.get('/export')
|
||||
@api.get("/export")
|
||||
async def export():
|
||||
if os.path.exists('/tmp/data.sqlite'):
|
||||
return FileResponse('/tmp/data.sqlite')
|
||||
return FileResponse('./src/db/data.sqlite')
|
||||
if os.path.exists("/tmp/data.sqlite"):
|
||||
return FileResponse("/tmp/data.sqlite")
|
||||
return FileResponse("./src/db/data.sqlite")
|
||||
|
||||
|
||||
@api.get('/query/theme/{name}')
|
||||
@api.get("/query/theme/{name}")
|
||||
async def query_theme(name: str):
|
||||
data = await Database().query_image(name)
|
||||
response = {
|
||||
'code': 200,
|
||||
'time': await _time(),
|
||||
'data': []
|
||||
"code": 200,
|
||||
"time": await _time(),
|
||||
"data": []
|
||||
}
|
||||
for i in data:
|
||||
response['data'].append({
|
||||
'index': i[0],
|
||||
'image': i[1],
|
||||
'width': i[2],
|
||||
'height': i[3]
|
||||
response["data"].append({
|
||||
"index": i[0],
|
||||
"image": i[1],
|
||||
"width": i[2],
|
||||
"height": i[3]
|
||||
})
|
||||
return JSONResponse(response)
|
||||
+1
-1
@@ -14,6 +14,6 @@ class Config:
|
||||
mysql -> user:pwd@host:port/db
|
||||
deta -> deta
|
||||
"""
|
||||
database = os.getenv('COUNTER_DB') or "sqlite3" # 数据库类型
|
||||
database = os.getenv("COUNTER_DB") or "sqlite3" # 数据库类型
|
||||
if os.getenv("PJ_DETA") is not None:
|
||||
database = "deta" # 自动设置为deta
|
||||
+8
-8
@@ -14,19 +14,19 @@ database = Config.database # operator
|
||||
|
||||
|
||||
def download_file(path: str):
|
||||
print('Downloading database file. Please wait...')
|
||||
file_url = 'https://static.rtast.cn/data.sqlite'
|
||||
print("Downloading database file. Please wait...")
|
||||
file_url = "https://static.rtast.cn/data.sqlite"
|
||||
urlretrieve(file_url, path) # standard lib for downloading file
|
||||
print('Download database file successfully.')
|
||||
print("Download database file successfully.")
|
||||
|
||||
|
||||
if database == 'sqlite3':
|
||||
if not os.path.exists('./src/db/data.sqlite'):
|
||||
download_file('./src/db/data.sqlite')
|
||||
if database == "sqlite3":
|
||||
if not os.path.exists("./src/db/data.sqlite"):
|
||||
download_file("./src/db/data.sqlite")
|
||||
|
||||
if database == 'sqlite3':
|
||||
if database == "sqlite3":
|
||||
from src.db.db import SQLite as Database
|
||||
elif database == 'deta':
|
||||
elif database == "deta":
|
||||
from src.db.db import DetaBase as Database
|
||||
else:
|
||||
from src.db.db import MySQL as Database
|
||||
|
||||
+14
-14
@@ -13,28 +13,28 @@ from fastapi.responses import RedirectResponse
|
||||
from fastapi.responses import FileResponse
|
||||
from fastapi.templating import Jinja2Templates
|
||||
|
||||
template = Jinja2Templates('./src/templates')
|
||||
template = Jinja2Templates("./src/templates")
|
||||
|
||||
|
||||
@main.get('/favicon.ico')
|
||||
@main.get("/favicon.ico")
|
||||
async def favicon():
|
||||
return FileResponse('./src/static/favicon.ico')
|
||||
return FileResponse("./src/static/favicon.ico")
|
||||
|
||||
|
||||
@main.get('/')
|
||||
@main.get("/")
|
||||
async def redirect_index():
|
||||
return RedirectResponse('/_redirect')
|
||||
return RedirectResponse("/_redirect")
|
||||
|
||||
|
||||
@main.get('/{name}')
|
||||
async def index(req: Request, name: str, length: int = 7, theme: str = 'lewd'):
|
||||
@main.get("/{name}")
|
||||
async def index(req: Request, name: str, length: int = 7, theme: str = "lewd"):
|
||||
if length > 10:
|
||||
return {'code': -200, 'msg': 'Length Error'}
|
||||
return {"code": -200, "msg": "Length Error"}
|
||||
response = await resp(name, length, theme)
|
||||
|
||||
return template.TemplateResponse('index.html',
|
||||
context={'request': req,
|
||||
'context': response['context'],
|
||||
'g_width': response['g_width'],
|
||||
'g_height': response['g_height']},
|
||||
headers=response['headers'])
|
||||
return template.TemplateResponse("index.html",
|
||||
context={"request": req,
|
||||
"context": response["context"],
|
||||
"g_width": response["g_width"],
|
||||
"g_height": response["g_height"]},
|
||||
headers=response["headers"])
|
||||
+12
-12
@@ -9,7 +9,7 @@
|
||||
from src.db import Database
|
||||
|
||||
|
||||
async def resp(_id: str, length: int = 7, theme: str = 'lewd') -> dict:
|
||||
async def resp(_id: str, length: int = 7, theme: str = "lewd") -> dict:
|
||||
"""
|
||||
generate dict response
|
||||
include all information
|
||||
@@ -22,27 +22,27 @@ async def resp(_id: str, length: int = 7, theme: str = 'lewd') -> dict:
|
||||
times = result[1]
|
||||
str_number = str(times) # 将整形转换为字符串
|
||||
len_number = len(str_number) # 再获取字符串长度
|
||||
g_length = length * '0' # 根据输入的位数来生成0的数量
|
||||
g_length = length * "0" # 根据输入的位数来生成0的数量
|
||||
show_number = str(g_length[:-len_number] + str_number)
|
||||
context = []
|
||||
headers = {'cache-control': 'max-age=0, no-cache, no-store, must-revalidate',
|
||||
'Content-Type': 'image/svg+xml; charset=utf-8'}
|
||||
headers = {"cache-control": "max-age=0, no-cache, no-store, must-revalidate",
|
||||
"Content-Type": "image/svg+xml; charset=utf-8"}
|
||||
data = await Database().query_image(theme)
|
||||
height = data[0][-1]
|
||||
width = data[0][-2]
|
||||
counter = 0
|
||||
for i, n in zip(data, show_number):
|
||||
context.append({
|
||||
'position': i[-2] * counter,
|
||||
'width': i[-2],
|
||||
'height': i[-1],
|
||||
'base64': data[int(n)][1]
|
||||
"position": i[-2] * counter,
|
||||
"width": i[-2],
|
||||
"height": i[-1],
|
||||
"base64": data[int(n)][1]
|
||||
})
|
||||
counter += 1
|
||||
|
||||
return {
|
||||
'context': context,
|
||||
'g_width': length * width,
|
||||
'g_height': height,
|
||||
'headers': headers
|
||||
"context": context,
|
||||
"g_width": length * width,
|
||||
"g_height": height,
|
||||
"headers": headers
|
||||
}
|
||||
Reference in New Issue
Block a user