From b78efea5903a5f511a3bb22f8de32340ca4b954b Mon Sep 17 00:00:00 2001 From: Guilherme Ruiz <57478888+guigoruiz1@users.noreply.github.com> Date: Sun, 26 Nov 2023 15:45:57 +0000 Subject: [PATCH 1/7] Using REST API directly --- tqdm/contrib/discord.py | 131 ++++++++++++++++++++++++++-------------- 1 file changed, 85 insertions(+), 46 deletions(-) diff --git a/tqdm/contrib/discord.py b/tqdm/contrib/discord.py index fd663f98..d4572e67 100644 --- a/tqdm/contrib/discord.py +++ b/tqdm/contrib/discord.py @@ -1,56 +1,92 @@ -""" -Sends updates to a Discord bot. - -Usage: ->>> from tqdm.contrib.discord import tqdm, trange ->>> for i in trange(10, token='{token}', channel_id='{channel_id}'): -... ... - -![screenshot](https://tqdm.github.io/img/screenshot-discord.png) -""" -import logging from os import getenv +from warnings import warn -try: - from disco.client import Client, ClientConfig -except ImportError: - raise ImportError("Please `pip install disco-py`") +import requests from ..auto import tqdm as tqdm_auto +from ..std import TqdmWarning from .utils_worker import MonoWorker +from ..version import __version__ __author__ = {"github.com/": ["casperdcl"]} -__all__ = ['DiscordIO', 'tqdm_discord', 'tdrange', 'tqdm', 'trange'] +__all__ = ["DiscordIO", "tqdm_discord", "tdrange", "tqdm", "trange"] class DiscordIO(MonoWorker): """Non-blocking file-like IO using a Discord Bot.""" + + API = "https://discord.com/api/v10" + def __init__(self, token, channel_id): """Creates a new message in the given `channel_id`.""" super().__init__() - config = ClientConfig() - config.token = token - client = Client(config) + self.token = token + self.channel_id = channel_id self.text = self.__class__.__name__ + self.message_id + + @property + def message_id(self): + if hasattr(self, "_message_id"): + return self._message_id try: - self.message = client.api.channels_messages_create(channel_id, self.text) + headers = { + "Authorization": f"Bot {self.token}", + "User-Agent": f"TQDM Discord progress bar (http://github.com/tqdm/tqdm, {__version__})", + } + data = {"content": "`" + self.text + "`"} + res = requests.post( + f"{self.API}/channels/{self.channel_id}/messages", + headers=headers, + json=data, + ).json() except Exception as e: tqdm_auto.write(str(e)) - self.message = None + else: + if "id" in res: + self._message_id = res["id"] + return self._message_id def write(self, s): - """Replaces internal `message`'s text with `s`.""" + """Replaces internal `message_id`'s text with `s`.""" if not s: s = "..." - s = s.replace('\r', '').strip() + s = s.replace("\r", "").strip() if s == self.text: - return # skip duplicate message - message = self.message - if message is None: + return # avoid duplicate message Bot error + message_id = self.message_id + if message_id is None: return self.text = s try: - future = self.submit(message.edit, '`' + s + '`') + headers = { + "Authorization": f"Bot {self.token}", + "User-Agent": f"TQDM Discord progress bar (http://github.com/tqdm/tqdm, {__version__})", + } + data = {"content": "`" + s + "`"} + future = self.submit( + requests.patch, + f"{self.API}/channels/{self.channel_id}/messages/{message_id}", + headers=headers, + json=data, + ) + except Exception as e: + tqdm_auto.write(str(e)) + else: + return future + + def delete(self): + """Deletes internal `message_id`.""" + try: + headers = { + "Authorization": f"Bot {self.token}", + "User-Agent": f"TQDM Discord progress bar (http://github.com/tqdm/tqdm, {__version__})", + } + future = self.submit( + requests.delete, + f"{self.API}/channels/{self.channel_id}/messages/{self.message_id}", + headers=headers, + ) except Exception as e: tqdm_auto.write(str(e)) else: @@ -62,45 +98,41 @@ class tqdm_discord(tqdm_auto): Standard `tqdm.auto.tqdm` but also sends updates to a Discord Bot. May take a few seconds to create (`__init__`). - - create a discord bot (not public, no requirement of OAuth2 code - grant, only send message permissions) & invite it to a channel: - - - copy the bot `{token}` & `{channel_id}` and paste below - - >>> from tqdm.contrib.discord import tqdm, trange + >>> from tqdm.contrib.discord import tqdm, drange >>> for i in tqdm(iterable, token='{token}', channel_id='{channel_id}'): ... ... """ + def __init__(self, *args, **kwargs): """ Parameters ---------- - token : str, required. Discord token + token : str, required. Discord bot token [default: ${TQDM_DISCORD_TOKEN}]. - channel_id : int, required. Discord channel ID + channel_id : str, required. Discord channel ID [default: ${TQDM_DISCORD_CHANNEL_ID}]. - mininterval : float, optional. - Minimum of [default: 1.5] to avoid rate limit. See `tqdm.auto.tqdm.__init__` for other parameters. """ - if not kwargs.get('disable'): + if not kwargs.get("disable"): kwargs = kwargs.copy() - logging.getLogger("HTTPClient").setLevel(logging.WARNING) self.dio = DiscordIO( - kwargs.pop('token', getenv("TQDM_DISCORD_TOKEN")), - kwargs.pop('channel_id', getenv("TQDM_DISCORD_CHANNEL_ID"))) - kwargs['mininterval'] = max(1.5, kwargs.get('mininterval', 1.5)) + kwargs.pop("token", getenv("TQDM_DISCORD_TOKEN")), + kwargs.pop("channel_id", getenv("TQDM_DISCORD_CHANNEL_ID")), + ) super().__init__(*args, **kwargs) def display(self, **kwargs): super().display(**kwargs) fmt = self.format_dict - if fmt.get('bar_format', None): - fmt['bar_format'] = fmt['bar_format'].replace( - '', '{bar:10u}').replace('{bar}', '{bar:10u}') + if fmt.get("bar_format", None): + fmt["bar_format"] = ( + fmt["bar_format"] + .replace("", "{bar:10u}") + .replace("{bar}", "{bar:10u}") + ) else: - fmt['bar_format'] = '{l_bar}{bar:10u}{r_bar}' + fmt["bar_format"] = "{l_bar}{bar:10u}{r_bar}" self.dio.write(self.format_meter(**fmt)) def clear(self, *args, **kwargs): @@ -108,6 +140,13 @@ class tqdm_discord(tqdm_auto): if not self.disable: self.dio.write("") + def close(self): + if self.disable: + return + super().close() + if not (self.leave or (self.leave is None and self.pos == 0)): + self.dio.delete() + def tdrange(*args, **kwargs): """Shortcut for `tqdm.contrib.discord.tqdm(range(*args), **kwargs)`.""" From 03b90e444ed18b88aed710371309c271c25ac391 Mon Sep 17 00:00:00 2001 From: Guilherme Ruiz <57478888+guigoruiz1@users.noreply.github.com> Date: Thu, 30 Nov 2023 18:07:32 +0000 Subject: [PATCH 2/7] Using correct user agent --- tqdm/contrib/discord.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tqdm/contrib/discord.py b/tqdm/contrib/discord.py index d4572e67..56f7d853 100644 --- a/tqdm/contrib/discord.py +++ b/tqdm/contrib/discord.py @@ -8,7 +8,7 @@ from ..std import TqdmWarning from .utils_worker import MonoWorker from ..version import __version__ -__author__ = {"github.com/": ["casperdcl"]} +__author__ = {"github.com/": ["casperdcl","guigoruiz1"]} __all__ = ["DiscordIO", "tqdm_discord", "tdrange", "tqdm", "trange"] @@ -16,6 +16,7 @@ class DiscordIO(MonoWorker): """Non-blocking file-like IO using a Discord Bot.""" API = "https://discord.com/api/v10" + user_agent = f"TQDM Discord progress bar (https://tqdm.github.io, {__version__})" def __init__(self, token, channel_id): """Creates a new message in the given `channel_id`.""" @@ -32,7 +33,7 @@ class DiscordIO(MonoWorker): try: headers = { "Authorization": f"Bot {self.token}", - "User-Agent": f"TQDM Discord progress bar (http://github.com/tqdm/tqdm, {__version__})", + "User-Agent": self.user_agent, } data = {"content": "`" + self.text + "`"} res = requests.post( @@ -61,7 +62,7 @@ class DiscordIO(MonoWorker): try: headers = { "Authorization": f"Bot {self.token}", - "User-Agent": f"TQDM Discord progress bar (http://github.com/tqdm/tqdm, {__version__})", + "User-Agent": self.user_agent, } data = {"content": "`" + s + "`"} future = self.submit( @@ -80,7 +81,7 @@ class DiscordIO(MonoWorker): try: headers = { "Authorization": f"Bot {self.token}", - "User-Agent": f"TQDM Discord progress bar (http://github.com/tqdm/tqdm, {__version__})", + "User-Agent": self.user_agent, } future = self.submit( requests.delete, From f1129e70c9983c527413e50e63016d4d885c51d3 Mon Sep 17 00:00:00 2001 From: Casper da Costa-Luis Date: Mon, 28 Oct 2024 00:33:45 +0000 Subject: [PATCH 3/7] restore original docs, misc linting --- tqdm/contrib/discord.py | 88 +++++++++++++++++++---------------------- 1 file changed, 41 insertions(+), 47 deletions(-) diff --git a/tqdm/contrib/discord.py b/tqdm/contrib/discord.py index 56f7d853..bafd8b61 100644 --- a/tqdm/contrib/discord.py +++ b/tqdm/contrib/discord.py @@ -1,3 +1,13 @@ +""" +Sends updates to a Discord bot. + +Usage: +>>> from tqdm.contrib.discord import tqdm, trange +>>> for i in trange(10, token='{token}', channel_id='{channel_id}'): +... ... + +![screenshot](https://tqdm.github.io/img/screenshot-discord.png) +""" from os import getenv from warnings import warn @@ -5,16 +15,15 @@ import requests from ..auto import tqdm as tqdm_auto from ..std import TqdmWarning -from .utils_worker import MonoWorker from ..version import __version__ +from .utils_worker import MonoWorker -__author__ = {"github.com/": ["casperdcl","guigoruiz1"]} -__all__ = ["DiscordIO", "tqdm_discord", "tdrange", "tqdm", "trange"] +__author__ = {"github.com/": ["casperdcl", "guigoruiz1"]} +__all__ = ['DiscordIO', 'tqdm_discord', 'tdrange', 'tqdm', 'trange'] class DiscordIO(MonoWorker): """Non-blocking file-like IO using a Discord Bot.""" - API = "https://discord.com/api/v10" user_agent = f"TQDM Discord progress bar (https://tqdm.github.io, {__version__})" @@ -28,31 +37,27 @@ class DiscordIO(MonoWorker): @property def message_id(self): - if hasattr(self, "_message_id"): + if hasattr(self, '_message_id'): return self._message_id try: - headers = { - "Authorization": f"Bot {self.token}", - "User-Agent": self.user_agent, - } - data = {"content": "`" + self.text + "`"} res = requests.post( - f"{self.API}/channels/{self.channel_id}/messages", - headers=headers, - json=data, - ).json() + f'{self.API}/channels/{self.channel_id}/messages', + headers={'Authorization': f'Bot {self.token}', 'User-Agent': self.UA}, + json={'content': f"`{self.text}`"}).json() except Exception as e: tqdm_auto.write(str(e)) else: - if "id" in res: - self._message_id = res["id"] + if 'id' not in res: + warn("Message not sent", TqdmWarning, stacklevel=2) + else: + self._message_id = res['id'] return self._message_id def write(self, s): """Replaces internal `message_id`'s text with `s`.""" if not s: s = "..." - s = s.replace("\r", "").strip() + s = s.replace('\r', '').strip() if s == self.text: return # avoid duplicate message Bot error message_id = self.message_id @@ -60,17 +65,11 @@ class DiscordIO(MonoWorker): return self.text = s try: - headers = { - "Authorization": f"Bot {self.token}", - "User-Agent": self.user_agent, - } - data = {"content": "`" + s + "`"} future = self.submit( requests.patch, - f"{self.API}/channels/{self.channel_id}/messages/{message_id}", - headers=headers, - json=data, - ) + f'{self.API}/channels/{self.channel_id}/messages/{message_id}', + headers={'Authorization': f'Bot {self.token}', 'User-Agent': self.UA}, + json={'content': f"`{self.text}`"}) except Exception as e: tqdm_auto.write(str(e)) else: @@ -79,15 +78,10 @@ class DiscordIO(MonoWorker): def delete(self): """Deletes internal `message_id`.""" try: - headers = { - "Authorization": f"Bot {self.token}", - "User-Agent": self.user_agent, - } future = self.submit( requests.delete, - f"{self.API}/channels/{self.channel_id}/messages/{self.message_id}", - headers=headers, - ) + f'{self.API}/channels/{self.channel_id}/messages/{self.message_id}', + headers={'Authorization': f'Bot {self.token}', 'User-Agent': self.UA}) except Exception as e: tqdm_auto.write(str(e)) else: @@ -99,41 +93,41 @@ class tqdm_discord(tqdm_auto): Standard `tqdm.auto.tqdm` but also sends updates to a Discord Bot. May take a few seconds to create (`__init__`). - >>> from tqdm.contrib.discord import tqdm, drange + - create a discord bot (not public, no requirement of OAuth2 code + grant, only send message permissions) & invite it to a channel: + + - copy the bot `{token}` & `{channel_id}` and paste below + + >>> from tqdm.contrib.discord import tqdm, trange >>> for i in tqdm(iterable, token='{token}', channel_id='{channel_id}'): ... ... """ - def __init__(self, *args, **kwargs): """ Parameters ---------- token : str, required. Discord bot token [default: ${TQDM_DISCORD_TOKEN}]. - channel_id : str, required. Discord channel ID + channel_id : int, required. Discord channel ID [default: ${TQDM_DISCORD_CHANNEL_ID}]. See `tqdm.auto.tqdm.__init__` for other parameters. """ - if not kwargs.get("disable"): + if not kwargs.get('disable'): kwargs = kwargs.copy() self.dio = DiscordIO( - kwargs.pop("token", getenv("TQDM_DISCORD_TOKEN")), - kwargs.pop("channel_id", getenv("TQDM_DISCORD_CHANNEL_ID")), - ) + kwargs.pop('token', getenv('TQDM_DISCORD_TOKEN')), + kwargs.pop('channel_id', getenv('TQDM_DISCORD_CHANNEL_ID'))) super().__init__(*args, **kwargs) def display(self, **kwargs): super().display(**kwargs) fmt = self.format_dict - if fmt.get("bar_format", None): - fmt["bar_format"] = ( - fmt["bar_format"] - .replace("", "{bar:10u}") - .replace("{bar}", "{bar:10u}") - ) + if fmt.get('bar_format', None): + fmt['bar_format'] = fmt['bar_format'].replace( + '', '{bar:10u}').replace('{bar}', '{bar:10u}') else: - fmt["bar_format"] = "{l_bar}{bar:10u}{r_bar}" + fmt['bar_format'] = '{l_bar}{bar:10u}{r_bar}' self.dio.write(self.format_meter(**fmt)) def clear(self, *args, **kwargs): From 670840021ddea4908a2e68144ce6c009d7e16e96 Mon Sep 17 00:00:00 2001 From: Casper da Costa-Luis Date: Mon, 28 Oct 2024 00:44:15 +0000 Subject: [PATCH 4/7] use requests.Session --- tqdm/contrib/discord.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tqdm/contrib/discord.py b/tqdm/contrib/discord.py index bafd8b61..59d63e18 100644 --- a/tqdm/contrib/discord.py +++ b/tqdm/contrib/discord.py @@ -11,7 +11,7 @@ Usage: from os import getenv from warnings import warn -import requests +from requests import Session from ..auto import tqdm as tqdm_auto from ..std import TqdmWarning @@ -32,6 +32,7 @@ class DiscordIO(MonoWorker): super().__init__() self.token = token self.channel_id = channel_id + self.session = Session() self.text = self.__class__.__name__ self.message_id @@ -40,7 +41,7 @@ class DiscordIO(MonoWorker): if hasattr(self, '_message_id'): return self._message_id try: - res = requests.post( + res = self.session.post( f'{self.API}/channels/{self.channel_id}/messages', headers={'Authorization': f'Bot {self.token}', 'User-Agent': self.UA}, json={'content': f"`{self.text}`"}).json() @@ -66,7 +67,7 @@ class DiscordIO(MonoWorker): self.text = s try: future = self.submit( - requests.patch, + self.session.patch, f'{self.API}/channels/{self.channel_id}/messages/{message_id}', headers={'Authorization': f'Bot {self.token}', 'User-Agent': self.UA}, json={'content': f"`{self.text}`"}) @@ -79,7 +80,7 @@ class DiscordIO(MonoWorker): """Deletes internal `message_id`.""" try: future = self.submit( - requests.delete, + self.session.delete, f'{self.API}/channels/{self.channel_id}/messages/{self.message_id}', headers={'Authorization': f'Bot {self.token}', 'User-Agent': self.UA}) except Exception as e: From 61365d8321ae4ca433d2c6cda770a73a8e0e62cb Mon Sep 17 00:00:00 2001 From: Casper da Costa-Luis Date: Mon, 28 Oct 2024 00:51:33 +0000 Subject: [PATCH 5/7] handle rate limit --- tqdm/contrib/discord.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tqdm/contrib/discord.py b/tqdm/contrib/discord.py index 59d63e18..d3e94284 100644 --- a/tqdm/contrib/discord.py +++ b/tqdm/contrib/discord.py @@ -48,8 +48,9 @@ class DiscordIO(MonoWorker): except Exception as e: tqdm_auto.write(str(e)) else: - if 'id' not in res: - warn("Message not sent", TqdmWarning, stacklevel=2) + if res.get('error_code') == 429: + warn("Creation rate limit: try increasing `mininterval`.", + TqdmWarning, stacklevel=2) else: self._message_id = res['id'] return self._message_id From 1db24b4ff442c43752cf56a55b1782998c76801c Mon Sep 17 00:00:00 2001 From: Casper da Costa-Luis Date: Mon, 28 Oct 2024 00:55:12 +0000 Subject: [PATCH 6/7] better user-agent --- tqdm/contrib/discord.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tqdm/contrib/discord.py b/tqdm/contrib/discord.py index d3e94284..574baa84 100644 --- a/tqdm/contrib/discord.py +++ b/tqdm/contrib/discord.py @@ -12,6 +12,7 @@ from os import getenv from warnings import warn from requests import Session +from requests.utils import default_user_agent from ..auto import tqdm as tqdm_auto from ..std import TqdmWarning @@ -25,7 +26,7 @@ __all__ = ['DiscordIO', 'tqdm_discord', 'tdrange', 'tqdm', 'trange'] class DiscordIO(MonoWorker): """Non-blocking file-like IO using a Discord Bot.""" API = "https://discord.com/api/v10" - user_agent = f"TQDM Discord progress bar (https://tqdm.github.io, {__version__})" + UA = f"tqdm (https://tqdm.github.io, {__version__}) {default_user_agent()}" def __init__(self, token, channel_id): """Creates a new message in the given `channel_id`.""" From 8aa9470e485a90679936d3781a4f953cf5afa8f4 Mon Sep 17 00:00:00 2001 From: Casper da Costa-Luis Date: Mon, 28 Oct 2024 02:42:32 +0000 Subject: [PATCH 7/7] add discord requests dep --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index ec1a63e4..5651e88e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -85,6 +85,7 @@ dependencies = ['colorama; platform_system == "Windows"'] [project.optional-dependencies] dev = ["pytest>=6", "pytest-cov", "pytest-timeout", "pytest-xdist"] +discord = ["requests"] slack = ["slack-sdk"] telegram = ["requests"] notebook = ["ipywidgets>=6"]