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.57]) by gnuweeb.org (Postfix) with ESMTPSA id 2CED27FBEE; Tue, 19 Jul 2022 00:18:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1658189900; bh=68fjWXNhUa20pzX/2dtxAGe+/ADy+ONIZsdSXbY8amQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nPDlKMo9DaP+ESGkUJzgpAYnz5r5FGkiIXKaaaPQ20PDGS93L44suo1YITb0+sSgV PdnsPaPXcVYjWI9JNx8LH4evHKeT/Kxr76R/KO4qjXRvbeVyY8cWeISlJXwgIS1QfE AhdDmYrfWWDWgA8ZIY9ONLI9CjZowXXYTPyxgvOiIi6JIf9eTeaBzEkdtjRqdGGkYK Z8FOcmVkq6ppcagy7fAyjp6urSz+A+zDwvw4hlGObaznqvBTJ6UF9sMp3dU60Kbd5o fGRY0YdR83Whzjt+kD3gvxVzm9TJ4yCSucYaYKvLruemhZftqLO9cGBr/zkBYo6ZQM av0MUkeHVR6Tg== From: Muhammad Rizki To: Ammar Faizi Cc: Muhammad Rizki , GNU/Weeb Mailing List Subject: [PATCH v2 09/18] Move prepare for patch and clean up patch functions Date: Tue, 19 Jul 2022 07:17:35 +0700 Message-Id: <20220719001744.1950-10-kiizuha@gnuweeb.org> X-Mailer: git-send-email 2.34.1.windows.1 In-Reply-To: <20220719001744.1950-1-kiizuha@gnuweeb.org> References: <20220719001744.1950-1-kiizuha@gnuweeb.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit List-Id: From: Muhammad Rizki I want these function is separate into the utils or utility file, I'm the type of person who likes to separate the utility function into the utility file. Signed-off-by: Muhammad Rizki --- daemon/scraper/utils.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/daemon/scraper/utils.py b/daemon/scraper/utils.py index 10fe956..5335fe2 100644 --- a/daemon/scraper/utils.py +++ b/daemon/scraper/utils.py @@ -6,10 +6,12 @@ from email.message import Message from typing import Dict +from slugify import slugify import hashlib import uuid import os import re +import shutil def get_email_msg_id(mail): @@ -190,6 +192,39 @@ def create_template(thread: Message, to=None, cc=None): return ret, files, is_patch +def prepare_send_patch(mail, text, url): + tmp = gen_temp(url) + fnm = str(mail.get("subject")) + sch = re.search(PATCH_PATTERN, fnm, re.IGNORECASE) + + nr_patch = sch.group(1) + if not nr_patch: + nr_patch = 1 + else: + nr_patch = int(nr_patch) + + num = "%04d" % nr_patch + fnm = slugify(sch.group(3)).replace("_", "-") + file = f"{tmp}/{num}-{fnm}.patch" + cap = text.split("\n\n")[0] + + with open(file, "wb") as f: + f.write(bytes(text, encoding="utf8")) + + caption = ( + "#patch #ml\n" + + cap.rstrip() + .replace("<", "<") + .replace(">",">") + .replace("�"," ") + ) + return tmp, file, caption, url + + +def clean_up_after_send_patch(tmp): + shutil.rmtree(tmp) + + EMAIL_MSG_ID_PATTERN = r"<([^\<\>]+)>" def extract_email_msg_id(msg_id): ret = re.search(EMAIL_MSG_ID_PATTERN, msg_id) -- Muhammad Rizki