diff options
author | Kyomotoi <[email protected]> | 2022-06-13 19:37:34 +0800 |
---|---|---|
committer | Kyomotoi <[email protected]> | 2022-06-13 19:37:34 +0800 |
commit | 59c923bbc3e91ad5a3482ec81c9192297ebbebe8 (patch) | |
tree | d74da226542a8b6f54200bd6d55e8d459315c71d /ATRI/plugins/bilibili_dynamic/database/db.py | |
parent | 4a876e2e64a690dcf01c385e273052e1e22caacd (diff) | |
download | ATRI-59c923bbc3e91ad5a3482ec81c9192297ebbebe8.tar.gz ATRI-59c923bbc3e91ad5a3482ec81c9192297ebbebe8.tar.bz2 ATRI-59c923bbc3e91ad5a3482ec81c9192297ebbebe8.zip |
♻️ 重构插件: b站动态订阅
Diffstat (limited to 'ATRI/plugins/bilibili_dynamic/database/db.py')
-rw-r--r-- | ATRI/plugins/bilibili_dynamic/database/db.py | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/ATRI/plugins/bilibili_dynamic/database/db.py b/ATRI/plugins/bilibili_dynamic/database/db.py new file mode 100644 index 0000000..37ed223 --- /dev/null +++ b/ATRI/plugins/bilibili_dynamic/database/db.py @@ -0,0 +1,49 @@ +from pathlib import Path +from tortoise import Tortoise + +from ATRI import driver +from .models import Subscription + + +DB_DIR = Path(".") / "data" / "database" / "bilibili_dynamic" +DB_DIR.mkdir(parents=True, exist_ok=True) + + +class DB: + async def __aenter__(self): + return self + + async def __aexit__(self, exc_type, exc_val, exc_tb): + pass + + async def init(self): + from . import models + + await Tortoise.init( + db_url=f"sqlite://{DB_DIR}/db.sqlite3", + modules={"models": [locals()["models"]]}, + ) + await Tortoise.generate_schemas() + + async def add_sub(self, uid: int, group_id: int): + await Subscription.create(uid=uid, group_id=group_id) + + async def update_sub(self, uid: int, update_map: dict): + await Subscription.filter(uid=uid).update(**update_map) + + async def del_sub(self, query_map: dict): + await Subscription.filter(**query_map).delete() + + async def get_sub_list(self, query_map: dict) -> list: + return await Subscription.filter(**query_map) + + async def get_all_subs(self) -> list: + return await Subscription.all() + + +async def init(): + async with DB() as db: + await db.init() + + +driver().on_startup(init) |