This repository has been archived on 2026-08-27. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
MoeCounter/src/db/db.py
T

92 lines
2.6 KiB
Python
Raw Normal View History

2022-09-11 10:31:35 +08:00
#!/usr/bin/env python3
# -- coding:utf-8 --
# @Author: markushammered@gmail.com
# @Development Tool: PyCharm
# @Create Time: 2022/9/11
# @File Name: db.py
2024-06-21 13:25:09 +08:00
import requests
2022-09-11 10:31:35 +08:00
from src.config import Config
2023-01-07 20:56:43 +08:00
if Config.database in ["sqlite3", "sqlite"]:
import sqlite3 as operator
2022-09-11 10:31:35 +08:00
2023-01-07 20:56:43 +08:00
class SQL:
2022-09-11 10:31:35 +08:00
def __init__(self):
self.conn = None
self.cursor = None
def __del__(self):
self.conn.commit()
self.cursor.close()
self.conn.close()
async def query(self, _id: str) -> tuple:
2022-09-11 10:31:35 +08:00
self.cursor.execute('select * from data where id="%s";' % _id)
result = self.cursor.fetchone()
if result is None:
await self.insert(_id)
2022-09-11 10:31:35 +08:00
return tuple([_id, 0])
await self.update(_id, result[1])
2022-09-11 10:31:35 +08:00
return result
async def insert(self, _id: str) -> bool:
self.cursor.execute('insert into data (id, times) values ("%s", 1);' % _id)
2022-09-11 10:31:35 +08:00
return True
async def update(self, _id: str, times: int) -> bool:
2022-09-11 10:31:35 +08:00
times += 1
self.cursor.execute('update data set times=%s where id="%s";' % (times, _id))
return True
async def query_all(self) -> list:
2022-09-11 10:31:35 +08:00
self.cursor.execute('select * from data;')
result = self.cursor.fetchall()
return result
async def query_image(self, theme: str) -> list:
self.cursor.execute('select * from %s;' % theme)
2022-09-11 10:31:35 +08:00
result = self.cursor.fetchall()
return result
2023-01-07 20:56:43 +08:00
class SQLite(SQL):
2022-09-11 10:31:35 +08:00
def __init__(self):
super().__init__()
2022-12-29 15:47:22 +08:00
self.conn = operator.connect('./src/db/data.sqlite')
2022-09-11 10:31:35 +08:00
self.cursor = self.conn.cursor()
2024-06-21 13:25:09 +08:00
class VercelKV:
2022-12-29 15:47:22 +08:00
async def query(self, _id: str) -> tuple:
2024-06-21 13:25:09 +08:00
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])
2022-12-29 15:47:22 +08:00
async def insert(self, _id: str) -> bool:
2024-06-21 13:25:09 +08:00
requests.get(
f"{Config.vercel_kv_url}/set/{_id}/1",
headers={"Authorization": f"Bearer {Config.api_key}"}
)
2022-12-29 15:47:22 +08:00
return True
async def update(self, _id: str, times: int) -> bool:
2024-06-21 13:25:09 +08:00
times += 1
requests.get(
f"{Config.vercel_kv_url}/set/{_id}/{times}",
headers={"Authorization": f"Bearer {Config.api_key}"}
)
2022-12-29 15:47:22 +08:00
return True
async def query_all(self) -> list:
2024-06-21 13:25:09 +08:00
return list("")
2022-12-29 15:47:22 +08:00
async def query_image(self, theme: str) -> list:
2024-06-21 13:25:09 +08:00
return Config.themes[theme]