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.0 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 Received: from mail-pg1-f169.google.com (mail-pg1-f169.google.com [209.85.215.169]) by gnuweeb.org (Postfix) with ESMTPS id 61CC77E328 for ; Mon, 18 Jul 2022 11:20:53 +0000 (UTC) Authentication-Results: gnuweeb.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20210112 header.b=UYFh8Nws; dkim-atps=neutral Received: by mail-pg1-f169.google.com with SMTP id g4so10289285pgc.1 for ; Mon, 18 Jul 2022 04:20:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=cO9/1TuDlLeVHIu8sVJ01ny1FgYTpINZaF864+cp7MQ=; b=UYFh8NwsRpNl/RgShjLvldV3xA1l2Wya+cArGnHrZn5kMJRSYxJvpZ2n5mjbt3HaMe 8WloSdDYGsZ0LqqokzvsSifXTftoxvq8D2zEttpl7omeInxO816IK/QhW6Q+h66bdx6h WF54FDmoAHoe4pDrPQy5MHgwC5Kht/KRTh2i73T8YBpmSFgIgibnpnvlrGNgiDmbOEWv 2vNoAsiEwCTqp3HSIQLQICuYOzrkjvJB2g32L/6npyej6GVR7TskQAX+gsaYWneDC7ir nDncqEGIgVwETPxZHdWFH0ZZmytjx+2tMsJP2U2i/T9FNVX9H81TYzqpm5Wa3IXo4ZBd wirw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=cO9/1TuDlLeVHIu8sVJ01ny1FgYTpINZaF864+cp7MQ=; b=0sK2Ih7yFiY1QqQTp0ITN3MiuBYiqTppyK4bnGdhlHtwkdtNJpbxrrt05BoHy8lLMN DS7/Fd/m9jqa4/djc1TqL0aY5GLKb4Kwq040dIMODXGlZN1dlHgm+q1jdpNYgqoKwIux /Z1b7/yk3EwhhKt9Ql3iTpGhnM+DtFrQOjz5n4VW1+uQxeZLTsY/YYLWv/69JetIgxy5 niQSLmgc9hgscnL1OCPFClc5ENPFwNtuN1wDwLryLOOcUzzzDudIntM3kTwhW/5Y07CM BYpfMLr9NbgZBOPVmHSSM4zWXTgvxdhl5oeLJ2rSJyb7ozkihNVB3x6ji5z8Lmvi8Xjb HCWg== X-Gm-Message-State: AJIora8k/nSmKAXqOZaTEygJGCx7ceGKLUBooUsakmn5H/hbrsem3ALj fBOIo47Oihej8S4cBxWSGn6o0lJ7rS4CNw== X-Google-Smtp-Source: AGRyM1tZafEqz1+Uy9yb++YhrTk48IGZwfEeTMEw3c3/QRxnugcnQDu9vm1j2BEEzC54XN16ACGHoA== X-Received: by 2002:a63:6aca:0:b0:419:cb1b:8b4f with SMTP id f193-20020a636aca000000b00419cb1b8b4fmr17753487pgc.92.1658143253024; Mon, 18 Jul 2022 04:20:53 -0700 (PDT) Received: from localhost.localdomain ([101.128.125.134]) by smtp.gmail.com with ESMTPSA id z14-20020aa7948e000000b0052ad6d627a6sm8991072pfk.166.2022.07.18.04.20.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Jul 2022 04:20:52 -0700 (PDT) From: Muhammad Rizki To: Ammar Faizi Cc: Muhammad Rizki , GNU/Weeb Mailing List Subject: [PATCH v1 07/11] Re-design send email message to Telegram Date: Mon, 18 Jul 2022 18:20:03 +0700 Message-Id: <20220718112007.792-8-riskimuhammmad1@gmail.com> X-Mailer: git-send-email 2.34.1.windows.1 In-Reply-To: <20220718112007.792-1-riskimuhammmad1@gmail.com> References: <20220718112007.792-1-riskimuhammmad1@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: We want to separate send message function and inherit with the Pyrogram Client, so it should makes the code clean and clear. Signed-off-by: Muhammad Rizki --- daemon/packages/__init__.py | 1 + daemon/packages/client.py | 59 +++++++++++++++++++++++++++++++++++++ daemon/run.py | 4 +-- daemon/scraper/bot.py | 35 ++++++---------------- 4 files changed, 71 insertions(+), 28 deletions(-) create mode 100644 daemon/packages/__init__.py create mode 100644 daemon/packages/client.py diff --git a/daemon/packages/__init__.py b/daemon/packages/__init__.py new file mode 100644 index 0000000..efef9ae --- /dev/null +++ b/daemon/packages/__init__.py @@ -0,0 +1 @@ +from .client import DaemonClient diff --git a/daemon/packages/client.py b/daemon/packages/client.py new file mode 100644 index 0000000..fd5b5ec --- /dev/null +++ b/daemon/packages/client.py @@ -0,0 +1,59 @@ +from pyrogram import Client +from pyrogram.enums import ParseMode +from pyrogram.types import Message, InlineKeyboardMarkup, InlineKeyboardButton +from typing import Union, BinaryIO + + +class DaemonClient(Client): + def __init__(self, name: str, api_id: int, + api_hash: str, **kwargs): + super().__init__(name, api_id, + api_hash, **kwargs) + + + async def send_text_email( + self, + chat_id: Union[int, str], + text: str, + reply_to: int, + url: str = None, + parse_mode: ParseMode = ParseMode.HTML + ) -> Message: + print("[send_text_email]") + return await self.send_message( + chat_id=chat_id, + text=text, + reply_to_message_id=reply_to, + parse_mode=parse_mode, + reply_markup=InlineKeyboardMarkup([ + [InlineKeyboardButton( + "See the full message", + url=url + )] + ]) + ) + + + async def send_patch_email( + self, + chat_id: Union[int, str], + doc: Union[str, BinaryIO], + caption: str, + reply_to: int, + url: str = None, + parse_mode: ParseMode = ParseMode.HTML + ) -> Message: + print("[send_patch_email]") + return await self.send_document( + chat_id=chat_id, + document=doc, + caption=caption, + reply_to_message_id=reply_to, + parse_mode=parse_mode, + reply_markup=InlineKeyboardMarkup([ + [InlineKeyboardButton( + "See the full message", + url=url + )] + ]) + ) diff --git a/daemon/run.py b/daemon/run.py index 83b2cdb..1151ccd 100644 --- a/daemon/run.py +++ b/daemon/run.py @@ -8,7 +8,7 @@ from apscheduler.schedulers.asyncio import AsyncIOScheduler from scraper import BotMutexes from dotenv import load_dotenv from mysql import connector -from pyrogram import Client +from packages import DaemonClient from scraper import Scraper from scraper import Bot import os @@ -17,7 +17,7 @@ import os def main(): load_dotenv() - client = Client( + client = DaemonClient( "storage/EmailScraper", api_id=int(os.getenv("API_ID")), api_hash=os.getenv("API_HASH"), diff --git a/daemon/scraper/bot.py b/daemon/scraper/bot.py index 2392b61..93e633a 100644 --- a/daemon/scraper/bot.py +++ b/daemon/scraper/bot.py @@ -8,7 +8,7 @@ from apscheduler.schedulers.asyncio import AsyncIOScheduler from pyrogram.types import InlineKeyboardMarkup from pyrogram.types import InlineKeyboardButton from slugify import slugify -from pyrogram import Client +from packages import DaemonClient from scraper import Scraper from pyrogram import enums from . import utils @@ -32,12 +32,11 @@ class Bot(): ] TG_CHAT_IDS = [ - -1001394203410, - -1001673279485, + "kiizuah" ] - def __init__(self, client: Client, sched: AsyncIOScheduler, + def __init__(self, client: DaemonClient, sched: AsyncIOScheduler, scraper: Scraper, mutexes: BotMutexes, conn): self.client = client self.sched = sched @@ -123,8 +122,8 @@ class Bot(): reply_to, text, url) else: text = "#ml\n" + text - m = await self.__send_text_msg(tg_chat_id, reply_to, - text, url) + m = await self.__send_text_msg(tg_chat_id, text, + reply_to, url) self.db.insert_telegram(email_id, m.chat.id, m.id) for d, f in files: @@ -161,10 +160,10 @@ class Bot(): async def __send_patch_msg(self, mail, tg_chat_id, reply_to, text, url): print("[__send_patch_msg]") - tmp, fnm, caption, url = Bot.prepare_send_patch(mail, text, url) + tmp, doc, caption, url = Bot.prepare_send_patch(mail, text, url) ret = await self.__handle_telegram_floodwait( - self.____send_patch_msg, - *[tg_chat_id, reply_to, fnm, caption, url] + self.client.send_patch_email, + *[tg_chat_id, doc, caption, reply_to, url] ) Bot.clean_up_after_send_patch(tmp) return ret @@ -207,7 +206,7 @@ class Bot(): async def __send_text_msg(self, *args): return await self.__handle_telegram_floodwait( - self.____send_text_msg, + self.client.send_text_email, *args ) @@ -251,19 +250,3 @@ class Bot(): )] ]) ) - - - async def ____send_text_msg(self, tg_chat_id, reply_to, text, url): - print("[__send_text_msg]") - return await self.client.send_message( - tg_chat_id, - text, - reply_to_message_id=reply_to, - parse_mode=enums.ParseMode.HTML, - reply_markup=InlineKeyboardMarkup([ - [InlineKeyboardButton( - "See the full message", - url=url - )] - ]) - ) -- Muhammad Rizki