Voice et bot modif
This commit is contained in:
parent
189d56026b
commit
7333a22bcd
10774 changed files with 634644 additions and 933308 deletions
|
|
@ -8,13 +8,13 @@ Usage:
|
|||
|
||||

|
||||
"""
|
||||
from os import getenv
|
||||
from warnings import warn
|
||||
|
||||
from requests import Session
|
||||
|
||||
from ..auto import tqdm as tqdm_auto
|
||||
from ..std import TqdmWarning
|
||||
from ..utils import envwrap
|
||||
from .utils_worker import MonoWorker
|
||||
|
||||
__author__ = {"github.com/": ["casperdcl"]}
|
||||
|
|
@ -37,21 +37,23 @@ class TelegramIO(MonoWorker):
|
|||
@property
|
||||
def message_id(self):
|
||||
if hasattr(self, '_message_id'):
|
||||
return self._message_id
|
||||
return self._message_id # pylint: disable=access-member-before-definition
|
||||
try:
|
||||
res = self.session.post(
|
||||
self.API + '%s/sendMessage' % self.token,
|
||||
data={'text': '`' + self.text + '`', 'chat_id': self.chat_id,
|
||||
'parse_mode': 'MarkdownV2'}).json()
|
||||
req = self.session.post(
|
||||
f'{self.API}{self.token}/sendMessage',
|
||||
data={'text': f"`{self.text}`", 'chat_id': self.chat_id,
|
||||
'parse_mode': 'MarkdownV2'})
|
||||
res = req.json()
|
||||
req.raise_for_status()
|
||||
except Exception as e:
|
||||
tqdm_auto.write(str(e))
|
||||
else:
|
||||
if res.get('error_code') == 429:
|
||||
if req.status_code == 429:
|
||||
warn("Creation rate limit: try increasing `mininterval`.",
|
||||
TqdmWarning, stacklevel=2)
|
||||
else:
|
||||
self._message_id = res['result']['message_id']
|
||||
return self._message_id
|
||||
tqdm_auto.write(str(e))
|
||||
else:
|
||||
self._message_id = res['result']['message_id']
|
||||
return self._message_id
|
||||
|
||||
def write(self, s):
|
||||
"""Replaces internal `message_id`'s text with `s`."""
|
||||
|
|
@ -66,8 +68,8 @@ class TelegramIO(MonoWorker):
|
|||
self.text = s
|
||||
try:
|
||||
future = self.submit(
|
||||
self.session.post, self.API + '%s/editMessageText' % self.token,
|
||||
data={'text': '`' + s + '`', 'chat_id': self.chat_id,
|
||||
self.session.post, f'{self.API}{self.token}/editMessageText',
|
||||
data={'text': f"`{s}`", 'chat_id': self.chat_id,
|
||||
'message_id': message_id, 'parse_mode': 'MarkdownV2'})
|
||||
except Exception as e:
|
||||
tqdm_auto.write(str(e))
|
||||
|
|
@ -78,7 +80,7 @@ class TelegramIO(MonoWorker):
|
|||
"""Deletes internal `message_id`."""
|
||||
try:
|
||||
future = self.submit(
|
||||
self.session.post, self.API + '%s/deleteMessage' % self.token,
|
||||
self.session.post, '{self.API}{self.token}/deleteMessage',
|
||||
data={'chat_id': self.chat_id, 'message_id': self.message_id})
|
||||
except Exception as e:
|
||||
tqdm_auto.write(str(e))
|
||||
|
|
@ -86,7 +88,7 @@ class TelegramIO(MonoWorker):
|
|||
return future
|
||||
|
||||
|
||||
class tqdm_telegram(tqdm_auto):
|
||||
class tqdm_telegram(tqdm_auto): # pylint: disable=inconsistent-mro
|
||||
"""
|
||||
Standard `tqdm.auto.tqdm` but also sends updates to a Telegram Bot.
|
||||
May take a few seconds to create (`__init__`).
|
||||
|
|
@ -102,7 +104,8 @@ class tqdm_telegram(tqdm_auto):
|
|||
>>> for i in tqdm(iterable, token='{token}', chat_id='{chat_id}'):
|
||||
... ...
|
||||
"""
|
||||
def __init__(self, *args, **kwargs):
|
||||
@envwrap("tqdm", "telegram", is_method=True)
|
||||
def __init__(self, *args, token=None, chat_id=None, **kwargs):
|
||||
"""
|
||||
Parameters
|
||||
----------
|
||||
|
|
@ -115,19 +118,15 @@ class tqdm_telegram(tqdm_auto):
|
|||
"""
|
||||
if not kwargs.get('disable'):
|
||||
kwargs = kwargs.copy()
|
||||
self.tgio = TelegramIO(
|
||||
kwargs.pop('token', getenv('TQDM_TELEGRAM_TOKEN')),
|
||||
kwargs.pop('chat_id', getenv('TQDM_TELEGRAM_CHAT_ID')))
|
||||
self.tgio = TelegramIO(token, chat_id)
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def display(self, **kwargs):
|
||||
def display(self, **kwargs): # pylint: disable=arguments-differ
|
||||
super().display(**kwargs)
|
||||
fmt = self.format_dict
|
||||
if fmt.get('bar_format', None):
|
||||
fmt['bar_format'] = fmt['bar_format'].replace(
|
||||
'<bar/>', '{bar:10u}').replace('{bar}', '{bar:10u}')
|
||||
else:
|
||||
fmt['bar_format'] = '{l_bar}{bar:10u}{r_bar}'
|
||||
self.tgio.write(self.format_meter(**fmt))
|
||||
|
||||
def clear(self, *args, **kwargs):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue