From 463e8bbec79f45167940d6be8b03a7ed3e84f3b3 Mon Sep 17 00:00:00 2001 From: miteruzo Date: Wed, 22 Oct 2025 23:25:49 +0900 Subject: [PATCH] =?UTF-8?q?config=20=E3=81=AB=E7=A7=BB=E3=81=97=E3=81=9F?= =?UTF-8?q?=EF=BC=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/config.py | 25 +++++++++++++++++++++++++ update_db.py | 32 ++++++++++++-------------------- 2 files changed, 37 insertions(+), 20 deletions(-) create mode 100644 db/config.py diff --git a/db/config.py b/db/config.py new file mode 100644 index 0000000..5ec62e6 --- /dev/null +++ b/db/config.py @@ -0,0 +1,25 @@ +from __future__ import annotations + +import os +from typing import TypedDict + +from eloquent import DatabaseManager, Model # type: ignore + +CONFIG: dict[str, DbConfig] = { 'mysql': { 'driver': 'mysql', + 'host': 'localhost', + 'database': 'nizika_nico', + 'user': os.environ['MYSQL_USER'], + 'password': os.environ['MYSQL_PASS'], + 'prefix': '' } } + +DB = DatabaseManager (CONFIG) +Model.set_connection_resolver (DB) + + +class DbConfig (TypedDict): + driver: str + host: str + database: str + user: str + password: str + prefix: str diff --git a/update_db.py b/update_db.py index fc529de..395b280 100644 --- a/update_db.py +++ b/update_db.py @@ -20,24 +20,25 @@ import jaconv import requests from eloquent import DatabaseManager, Model +from db.config import DB from db.models import Comment, Tag, User, Video, VideoHistory, VideoTag def main ( ) -> None: - config: dict[str, DbConfig] = { 'mysql': { 'driver': 'mysql', - 'host': 'localhost', - 'database': 'nizika_nico', - 'user': os.environ['MYSQL_USER'], - 'password': os.environ['MYSQL_PASS'], - 'prefix': '' } } - db = DatabaseManager (config) - Model.set_connection_resolver (db) - now = datetime.now () - api_data = search_nico_by_tags (['伊地知ニジカ', 'ぼざろクリーチャーシリーズ', 'ぼざろクリーチャーシリーズ外伝']) - update_tables (api_data, now) + api_data = search_nico_by_tags (['伊地知ニジカ', + 'ぼざろクリーチャーシリーズ', + 'ぼざろクリーチャーシリーズ外伝']) + + DB.begin_transaction () + try: + update_tables (api_data, now) + DB.commit () + except Exception: + DB.rollback () + raise def update_tables ( @@ -211,15 +212,6 @@ def search_nico_by_tags ( return result_data -class DbConfig (TypedDict): - driver: str - host: str - database: str - user: str - password: str - prefix: str - - class VideoSearchParam (TypedDict): q: str targets: str