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.9 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_H2,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) by gnuweeb.org (Postfix) with ESMTPS id C0C737E328 for ; Mon, 18 Jul 2022 11:20:57 +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=NIYb/d9G; dkim-atps=neutral Received: by mail-pl1-f176.google.com with SMTP id y15so3985548plp.10 for ; Mon, 18 Jul 2022 04:20:57 -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=mQCHNfqXqBY+xdAZ8U0k4f8tcahryetYDQt3QcMvoiI=; b=NIYb/d9G+8XHg97ReD/uQ2wWqJUnuUFZY0Rn9MDcOfpklTVBSNA8BGN07pyo+nlpKP siJjDGpx8ZgRSOqXwo5EajZn/x98oDmRQr3219b/Y6hZh01FRC3Cks1Yd1zRE9ulzjnJ HsBMfLFhVr1ASkGHRj8bUCDz1MaI1DrFK6vZNAhxowkvBqrfrKlewjiK/gru5CJH9RFB t2mEcB7r1ejR2tyY7Q6ViZN1seufBZ0kogKXdxXq6xnN31tqXeRyfdeB3XhARz0zLs9r +vK2tRbrLKbrybm05yiwSeIYDCgpMTw8Mj3iAjhf3tqHX3wXefRLfe0pF81MB4Zd88ra Sb/Q== 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=mQCHNfqXqBY+xdAZ8U0k4f8tcahryetYDQt3QcMvoiI=; b=nBVssCQbEIJ/3Dg8a2BkyFLHSqaULOxptsv7KvTgZCrtYGmtJMf7o9Nb8r8OS3QrL2 aRLGVABUhvufO1aMGb2g4s8+wyG9EN0g6bbaLLJDtt8QvtFKpxVGPAjpyecENVblZZCa 2vUSkYcrkGBz6g9sF5m1/w6cS0soX52ZLGRI6Bmif2orr9YaGHdtX9Yp8qUlD7TEv0sd aHmlv/Gem5I2RX6wqWmjQ/EK50K7lBfLyTbb7ARZCRbBg7sLOjqLc/Ib5pgEHHB9iXwn RyV9H81PWPQVvUKLhNtOkkaVT+edj7LjEcICmyjmFe2A0Jp/tjGm1pY5t/3V8FIJ5xWr CE5A== X-Gm-Message-State: AJIora8rFr/xKQ9Va13MZIW7fqWEYZ0CzjuRHEtjWTXVog8DvZQV6yD0 ndiSiThV2IIgYo9hOE7re8LO1FTO3wOIfg== X-Google-Smtp-Source: AGRyM1tLg1OFwcxSqvXi/fZPShj2Wd+n++xsTO/PjC/+M5uFCANqqJZ3jCGiIKeTva5N2Zue9F9yFQ== X-Received: by 2002:a17:903:1103:b0:16c:9a6e:d54 with SMTP id n3-20020a170903110300b0016c9a6e0d54mr24929087plh.131.1658143257216; Mon, 18 Jul 2022 04:20:57 -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.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Jul 2022 04:20:56 -0700 (PDT) From: Muhammad Rizki To: Ammar Faizi Cc: Muhammad Rizki , GNU/Weeb Mailing List Subject: [PATCH v1 09/11] Move prepare for patch and clean up patch functions Date: Mon, 18 Jul 2022 18:20:05 +0700 Message-Id: <20220718112007.792-10-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-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit List-Id: 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