support vercel kv db and remove mysql

This commit is contained in:
2024-06-21 13:25:09 +08:00
parent 01c7fb5666
commit b6a025ddef
5 files changed
+56 -110

No files matched your search

+22 -94
View File
@@ -4,17 +4,12 @@
# @Development Tool: PyCharm
# @Create Time: 2022/9/11
# @File Name: db.py
import requests
import os
from src.config import Config
if Config.database in ["sqlite3", "sqlite"]:
import sqlite3 as operator
elif Config.database == "deta":
from deta import Deta
else:
import pymysql as operator
class SQL:
@@ -63,102 +58,35 @@ class SQLite(SQL):
self.cursor = self.conn.cursor()
class MySQL(SQL):
def __init__(self):
super().__init__()
_CONFIG = Config.database.replace("mysql://", "").split("@")
user = _CONFIG[0].split(":")[0]
pwd = _CONFIG[0].split(":")[1]
host = _CONFIG[1].split(":")[0]
port = int(_CONFIG[1].split(":")[1].split("/")[0])
db = _CONFIG[1].split("/")[1]
self.conn = operator.connect(user=user,
passwd=pwd,
host=host,
port=port,
database=db)
self.cursor = self.conn.cursor()
class DetaBase:
def __init__(self):
self.__deta = Deta(os.getenv("PJ_DETA"))
self.__data = self.__deta.AsyncBase("times")
self.__image = self.__deta.AsyncBase("images")
async def __get(self, _id: str) -> tuple:
result = await self.__data.get(_id)
if result is None:
await self.__put_data(_id)
return tuple([_id, 0])
await self.update(_id, result["times"])
await self.__image.close()
await self.__data.close()
return tuple([_id, result["times"]])
async def __put_data(self, _id: str) -> bool:
await self.__data.put({"times": 0}, _id)
await self.__image.close()
await self.__data.close()
return True
async def __update_data(self, _id: str, times: int) -> bool:
new = {"times": times + 1}
await self.__data.update(new, _id)
await self.__image.close()
await self.__data.close()
return True
async def __insert_data(self, _id: str) -> bool:
await self.__put_data(_id)
await self.__image.close()
await self.__data.close()
return True
async def __get_images(self, theme: str) -> list:
response = []
result = await self.__image.get(theme)
for i in result:
if i != "key":
response.append(tuple([
i,
result[i]["base64"],
result[i]["width"],
result[i]["height"]
]))
await self.__image.close()
await self.__data.close()
return response
class VercelKV:
async def query(self, _id: str) -> tuple:
result = await self.__get(_id)
return result
times = requests.get(
f"{Config.vercel_kv_url}/get/{_id}",
headers={"Authorization": f"Bearer {Config.api_key}"}
).json()["result"]
if times is None:
await self.insert(_id)
return tuple([_id, 0])
await self.update(_id, int(times))
return tuple([_id, times])
async def insert(self, _id: str) -> bool:
await self.__insert_data(_id)
requests.get(
f"{Config.vercel_kv_url}/set/{_id}/1",
headers={"Authorization": f"Bearer {Config.api_key}"}
)
return True
async def update(self, _id: str, times: int) -> bool:
await self.__update_data(_id, times)
times += 1
requests.get(
f"{Config.vercel_kv_url}/set/{_id}/{times}",
headers={"Authorization": f"Bearer {Config.api_key}"}
)
return True
async def query_all(self) -> list:
res = await self.__data.fetch()
all_items = res.items
while res.last:
res = await self.__data.fetch(last=res.last)
all_items += res.items
await self.__image.close()
await self.__data.close()
result = []
for i in all_items:
result.append((
i["key"],
i["times"]
))
return result
return list("")
async def query_image(self, theme: str) -> list:
result = await self.__get_images(theme)
return result
return Config.themes[theme]