From 81f6d590eb1e09be64262efbf81364af218ad014 Mon Sep 17 00:00:00 2001 From: RTAkland Date: Thu, 29 Dec 2022 15:47:22 +0800 Subject: [PATCH] =?UTF-8?q?feature:=20=E6=B7=BB=E5=8A=A0=E4=BA=86Deta=20Ba?= =?UTF-8?q?se=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/db/__init__.py | 10 +++---- src/db/db.py | 71 +++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 71 insertions(+), 10 deletions(-) diff --git a/src/db/__init__.py b/src/db/__init__.py index c820241..0add4cf 100644 --- a/src/db/__init__.py +++ b/src/db/__init__.py @@ -20,16 +20,14 @@ def download_file(path: str): print('Download database file successfully.') -if Config.DETA and Config.database == 'sqlite3': - if not os.path.exists('/tmp/data.sqlite'): - download_file('/tmp/data.sqlite') -elif not Config.DETA and Config.database == 'sqlite3': +if database == 'sqlite3': if not os.path.exists('./src/db/data.sqlite'): download_file('./src/db/data.sqlite') -if Config.database == 'sqlite3': +if database == 'sqlite3': from src.db.db import SQLite as Database - +elif database == 'deta': + from src.db.db import DetaBase as Database else: from src.db.db import MySQL as Database __all__ = [Database] diff --git a/src/db/db.py b/src/db/db.py index ad88921..e2ad871 100644 --- a/src/db/db.py +++ b/src/db/db.py @@ -6,10 +6,13 @@ # @File Name: db.py +import os from src.config import Config if Config.database == 'sqlite3': import sqlite3 as operator +elif Config.database == 'deta': + from deta import Deta else: import pymysql as operator @@ -56,10 +59,7 @@ class BaseSQL: class SQLite(BaseSQL): def __init__(self): super().__init__() - if Config.DETA: - self.conn = operator.connect('/tmp/data.sqlite') - else: - self.conn = operator.connect('./src/db/data.sqlite') + self.conn = operator.connect('./src/db/data.sqlite') self.cursor = self.conn.cursor() @@ -79,3 +79,66 @@ class MySQL(BaseSQL): 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.Base("times") + self.__image = self.__deta.Base("images") + + async def __get(self, _id: str) -> tuple: + result = self.__data.get(_id) + if result is None: + await self.__put_data(_id) + return tuple([_id, 0]) + await self.update(_id, result["times"]) + return tuple([_id, result["times"]]) + + async def __put_data(self, _id: str) -> bool: + self.__data.put({"times": 0}, _id) + return True + + async def __update_data(self, _id: str, times: int) -> bool: + new = {"times": times + 1} + self.__data.update(new, _id) + return True + + async def __insert_data(self, _id: str) -> bool: + await self.__put_data(_id) + 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 [] + return response + + async def query(self, _id: str) -> tuple: + result = await self.__get(_id) + return result + + async def insert(self, _id: str) -> bool: + await self.__insert_data(_id) + return True + + async def update(self, _id: str, times: int) -> bool: + await self.__update_data(_id, times) + return True + + async def query_all(self) -> list: + pass + + async def query_image(self, theme: str) -> list: + result = await self.__get_images(theme) + return result