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 B19307FA25; Wed, 27 Jul 2022 02:47:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1658890029; bh=Y47bEUHzpLq3GKjsueDeqo81KuFCnLYCPmbqTGHiHmg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PH8UW91l414ScXTNxzRhS8dHQ/6gluKPDI3v1twe95lFIGh03h4cDVpY3gkNsrKmd LhnP6BQ4HBSsENVL70GQUjOuvERFYjLG44Ujy5o3z6mNHetHAKRWwIT72HIlfjk2VO SlIFlIdX5OZmu8tTkPSPeWZXOPfNY4oGb/f0XDxOzdnE8fxlXtOhBSAoTYbbxKi8hg NKbJA3RINfvsAY1AIgSQ30TF1nKJXqMvYZZ5s7MmB7WHsDQmIdCOfvdOfHxTItyFZ/ DTmKpU4zQdDbD03V1oRONgTU/9dex184WehotmgGOH1+Iuf/4A/ZKOX+5zvmKYtlBU s0OYFlTQXgaNg== From: Muhammad Rizki To: Ammar Faizi Cc: Muhammad Rizki , GNU/Weeb Mailing List Subject: [PATCH v1 07/18] Add insert_atom() in scraper/db.py Date: Wed, 27 Jul 2022 09:46:23 +0700 Message-Id: <20220727024634.1995-8-kiizuha@gnuweeb.org> X-Mailer: git-send-email 2.34.1.windows.1 In-Reply-To: <20220727024634.1995-1-kiizuha@gnuweeb.org> References: <20220727024634.1995-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