From cddd01c6ad923b77186f700486c240d64c445298 Mon Sep 17 00:00:00 2001 From: RTAkland Date: Fri, 30 Dec 2022 16:41:18 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E4=BF=AE=E5=A4=8D=E4=BA=86=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E5=85=A8=E9=83=A8=E6=95=B0=E6=8D=AE=E6=97=B6Internal?= =?UTF-8?q?=20Server=20Error=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/db/db.py | 66 ++++++++++++++++++++++++++++--------------- src/tests/API.http | 27 ------------------ src/tests/__init__.py | 9 ------ 3 files changed, 43 insertions(+), 59 deletions(-) delete mode 100644 src/tests/API.http delete mode 100644 src/tests/__init__.py diff --git a/src/db/db.py b/src/db/db.py index e2ad871..53e277d 100644 --- a/src/db/db.py +++ b/src/db/db.py @@ -67,11 +67,11 @@ class MySQL(BaseSQL): def __init__(self): super().__init__() _CONFIG = Config.database.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] + 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, @@ -84,44 +84,51 @@ class MySQL(BaseSQL): class DetaBase: def __init__(self): self.__deta = Deta(os.getenv("PJ_DETA")) - self.__data = self.__deta.Base("times") - self.__image = self.__deta.Base("images") + self.__data = self.__deta.AsyncBase("times") + self.__image = self.__deta.AsyncBase("images") async def __get(self, _id: str) -> tuple: - result = self.__data.get(_id) + 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: - self.__data.put({"times": 0}, _id) + 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} - self.__data.update(new, _id) + 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 = self.__image.get(theme) - try: - for i in result: - if i != "key": - response.append(tuple([ - i, - result[i]["base64"], - result[i]["width"], - result[i]["height"] - ])) - except TypeError: - return [] + 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 async def query(self, _id: str) -> tuple: @@ -137,7 +144,20 @@ class DetaBase: return True async def query_all(self) -> list: - pass + 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 async def query_image(self, theme: str) -> list: result = await self.__get_images(theme) diff --git a/src/tests/API.http b/src/tests/API.http deleted file mode 100644 index 9a93324..0000000 --- a/src/tests/API.http +++ /dev/null @@ -1,27 +0,0 @@ -GET http://localhost:8000/api/query-theme/blacked -Accept: application/json - -### - -GET http://localhost:8000/api/query-theme/lisu -Accept: application/json - -### - -GET http://localhost:8000/api/query-theme/moebooru -Accept: application/json - -### - -GET http://localhost:8000/api/query-theme/lewd -Accept: application/json - -### - -GET http://localhost:8000/api/query/test -Accept: application/json - -### - - - diff --git a/src/tests/__init__.py b/src/tests/__init__.py deleted file mode 100644 index f1dcc0b..0000000 --- a/src/tests/__init__.py +++ /dev/null @@ -1,9 +0,0 @@ -#!/usr/bin/env python3 -# -- coding:utf-8 -- -# @Author: markushammered@gmail.com -# @Development Tool: PyCharm -# @Create Time: 2022/8/30 -# @File Name: __init__.py - - -__all__ = []