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 E73C57E804; Thu, 21 Jul 2022 23:30:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1658446214; bh=68fjWXNhUa20pzX/2dtxAGe+/ADy+ONIZsdSXbY8amQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rdp0FfCPSpXfIkfZ/2FGsk6MAR3aLg7JL1mnd62XEvmietRkLAvvAAYr/laoDJSfP s+PbZ4W659yYreVVdDiEb9rVsrUmL+RTzgtSJBQoDV2iCgfiDrch1i7aOUtE496+C+ MgQp9c9veU40EFzKk43XDKNJKadURqHkNgFASF1/M7qrM1wyTkdPH2/5swavTk81YL dXKpub0FTeVzU7Y6rRQt9DNfzlqoPoWYov/ErlGbqHMFqmiQDFnZWTKWnjGLtDQZTo t8+uPguGFyCk2l84ctnRfc+5VtGYYF2FQTfQ5Q1DPeATE+dqBEj8SGfTpY1iu6LvWl uuHVMfk2iQN2A== From: Muhammad Rizki To: Ammar Faizi Cc: Muhammad Rizki , GNU/Weeb Mailing List Subject: [PATCH v3 09/17] Move prepare for patch and clean up patch functions Date: Fri, 22 Jul 2022 06:29:30 +0700 Message-Id: <20220721232938.503-10-kiizuha@gnuweeb.org> X-Mailer: git-send-email 2.34.1.windows.1 In-Reply-To: <20220721232938.503-1-kiizuha@gnuweeb.org> References: <20220721232938.503-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