From: Muhammad Rizki <[email protected]>
To: Ammar Faizi <[email protected]>
Cc: Muhammad Rizki <[email protected]>,
GNU/Weeb Mailing List <[email protected]>
Subject: [PATCH v3 13/17] daemon: Add @handle_flood decorator and remove some functions
Date: Fri, 22 Jul 2022 06:29:34 +0700 [thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>
From: Muhammad Rizki <[email protected]>
I decided to create @handle_flood decorator to make it easier to handle
the Telegram floodwait. Remove __send_text_msg() and replace the
call of the __send_text_msg() with send_text_email() in
packages/client.py and it have handle_flood decorator support.
Signed-off-by: Muhammad Rizki <[email protected]>
---
daemon/packages/client.py | 3 +++
daemon/packages/decorator.py | 38 +++++++++++++++++++++++++++++
daemon/scraper/bot.py | 47 ++++++------------------------------
3 files changed, 49 insertions(+), 39 deletions(-)
create mode 100644 daemon/packages/decorator.py
diff --git a/daemon/packages/client.py b/daemon/packages/client.py
index f73b913..45acac3 100644
--- a/daemon/packages/client.py
+++ b/daemon/packages/client.py
@@ -7,6 +7,7 @@ from pyrogram import Client
from pyrogram.enums import ParseMode
from pyrogram.types import Message, InlineKeyboardMarkup, InlineKeyboardButton
from typing import Union, BinaryIO
+from .decorator import handle_flood
class DaemonClient(Client):
@@ -16,6 +17,7 @@ class DaemonClient(Client):
api_hash, **kwargs)
+ @handle_flood
async def send_text_email(
self,
chat_id: Union[int, str],
@@ -39,6 +41,7 @@ class DaemonClient(Client):
)
+ @handle_flood
async def send_patch_email(
self,
chat_id: Union[int, str],
diff --git a/daemon/packages/decorator.py b/daemon/packages/decorator.py
new file mode 100644
index 0000000..7d7dc39
--- /dev/null
+++ b/daemon/packages/decorator.py
@@ -0,0 +1,38 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# Copyright (C) 2022 Muhammad Rizki <[email protected]>
+#
+
+from pyrogram.errors.exceptions.flood_420 import FloodWait
+from typing import Any, Callable
+from functools import wraps
+import re
+import asyncio
+
+__all__ = ["handle_flood"]
+
+
+def handle_flood(func: Callable[[Any], Any]) -> Callable[[Any], Any]:
+ @wraps(func)
+ async def callback(*args: Any) -> Any:
+ while True:
+ try:
+ return await func(*args)
+ except FloodWait as e:
+ #
+ # Aiee... we hit our limit.
+ # Let's slow down a bit.
+ #
+ _flood_exceptions(e)
+ print("[__handle_telegram_floodwait]: Woken up from flood wait...")
+ return callback
+
+
+async def _flood_exceptions(e):
+ x = re.search(r"A wait of (\d+) seconds is required", str(e))
+ if not x:
+ raise e
+
+ n = int(x.group(1))
+ print(f"[____handle_telegram_floodwait]: Sleeping for {n} seconds due to Telegram limit")
+ await asyncio.sleep(n)
diff --git a/daemon/scraper/bot.py b/daemon/scraper/bot.py
index 1abfa98..38c6e94 100644
--- a/daemon/scraper/bot.py
+++ b/daemon/scraper/bot.py
@@ -37,7 +37,7 @@ class Bot():
def __init__(self, client: DaemonClient, sched: AsyncIOScheduler,
- scraper: Scraper, mutexes: BotMutexes, conn):
+ scraper: Scraper, mutexes: BotMutexes, conn):
self.client = client
self.sched = sched
self.scraper = scraper
@@ -88,7 +88,7 @@ class Bot():
for tg_chat_id in self.TG_CHAT_IDS:
async with self.mutexes.send_to_tg:
should_wait = await self.__send_to_tg(url, mail,
- tg_chat_id)
+ tg_chat_id)
if should_wait:
await asyncio.sleep(1)
@@ -105,7 +105,7 @@ class Bot():
return False
email_id = self.__need_to_send_to_telegram(email_msg_id,
- tg_chat_id)
+ tg_chat_id)
if not email_id:
#
# Email has already been sent to Telegram.
@@ -122,8 +122,9 @@ class Bot():
reply_to, text, url)
else:
text = "#ml\n" + text
- m = await self.__send_text_msg(tg_chat_id, text,
- reply_to, url)
+ m = await self.client.send_text_email(
+ tg_chat_id, text,reply_to, url
+ )
self.db.insert_telegram(email_id, m.chat.id, m.id)
for d, f in files:
@@ -161,40 +162,8 @@ class Bot():
print("[__send_patch_msg]")
tmp, doc, caption, url = utils.prepare_send_patch(mail, text, url)
- ret = await self.__handle_telegram_floodwait(
- self.client.send_patch_email,
- *[tg_chat_id, doc, caption, reply_to, url]
+ ret = await self.client.send_patch_email(
+ tg_chat_id, doc, caption, reply_to, url
)
utils.clean_up_after_send_patch(tmp)
return ret
-
-
- async def __send_text_msg(self, *args):
- return await self.__handle_telegram_floodwait(
- self.client.send_text_email,
- *args
- )
-
-
- async def __handle_telegram_floodwait(self, callback, *args):
- while True:
- try:
- return await callback(*args)
- except pyrogram.errors.exceptions.flood_420.FloodWait as e:
- #
- # Aiee... we hit our limit.
- # Let's slow down a bit.
- #
- await self.____handle_telegram_floodwait(e)
- print("[__handle_telegram_floodwait]: Woken up from flood wait...")
-
-
- async def ____handle_telegram_floodwait(self, e):
- x = str(e)
- x = re.search(r"A wait of (\d+) seconds is required", x)
- if not x:
- raise e
-
- n = int(x.group(1))
- print(f"[____handle_telegram_floodwait]: Sleeping for {n} seconds due to Telegram limit")
- await asyncio.sleep(n)
--
Muhammad Rizki
next prev parent reply other threads:[~2022-07-21 23:30 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-07-21 23:29 [PATCH v3 00/17] Code improvements Muhammad Rizki
2022-07-21 23:29 ` [PATCH v3 01/17] Fix __send_patch_msg function parameter Muhammad Rizki
2022-07-21 23:29 ` [PATCH v3 02/17] Fix import problem Muhammad Rizki
2022-07-21 23:29 ` [PATCH v3 03/17] Add default temporary directory Muhammad Rizki
2022-07-21 23:29 ` [PATCH v3 04/17] Move the Telegram bot session into the storage directory Muhammad Rizki
2022-07-21 23:29 ` [PATCH v3 05/17] daemon: Fix raw lore URL on the inline keyboard button Muhammad Rizki
2022-07-21 23:29 ` [PATCH v3 06/17] daemon: Use traceback.format_exc() to get the error detail Muhammad Rizki
2022-07-21 23:29 ` [PATCH v3 07/17] Re-design send email message to Telegram Muhammad Rizki
2022-07-21 23:29 ` [PATCH v3 08/17] Move ____send_patch_msg Muhammad Rizki
2022-07-21 23:29 ` [PATCH v3 09/17] Move prepare for patch and clean up patch functions Muhammad Rizki
2022-07-21 23:29 ` [PATCH v3 10/17] Create fix_utf8_chars function Muhammad Rizki
2022-07-21 23:29 ` [PATCH v3 11/17] Remove whitespace Muhammad Rizki
2022-07-21 23:29 ` [PATCH v3 12/17] Remove prepare patch and clean up patch Muhammad Rizki
2022-07-21 23:29 ` Muhammad Rizki [this message]
2022-07-21 23:29 ` [PATCH v3 14/17] daemon: Remove __send_patch_msg() Muhammad Rizki
2022-07-21 23:29 ` [PATCH v3 15/17] daemon: Remove unused imports Muhammad Rizki
2022-07-21 23:29 ` [PATCH v3 16/17] Replace send email functions Muhammad Rizki
2022-07-21 23:29 ` [PATCH v3 17/17] Add typing in decorator Muhammad Rizki
2022-07-22 10:57 ` [PATCH v3 00/17] Code improvements Ammar Faizi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
[email protected] \
[email protected] \
[email protected] \
[email protected] \
[email protected] \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox