From d415790da16ef6f5069a1c302a11bb2a4ef50ddd Mon Sep 17 00:00:00 2001 From: MarkusJoe Date: Tue, 4 Oct 2022 15:33:52 +0800 Subject: [PATCH] =?UTF-8?q?rm:=20=E5=9C=A8main=E5=88=86=E6=94=AF=E4=B8=AD?= =?UTF-8?q?=E5=8F=96=E6=B6=88=E4=BA=86=E5=AF=B9Redis=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E7=9A=84=E6=94=AF=E6=8C=81,=20=E5=B0=86=E6=AD=A4?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E7=A7=BB=E8=87=B3Redis=E5=88=86=E6=94=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/view.py | 4 ---- src/db/__init__.py | 18 ++++++-------- src/db/db.py | 58 ---------------------------------------------- 3 files changed, 7 insertions(+), 73 deletions(-) diff --git a/src/api/view.py b/src/api/view.py index 2132572..8622112 100644 --- a/src/api/view.py +++ b/src/api/view.py @@ -6,11 +6,9 @@ # @File Name: view.py -import os from src.api import api from src.db import Database from fastapi.responses import FileResponse -from fastapi.responses import JSONResponse @api.get('/query/{name}') @@ -21,8 +19,6 @@ async def query(name: str): @api.get('/query-all/') async def query_all(limit: int = 30): - if os.getenv('c_not_full'): - return JSONResponse({'code': -200, 'msg': 'Redis Database do not support this action'}) data = Database().query_all()[:limit] result = {} for i in data: diff --git a/src/db/__init__.py b/src/db/__init__.py index 0b0c34b..676a754 100644 --- a/src/db/__init__.py +++ b/src/db/__init__.py @@ -7,21 +7,17 @@ import os +import sqlite3 as operator from urllib.request import urlretrieve from src.config import Config +from src.db.db import SQLite as Database database = Config.database -if database == 'sqlite': - import sqlite3 as operator +if not os.path.exists('./src/db/data.sqlite'): + print('Downloading database file. Please wait...') + file_url = 'https://static.rtast.cn/data.sqlite' + urlretrieve(file_url, './src/db/data.sqlite') + print('Download database file successfully.') - if not os.path.exists('./src/db/data.sqlite'): - print('Downloading database file. Please wait...') - file_url = 'https://static.rtast.cn/data.sqlite' - urlretrieve(file_url, './src/db/data.sqlite') - print('Download database file successfully.') - from src.db.db import SQLite as Database -elif 'redis' in database: - import redis as operator - from src.db.db import Redis as Database __all__ = [Database, operator] diff --git a/src/db/db.py b/src/db/db.py index 8a7dc6d..500f839 100644 --- a/src/db/db.py +++ b/src/db/db.py @@ -54,61 +54,3 @@ class SQLite(Base): super().__init__() self.conn = operator.connect('./src/db/data.sqlite') self.cursor = self.conn.cursor() - - -class Redis(Base): - def __init__(self): - super().__init__() - self.host: str = os.getenv('c_host') - self.port: int = int(os.getenv('c_port')) - self.password: str = os.getenv('c_password') # if is None then it is None. lol - self.conn = operator.Redis(host=self.host, - port=self.port, - password=self.password, - decode_responses=True) - - def __del__(self): - self.conn.close() - - def query(self, _id: str) -> tuple: - return self._get(_id) - - def insert(self, _id: str) -> bool: - return self._set(_id) - - def update(self, _id: str, times: int) -> bool: - return self._set(_id, times) - - def query_all(self) -> list: - return self._get_all() - - def query_image(self, theme: str) -> list: - return self._get_image(theme) - - def _get(self, _id: str) -> tuple: - result = self.conn.get(_id) - if result is None: - self._set(_id) - return tuple([_id, 0]) - self._set(_id, int(result)) - return tuple([_id, int(result)]) - - def _set(self, _id: str, times: int = 0) -> bool: - times += 1 - return self.conn.set(_id, times) - - def _get_all(self) -> list: - results = self.conn.keys() - return results - - def _get_image(self, theme: str) -> list: - images = [] - for i in range(10): - images.append([i, self.conn.get(f'{theme}-{i}')]) - width = int(self.conn.get(f'{theme}-width')) - height = int(self.conn.get(f'{theme}-height')) - for j in range(10): - images[j].append(width) - images[j].append(height) - - return images