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.123]) by gnuweeb.org (Postfix) with ESMTPSA id 7FB2280908; Fri, 21 Oct 2022 13:45:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1666359959; bh=A6x8bTQL10dhR0DrApHo5vvmOJqnyqZr6jaseg293RA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TN075J4VvCQqGryPxJZ1dtrUeYjfTZ0Qp7AaCRPB/YjS12l+1iFnmh4pkA90hBedx siuGQMewnyOoqMe7Rl0H5ngCR8K99bw8t5E01rRWxAnbikFtH6IJh899dkjHP0QUiV f42PekIZrhbqHWdxI3/AeJ2nRVgSk3z/Ygvg7YH5Ls+1fQX8l56WzAp0rF6UIbnsET 4qfHKSvThvsRXM4vmqsgQ2k6MkoWndQUFxGkSJYZ6TMFae30I6NoA1A1SN1UktpoLX 6UG0eGuuPTRzWVE9PKG07SFg6n8qSy30rSybl8OHwB8ISZJSnU9jO4PomA8UnIOAKh pIf171XuGwKhA== From: Muhammad Rizki To: Ammar Faizi Cc: Muhammad Rizki , Alviro Iskandar Setiawan , GNU/Weeb Mailing List Subject: [PATCH v3 9/9] enum: Use the created Platform class enumeration Date: Fri, 21 Oct 2022 20:45:20 +0700 Message-Id: <20221021134520.701-10-kiizuha@gnuweeb.org> X-Mailer: git-send-email 2.34.1.windows.1 In-Reply-To: <20221021134520.701-1-kiizuha@gnuweeb.org> References: <20221021134520.701-1-kiizuha@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: Use the Platform class enumeration to make it more practice, avoid getting typo, type-hinted suggestion to easily manageable-maintainable. Signed-off-by: Muhammad Rizki --- daemon/atom/utils.py | 28 ++++++++----------- daemon/dscord/gnuweeb/client.py | 5 ++-- .../plugins/slash_commands/get_lore_mail.py | 3 +- daemon/dscord/mailer/listener.py | 3 +- daemon/telegram/mailer/listener.py | 4 +-- daemon/telegram/packages/client.py | 3 +- .../packages/plugins/commands/scrape.py | 4 +-- 7 files changed, 25 insertions(+), 25 deletions(-) diff --git a/daemon/atom/utils.py b/daemon/atom/utils.py index 6a9e5c8..b7a7aaf 100644 --- a/daemon/atom/utils.py +++ b/daemon/atom/utils.py @@ -4,6 +4,8 @@ # Copyright (C) 2022 Ammar Faizi # +from enums import Platform + from pyrogram.types import Chat, InlineKeyboardMarkup, InlineKeyboardButton from email.message import Message from typing import Dict, Union @@ -115,19 +117,13 @@ def consruct_to_n_cc(to: list, cc: list): return ret -def gen_temp(name: str, platform: str): - platform = platform.lower() - plt_ls = ["telegram", "discord"] - - if platform not in plt_ls: - t = f"Platform {platform} is not found, " - t += f"only {', '.join(plt_ls)} is available" - raise ValueError(f"Platform {platform} is not found") - +def gen_temp(name: str, platform: Platform): + platform: str = platform.value md5 = hashlib.md5(name.encode()).hexdigest() store_dir = os.getenv("STORAGE_DIR", "storage") platform = platform.replace("discord", "dscord") path = f"{platform}/{store_dir}/{md5}" + try: os.mkdir(path) except FileExistsError: @@ -136,11 +132,11 @@ def gen_temp(name: str, platform: str): return path -def extract_body(thread: Message, platform: str): +def extract_body(thread: Message, platform: Platform): if not thread.is_multipart(): p = manage_payload(thread) - if platform == "discord": + if platform is Platform.DISCORD: p = quote_reply(p) return f"{p}\n".lstrip(), [] @@ -183,12 +179,12 @@ def __is_patch(subject, content): return True -def create_template(thread: Message, platform: str, to=None, cc=None): +def create_template(thread: Message, platform: Platform, to=None, cc=None): if not to: to = extract_list("to", thread) if not cc: cc = extract_list("cc", thread) - if platform == "telegram": + if platform is Platform.TELEGRAM: substr = 4000 border = f"\n{'-'*72}" else: @@ -211,13 +207,13 @@ def create_template(thread: Message, platform: str, to=None, cc=None): if len(ret) >= substr: ret = ret[:substr] + "..." - ret = fix_utf8_char(ret, platform == "telegram") + ret = fix_utf8_char(ret, platform is Platform.TELEGRAM) ret += border return ret, files, is_patch -def prepare_patch(mail, text, url, platform: str): +def prepare_patch(mail, text, url, platform: Platform): tmp = gen_temp(url, platform) fnm = str(mail.get("subject")) sch = re.search(PATCH_PATTERN, fnm, re.IGNORECASE) @@ -237,7 +233,7 @@ def prepare_patch(mail, text, url, platform: str): f.write(bytes(text, encoding="utf8")) caption = "#patch #ml" - if platform == "telegram": + if platform is Platform.TELEGRAM: caption += fix_utf8_char("\n" + cap, True) return tmp, file, caption, url diff --git a/daemon/dscord/gnuweeb/client.py b/daemon/dscord/gnuweeb/client.py index b921f7d..82858c5 100644 --- a/daemon/dscord/gnuweeb/client.py +++ b/daemon/dscord/gnuweeb/client.py @@ -13,6 +13,7 @@ from typing import Union from . import filters from . import models from atom import utils +from enums import Platform from dscord.database import DB @@ -59,7 +60,7 @@ class GWClient(commands.Bot): reply_to: Union[int, None] = None, url: str = None): print("[send_patch_email]") tmp, doc, caption, url = utils.prepare_patch( - mail, text, url, "discord" + mail, text, url, Platform.DISCORD ) channel = self.get_channel(chat_id) @@ -90,7 +91,7 @@ class GWClient(commands.Bot): async def send_patch_mail_interaction(self, mail, i: "Interaction", text: str, url: str = None): tmp, doc, caption, url = utils.prepare_patch( - mail, text, url, "discord" + mail, text, url, Platform.DISCORD ) m = await i.response.send_message( content=caption, diff --git a/daemon/dscord/gnuweeb/plugins/slash_commands/get_lore_mail.py b/daemon/dscord/gnuweeb/plugins/slash_commands/get_lore_mail.py index 2d55d16..0c67b8c 100644 --- a/daemon/dscord/gnuweeb/plugins/slash_commands/get_lore_mail.py +++ b/daemon/dscord/gnuweeb/plugins/slash_commands/get_lore_mail.py @@ -11,6 +11,7 @@ from discord import app_commands from atom import utils from atom import Scraper +from enums import Platform class GetLoreSC(commands.Cog): @@ -26,7 +27,7 @@ class GetLoreSC(commands.Cog): async def get_lore(self, i: "Interaction", url: str): s = Scraper() mail = await s.get_email_from_url(url) - text, _, is_patch = utils.create_template(mail, "discord") + text, _, is_patch = utils.create_template(mail, Platform.DISCORD) if is_patch: m = await self.bot.send_patch_mail_interaction( diff --git a/daemon/dscord/mailer/listener.py b/daemon/dscord/mailer/listener.py index cc0a9f7..d986fbd 100644 --- a/daemon/dscord/mailer/listener.py +++ b/daemon/dscord/mailer/listener.py @@ -14,6 +14,7 @@ from discord import Message from dscord.gnuweeb import GWClient from atom.scraper import Scraper from atom import utils +from enums import Platform class Mutexes: @@ -107,7 +108,7 @@ class Listener: # return False - text, files, is_patch = utils.create_template(mail, "discord") + text, files, is_patch = utils.create_template(mail, Platform.DISCORD) reply_to = self.get_discord_reply(mail, dc_chat_id) url = str(re.sub(r"/raw$", "", url)) diff --git a/daemon/telegram/mailer/listener.py b/daemon/telegram/mailer/listener.py index 08feddd..1c92f23 100644 --- a/daemon/telegram/mailer/listener.py +++ b/daemon/telegram/mailer/listener.py @@ -9,8 +9,8 @@ from apscheduler.schedulers.asyncio import AsyncIOScheduler from telegram.packages import DaemonClient from atom import Scraper from atom import utils +from enums import Platform import asyncio -import shutil import re import traceback @@ -99,7 +99,7 @@ class Bot(): # return False - text, files, is_patch = utils.create_template(mail, "telegram") + text, files, is_patch = utils.create_template(mail, Platform.TELEGRAM) reply_to = self.get_reply(mail, tg_chat_id) url = str(re.sub(r"/raw$", "", url)) diff --git a/daemon/telegram/packages/client.py b/daemon/telegram/packages/client.py index 686e5ef..c971ea1 100644 --- a/daemon/telegram/packages/client.py +++ b/daemon/telegram/packages/client.py @@ -9,6 +9,7 @@ from pyrogram.types import Message, InlineKeyboardMarkup, InlineKeyboardButton from typing import Union from email.message import Message from atom import utils +from enums import Platform from telegram.database import DB from .decorator import handle_flood @@ -57,7 +58,7 @@ class DaemonClient(Client): ) -> Message: print("[send_patch_email]") tmp, doc, caption, url = utils.prepare_patch( - mail, text, url, "telegram" + mail, text, url, Platform.TELEGRAM ) m = await self.send_document( chat_id=chat_id, diff --git a/daemon/telegram/packages/plugins/commands/scrape.py b/daemon/telegram/packages/plugins/commands/scrape.py index 860e993..29cc8ad 100644 --- a/daemon/telegram/packages/plugins/commands/scrape.py +++ b/daemon/telegram/packages/plugins/commands/scrape.py @@ -9,8 +9,8 @@ from pyrogram import filters from telegram.packages import DaemonClient from atom import Scraper from atom import utils +from enums import Platform from telegram import config -import shutil import re import asyncio @@ -37,7 +37,7 @@ async def scrap_email(c: DaemonClient, m: Message): s = Scraper() mail = await s.get_email_from_url(url) - text, files, is_patch = utils.create_template(mail, "telegram") + text, files, is_patch = utils.create_template(mail, Platform.TELEGRAM) if is_patch: m = await c.send_patch_email( -- Muhammad Rizki