From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on gnuweeb.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NO_DNS_FOR_FROM,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 Received: from localhost.localdomain (unknown [101.128.125.157]) by gnuweeb.org (Postfix) with ESMTPSA id E30327E257; Sat, 30 Jul 2022 04:40:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1659156047; bh=N+nvS3/yHWBCmzlxk3HFmcQXBfwsqbzP9s0tchMond0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iLwhGcdVuzcShwxloPiI7l+56wqbpruvVnqndyzkN88OiXDfdCifZ9KzaLHYe/Nne waLG+eImyLghU+3e3HhxF8+DX0TJ5GvTj4H7uIdNp3CRtjAz4WvDPkmsNk2LFQQ6bm lVoyBZ95kA5GKj73TjKA7pArN6qmiq1QC/91A6hWbSLSYpLfo/qWdfb3MBJl+VV9fB m4BDTYyDZ2OhfV9e2WpgmiFv3e3gfV8O5cNXR9FL9z7Cu1YkeAhzKdup1E2dE8goPW /PYCp3rtGBikQvuCSq43jPct6YN7dYjnxLW323acACJAcN6166Lvc2eunHwRKFK1nk TqoV0h7bVxH5w== From: Muhammad Rizki To: Ammar Faizi Cc: Muhammad Rizki , GNU/Weeb Mailing List , Alviro Iskandar Setiawan Subject: [PATCH v4 06/18] Refactor database methods Date: Sat, 30 Jul 2022 11:40:04 +0700 Message-Id: <20220730044016.988-7-kiizuha@gnuweeb.org> X-Mailer: git-send-email 2.34.1.windows.1 In-Reply-To: <20220730044016.988-1-kiizuha@gnuweeb.org> References: <20220730044016.988-1-kiizuha@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: I decided to refactor the database methods to make it able to access with DaemonClient, so in that client I just to call one of the database function just like: async def add_atom(c: DaemonClient, m: Message): c.db.insert_atom("atom_url_here") The c here is the DaemonClient parameter in the add_atom MessageHandler function Signed-off-by: Muhammad Rizki --- daemon/packages/client.py | 4 +++- daemon/run.py | 16 ++++++++-------- daemon/scraper/bot.py | 5 ++--- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/daemon/packages/client.py b/daemon/packages/client.py index d16fe46..282daf6 100644 --- a/daemon/packages/client.py +++ b/daemon/packages/client.py @@ -9,14 +9,16 @@ from pyrogram.types import Message, InlineKeyboardMarkup, InlineKeyboardButton from typing import Union from email.message import Message from scraper import utils +from scraper.db import Db from .decorator import handle_flood class DaemonClient(Client): def __init__(self, name: str, api_id: int, - api_hash: str, **kwargs): + api_hash: str, conn, **kwargs): super().__init__(name, api_id, api_hash, **kwargs) + self.db = Db(conn) @handle_flood diff --git a/daemon/run.py b/daemon/run.py index 1151ccd..5360395 100644 --- a/daemon/run.py +++ b/daemon/run.py @@ -22,9 +22,15 @@ def main(): api_id=int(os.getenv("API_ID")), api_hash=os.getenv("API_HASH"), bot_token=os.getenv("BOT_TOKEN"), + conn=connector.connect( + host=os.getenv("DB_HOST"), + user=os.getenv("DB_USER"), + password=os.getenv("DB_PASS"), + database=os.getenv("DB_NAME") + ), plugins=dict( root="packages.plugins" - ) + ), ) sched = AsyncIOScheduler( @@ -38,13 +44,7 @@ def main(): client=client, sched=sched, scraper=Scraper(), - mutexes=BotMutexes(), - conn=connector.connect( - host=os.getenv("DB_HOST"), - user=os.getenv("DB_USER"), - password=os.getenv("DB_PASS"), - database=os.getenv("DB_NAME") - ) + mutexes=BotMutexes() ) sched.start() bot.run() diff --git a/daemon/scraper/bot.py b/daemon/scraper/bot.py index c475e34..7adfb12 100644 --- a/daemon/scraper/bot.py +++ b/daemon/scraper/bot.py @@ -8,7 +8,6 @@ from apscheduler.schedulers.asyncio import AsyncIOScheduler from packages import DaemonClient from scraper import Scraper from . import utils -from .db import Db import asyncio import shutil import re @@ -22,12 +21,12 @@ class BotMutexes(): class Bot(): def __init__(self, client: DaemonClient, sched: AsyncIOScheduler, - scraper: Scraper, mutexes: BotMutexes, conn): + scraper: Scraper, mutexes: BotMutexes): self.client = client self.sched = sched self.scraper = scraper self.mutexes = mutexes - self.db = Db(conn) + self.db = client.db self.isRunnerFixed = False -- Muhammad Rizki