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.94]) by gnuweeb.org (Postfix) with ESMTPSA id 87C6C80724; Fri, 29 Jul 2022 00:45:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1659055548; bh=N+nvS3/yHWBCmzlxk3HFmcQXBfwsqbzP9s0tchMond0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VvRNV8PAG/1gnngTspaftigZYzDqIcDaFybCLqIaU2Q03q9DR8rqgPu6T4Eay3Zm9 YkydLMrXHuh7G7BlCWy4zm/KIesPtWXlSmHATJLxVxEW08PKLdT/fHJIEbovwjH+79 p2qN7Wtvkxc4X7ZOh0RXOxJS9j+zWGKyiUfqkXGV7abhj+hx7kmrcLklJwT80wRL1x n+HLSZvUfImkyFRnKLiJsHMr42I0fh4h1HiBqrd27egwSmMHDf59NxsornwoujBmAr hVH6fxA8Hd1NS5/uPtFNih8PUwn6m1ZH4vTf1sER5WzSrRA72wxbRaVdLuMN8YZ69C EZaIonzxJkVDA== From: Muhammad Rizki To: Ammar Faizi Cc: Muhammad Rizki , GNU/Weeb Mailing List Subject: [PATCH v2 06/18] Refactor database methods Date: Fri, 29 Jul 2022 07:44:55 +0700 Message-Id: <20220729004507.1668-7-kiizuha@gnuweeb.org> X-Mailer: git-send-email 2.34.1.windows.1 In-Reply-To: <20220729004507.1668-1-kiizuha@gnuweeb.org> References: <20220729004507.1668-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