GNU/Weeb Mailing List <[email protected]>
 help / color / mirror / Atom feed
From: Muhammad Rizki <[email protected]>
To: Ammar Faizi <[email protected]>
Cc: Muhammad Rizki <[email protected]>,
	GNU/Weeb Mailing List <[email protected]>
Subject: [PATCH v2 06/18] Refactor database methods
Date: Fri, 29 Jul 2022 07:40:34 +0700	[thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>

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 <[email protected]>
---
 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


  parent reply	other threads:[~2022-07-29  0:42 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-29  0:40 [PATCH v1 00/18] New feature to handle atom URLs and broadcast Muhammad Rizki
2022-07-29  0:40 ` [PATCH v1 01/18] Add atom_broadcast.sql Muhammad Rizki
2022-07-29  0:40 ` [PATCH v1 02/18] Add ATOM_URLS and BROADCAST_CHAT methods Muhammad Rizki
2022-07-29  0:40 ` [PATCH v1 03/18] Fix MySQL InternalError: Unread result found Muhammad Rizki
2022-07-29  0:40 ` [PATCH v1 04/18] Create /add_atom command Muhammad Rizki
2022-07-29  0:40 ` [PATCH v1 05/18] Add utility functions for /add_atom Muhammad Rizki
2022-07-29  0:40 ` [PATCH v1 06/18] Refactor database methods Muhammad Rizki
2022-07-29  0:40 ` [PATCH v1 07/18] Add insert_atom() in scraper/db.py Muhammad Rizki
2022-07-29  0:40 ` [PATCH v1 08/18] Rename admin.py to debugger.py Muhammad Rizki
2022-07-29  0:40 ` [PATCH v1 09/18] Add delete_atom() function in scraper/db.py Muhammad Rizki
2022-07-29  0:40 ` [PATCH v1 10/18] Add utility function buton_numbers() Muhammad Rizki
2022-07-29  0:40 ` [PATCH v1 11/18] Move bot commands related files to packages/plugins/commands/ Muhammad Rizki
2022-07-29  0:40 ` [PATCH v1 12/18] Add del_atom callback query Muhammad Rizki
2022-07-29  0:40 ` [PATCH v1 13/18] Add insert_broadcast() in scraper/db.py Muhammad Rizki
2022-07-29  0:40 ` [PATCH v1 14/18] Add delete_broadcast() " Muhammad Rizki
2022-07-29  0:40 ` [PATCH v1 15/18] Create /add_bc bot command Muhammad Rizki
2022-07-29  0:40 ` [PATCH v1 16/18] Add create_chat_link() function Muhammad Rizki
2022-07-29  0:40 ` [PATCH v1 17/18] Add /del_bc bot command Muhammad Rizki
2022-07-29  0:40 ` [PATCH v1 18/18] Add del_chat callback query Muhammad Rizki
2022-07-29  0:40 ` [PATCH v2 00/18] New feature to handle atom URLs and broadcast Muhammad Rizki
2022-07-29  0:40 ` [PATCH v2 01/18] Add atom_broadcast.sql Muhammad Rizki
2022-07-29  0:40 ` [PATCH v2 02/18] Add ATOM_URLS and BROADCAST_CHAT methods Muhammad Rizki
2022-07-29  0:40 ` [PATCH v2 03/18] Fix MySQL InternalError: Unread result found Muhammad Rizki
2022-07-29  0:40 ` [PATCH v2 04/18] Create /add_atom command Muhammad Rizki
2022-07-29  0:40 ` [PATCH v2 05/18] Add utility functions for /add_atom Muhammad Rizki
2022-07-29  0:40 ` Muhammad Rizki [this message]
2022-07-29  0:40 ` [PATCH v2 07/18] Add insert_atom() in scraper/db.py Muhammad Rizki
2022-07-29  0:40 ` [PATCH v2 08/18] Rename admin.py to debugger.py Muhammad Rizki
2022-07-29  0:40 ` [PATCH v2 09/18] Add delete_atom() function in scraper/db.py Muhammad Rizki
2022-07-29  0:40 ` [PATCH v2 10/18] Add utility function buton_numbers() Muhammad Rizki
2022-07-29  0:40 ` [PATCH v2 11/18] Move bot commands related files to packages/plugins/commands/ Muhammad Rizki
2022-07-29  0:40 ` [PATCH v2 12/18] Add del_atom callback query Muhammad Rizki
2022-07-29  0:40 ` [PATCH v2 13/18] Add insert_broadcast() in scraper/db.py Muhammad Rizki
2022-07-29  0:40 ` [PATCH v2 14/18] Add delete_broadcast() " Muhammad Rizki
2022-07-29  0:40 ` [PATCH v2 15/18] Create /add_bc bot command Muhammad Rizki
2022-07-29  0:40 ` [PATCH v2 16/18] Add create_chat_link() function Muhammad Rizki
2022-07-29  0:40 ` [PATCH v2 17/18] Add /del_bc bot command Muhammad Rizki
2022-07-29  0:40 ` [PATCH v2 18/18] Add del_chat callback query Muhammad Rizki
2022-07-29  0:44 [PATCH v2 00/18] New feature to handle atom URLs and broadcast Muhammad Rizki
2022-07-29  0:44 ` [PATCH v2 06/18] Refactor database methods Muhammad Rizki

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox