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 2AB54806D7; Fri, 29 Jul 2022 00:45:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1659055551; bh=Y47bEUHzpLq3GKjsueDeqo81KuFCnLYCPmbqTGHiHmg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nDMkR9tWZEk4X2aR+MrdvxbVSZCTwNlXHbNkSmvovYypPCTR7pvlFaTBZVNILt9Yu KSwBRxeg4KutR4xsZDCLnsiB1j42BSly/dP8p3xCrKsSmQ0vANW5FOXrQ09o1wIAP4 LwVHf+58QLOzAywcO6Mhs9uqywNja7Y25LgTxR1yLS4YdZznKaZORRzWp6v7R//dvc JH5ZcX2WCd8wXUsmuJb1oQbDs+8CP5LbgTX5xYgGRR/umJGqnZlYmI8Ubr05J66GH3 xBBBACbmh0RwyjTtGITFJ5E+VETKv4prNtBngq+lcja2IvYVYIVk2tW3oalI0FlfE6 8akVNwVo8OeUg== From: Muhammad Rizki To: Ammar Faizi Cc: Muhammad Rizki , GNU/Weeb Mailing List Subject: [PATCH v2 07/18] Add insert_atom() in scraper/db.py Date: Fri, 29 Jul 2022 07:44:56 +0700 Message-Id: <20220729004507.1668-8-kiizuha@gnuweeb.org> X-Mailer: git-send-email 2.34.1.windows.1 In-Reply-To: <20220729004507.1668-1-kiizuha@gnuweeb.org> References: <20220729004507.1668-1-kiizuha@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: Add insert_atom() in scraper/db.py and call it in packages/plugins/add_atom.py to insert atom url into the database Signed-off-by: Muhammad Rizki --- daemon/packages/plugins/manage_atom.py | 18 +++++++++++++----- daemon/scraper/db.py | 16 ++++++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/daemon/packages/plugins/manage_atom.py b/daemon/packages/plugins/manage_atom.py index a10318f..934aba9 100644 --- a/daemon/packages/plugins/manage_atom.py +++ b/daemon/packages/plugins/manage_atom.py @@ -13,10 +13,18 @@ from scraper import utils filters.chat(["kiizuah", "nekoha", -1001673279485]) ) async def add_atom_url(c: DaemonClient, m: Message): + if len(m.command) <= 1: + tutor = "Please specify the URL\n" + tutor += "Example: `/add_atom https://lore.kernel.org/linux-sgx/new.atom`" + return await m.reply(tutor) + text = utils.remove_command(m.text) - if not utils.is_atom_url(text): - return + is_atom = await utils.is_atom_url(text) + if not is_atom: + return await m.reply("Invalid Atom URL") + + inserted = c.db.insert_atom(text) + if inserted is None: + return await m.reply(f"This URL already listened for new email.") - ### - ### TODO: Muhammad Rizki: Add atom url into the database - ### + await m.reply(f"Success add **{text}** for listening new email") diff --git a/daemon/scraper/db.py b/daemon/scraper/db.py index 296559e..30ed9a7 100644 --- a/daemon/scraper/db.py +++ b/daemon/scraper/db.py @@ -114,6 +114,22 @@ class Db(): return res[0] + def insert_atom(self, atom: str): + try: + return self.__save_atom(atom) + except mysql.connector.errors.IntegrityError: + # + # Duplicate data, skip! + # + return None + + + def __save_atom(self, atom: str): + q = "INSERT INTO atom_urls (url, created_at) VALUES (%s, %s)" + self.cur.execute(q, (atom, datetime.utcnow())) + return self.cur.lastrowid + + def get_atom_urls(self): q = """ SELECT atom_urls.url -- Muhammad Rizki