mirror of
https://github.com/Mayuri-Chan/pyrofork.git
synced 2026-01-07 15:34:52 +00:00
Update usages of Parser all around the library
This commit is contained in:
parent
e61bf92627
commit
f05e79e0f4
13 changed files with 32 additions and 39 deletions
|
|
@ -25,7 +25,7 @@ from queue import Queue
|
||||||
from threading import Lock
|
from threading import Lock
|
||||||
|
|
||||||
from pyrogram import __version__
|
from pyrogram import __version__
|
||||||
from ..style import Markdown, HTML
|
from ..parser import Parser
|
||||||
from ...session.internals import MsgId
|
from ...session.internals import MsgId
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -92,8 +92,7 @@ class BaseClient:
|
||||||
|
|
||||||
self.rnd_id = MsgId
|
self.rnd_id = MsgId
|
||||||
|
|
||||||
self.markdown = Markdown(self)
|
self.parser = Parser(self)
|
||||||
self.html = HTML(self)
|
|
||||||
|
|
||||||
self.session = None
|
self.session = None
|
||||||
self.media_sessions = {}
|
self.media_sessions = {}
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,8 @@
|
||||||
# You should have received a copy of the GNU Lesser General Public License
|
# You should have received a copy of the GNU Lesser General Public License
|
||||||
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
import pyrogram
|
import pyrogram
|
||||||
from pyrogram.api import functions
|
from pyrogram.api import functions
|
||||||
from pyrogram.client.ext import BaseClient, utils
|
from pyrogram.client.ext import BaseClient, utils
|
||||||
|
|
@ -26,7 +28,7 @@ class EditInlineText(BaseClient):
|
||||||
self,
|
self,
|
||||||
inline_message_id: str,
|
inline_message_id: str,
|
||||||
text: str,
|
text: str,
|
||||||
parse_mode: str = "",
|
parse_mode: Union[str, None] = "",
|
||||||
disable_web_page_preview: bool = None,
|
disable_web_page_preview: bool = None,
|
||||||
reply_markup: "pyrogram.InlineKeyboardMarkup" = None
|
reply_markup: "pyrogram.InlineKeyboardMarkup" = None
|
||||||
) -> bool:
|
) -> bool:
|
||||||
|
|
@ -55,13 +57,12 @@ class EditInlineText(BaseClient):
|
||||||
Raises:
|
Raises:
|
||||||
RPCError: In case of a Telegram RPC error.
|
RPCError: In case of a Telegram RPC error.
|
||||||
"""
|
"""
|
||||||
style = self.html if parse_mode.lower() == "html" else self.markdown
|
|
||||||
|
|
||||||
return self.send(
|
return self.send(
|
||||||
functions.messages.EditInlineBotMessage(
|
functions.messages.EditInlineBotMessage(
|
||||||
id=utils.unpack_inline_message_id(inline_message_id),
|
id=utils.unpack_inline_message_id(inline_message_id),
|
||||||
no_webpage=disable_web_page_preview or None,
|
no_webpage=disable_web_page_preview or None,
|
||||||
reply_markup=reply_markup.write() if reply_markup else None,
|
reply_markup=reply_markup.write() if reply_markup else None,
|
||||||
**style.parse(text)
|
**self.parser.parse(text, parse_mode)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,7 @@ class EditMessageText(BaseClient):
|
||||||
chat_id: Union[int, str],
|
chat_id: Union[int, str],
|
||||||
message_id: int,
|
message_id: int,
|
||||||
text: str,
|
text: str,
|
||||||
parse_mode: str = "",
|
parse_mode: Union[str, None] = "",
|
||||||
disable_web_page_preview: bool = None,
|
disable_web_page_preview: bool = None,
|
||||||
reply_markup: "pyrogram.InlineKeyboardMarkup" = None
|
reply_markup: "pyrogram.InlineKeyboardMarkup" = None
|
||||||
) -> "pyrogram.Message":
|
) -> "pyrogram.Message":
|
||||||
|
|
@ -63,7 +63,6 @@ class EditMessageText(BaseClient):
|
||||||
Raises:
|
Raises:
|
||||||
RPCError: In case of a Telegram RPC error.
|
RPCError: In case of a Telegram RPC error.
|
||||||
"""
|
"""
|
||||||
style = self.html if parse_mode.lower() == "html" else self.markdown
|
|
||||||
|
|
||||||
r = self.send(
|
r = self.send(
|
||||||
functions.messages.EditMessage(
|
functions.messages.EditMessage(
|
||||||
|
|
@ -71,7 +70,7 @@ class EditMessageText(BaseClient):
|
||||||
id=message_id,
|
id=message_id,
|
||||||
no_webpage=disable_web_page_preview or None,
|
no_webpage=disable_web_page_preview or None,
|
||||||
reply_markup=reply_markup.write() if reply_markup else None,
|
reply_markup=reply_markup.write() if reply_markup else None,
|
||||||
**style.parse(text)
|
**self.parser.parse(text, parse_mode)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@ class SendAnimation(BaseClient):
|
||||||
animation: str,
|
animation: str,
|
||||||
caption: str = "",
|
caption: str = "",
|
||||||
unsave: bool = False,
|
unsave: bool = False,
|
||||||
parse_mode: str = "",
|
parse_mode: Union[str, None] = "",
|
||||||
duration: int = 0,
|
duration: int = 0,
|
||||||
width: int = 0,
|
width: int = 0,
|
||||||
height: int = 0,
|
height: int = 0,
|
||||||
|
|
@ -130,7 +130,6 @@ class SendAnimation(BaseClient):
|
||||||
RPCError: In case of a Telegram RPC error.
|
RPCError: In case of a Telegram RPC error.
|
||||||
"""
|
"""
|
||||||
file = None
|
file = None
|
||||||
style = self.html if parse_mode.lower() == "html" else self.markdown
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if os.path.exists(animation):
|
if os.path.exists(animation):
|
||||||
|
|
@ -168,7 +167,7 @@ class SendAnimation(BaseClient):
|
||||||
reply_to_msg_id=reply_to_message_id,
|
reply_to_msg_id=reply_to_message_id,
|
||||||
random_id=self.rnd_id(),
|
random_id=self.rnd_id(),
|
||||||
reply_markup=reply_markup.write() if reply_markup else None,
|
reply_markup=reply_markup.write() if reply_markup else None,
|
||||||
**style.parse(caption)
|
**self.parser.parse(caption, parse_mode)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
except FilePartMissing as e:
|
except FilePartMissing as e:
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ class SendAudio(BaseClient):
|
||||||
chat_id: Union[int, str],
|
chat_id: Union[int, str],
|
||||||
audio: str,
|
audio: str,
|
||||||
caption: str = "",
|
caption: str = "",
|
||||||
parse_mode: str = "",
|
parse_mode: Union[str, None] = "",
|
||||||
duration: int = 0,
|
duration: int = 0,
|
||||||
performer: str = None,
|
performer: str = None,
|
||||||
title: str = None,
|
title: str = None,
|
||||||
|
|
@ -127,7 +127,6 @@ class SendAudio(BaseClient):
|
||||||
RPCError: In case of a Telegram RPC error.
|
RPCError: In case of a Telegram RPC error.
|
||||||
"""
|
"""
|
||||||
file = None
|
file = None
|
||||||
style = self.html if parse_mode.lower() == "html" else self.markdown
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if os.path.exists(audio):
|
if os.path.exists(audio):
|
||||||
|
|
@ -163,7 +162,7 @@ class SendAudio(BaseClient):
|
||||||
reply_to_msg_id=reply_to_message_id,
|
reply_to_msg_id=reply_to_message_id,
|
||||||
random_id=self.rnd_id(),
|
random_id=self.rnd_id(),
|
||||||
reply_markup=reply_markup.write() if reply_markup else None,
|
reply_markup=reply_markup.write() if reply_markup else None,
|
||||||
**style.parse(caption)
|
**self.parser.parse(caption, parse_mode)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
except FilePartMissing as e:
|
except FilePartMissing as e:
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,7 @@ class SendCachedMedia(BaseClient):
|
||||||
chat_id: Union[int, str],
|
chat_id: Union[int, str],
|
||||||
file_id: str,
|
file_id: str,
|
||||||
caption: str = "",
|
caption: str = "",
|
||||||
parse_mode: str = "",
|
parse_mode: Union[str, None] = "",
|
||||||
disable_notification: bool = None,
|
disable_notification: bool = None,
|
||||||
reply_to_message_id: int = None,
|
reply_to_message_id: int = None,
|
||||||
reply_markup: Union[
|
reply_markup: Union[
|
||||||
|
|
@ -79,7 +79,6 @@ class SendCachedMedia(BaseClient):
|
||||||
Raises:
|
Raises:
|
||||||
RPCError: In case of a Telegram RPC error.
|
RPCError: In case of a Telegram RPC error.
|
||||||
"""
|
"""
|
||||||
style = self.html if parse_mode.lower() == "html" else self.markdown
|
|
||||||
|
|
||||||
r = self.send(
|
r = self.send(
|
||||||
functions.messages.SendMedia(
|
functions.messages.SendMedia(
|
||||||
|
|
@ -89,7 +88,7 @@ class SendCachedMedia(BaseClient):
|
||||||
reply_to_msg_id=reply_to_message_id,
|
reply_to_msg_id=reply_to_message_id,
|
||||||
random_id=self.rnd_id(),
|
random_id=self.rnd_id(),
|
||||||
reply_markup=reply_markup.write() if reply_markup else None,
|
reply_markup=reply_markup.write() if reply_markup else None,
|
||||||
**style.parse(caption)
|
**self.parser.parse(caption, parse_mode)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@ class SendDocument(BaseClient):
|
||||||
document: str,
|
document: str,
|
||||||
thumb: str = None,
|
thumb: str = None,
|
||||||
caption: str = "",
|
caption: str = "",
|
||||||
parse_mode: str = "",
|
parse_mode: Union[str, None] = "",
|
||||||
disable_notification: bool = None,
|
disable_notification: bool = None,
|
||||||
reply_to_message_id: int = None,
|
reply_to_message_id: int = None,
|
||||||
reply_markup: Union[
|
reply_markup: Union[
|
||||||
|
|
@ -113,7 +113,6 @@ class SendDocument(BaseClient):
|
||||||
RPCError: In case of a Telegram RPC error.
|
RPCError: In case of a Telegram RPC error.
|
||||||
"""
|
"""
|
||||||
file = None
|
file = None
|
||||||
style = self.html if parse_mode.lower() == "html" else self.markdown
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if os.path.exists(document):
|
if os.path.exists(document):
|
||||||
|
|
@ -144,7 +143,7 @@ class SendDocument(BaseClient):
|
||||||
reply_to_msg_id=reply_to_message_id,
|
reply_to_msg_id=reply_to_message_id,
|
||||||
random_id=self.rnd_id(),
|
random_id=self.rnd_id(),
|
||||||
reply_markup=reply_markup.write() if reply_markup else None,
|
reply_markup=reply_markup.write() if reply_markup else None,
|
||||||
**style.parse(caption)
|
**self.parser(caption, parse_mode)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
except FilePartMissing as e:
|
except FilePartMissing as e:
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@ class SendMessage(BaseClient):
|
||||||
self,
|
self,
|
||||||
chat_id: Union[int, str],
|
chat_id: Union[int, str],
|
||||||
text: str,
|
text: str,
|
||||||
parse_mode: str = "",
|
parse_mode: Union[str, None] = "",
|
||||||
disable_web_page_preview: bool = None,
|
disable_web_page_preview: bool = None,
|
||||||
disable_notification: bool = None,
|
disable_notification: bool = None,
|
||||||
reply_to_message_id: int = None,
|
reply_to_message_id: int = None,
|
||||||
|
|
@ -74,8 +74,7 @@ class SendMessage(BaseClient):
|
||||||
Raises:
|
Raises:
|
||||||
RPCError: In case of a Telegram RPC error.
|
RPCError: In case of a Telegram RPC error.
|
||||||
"""
|
"""
|
||||||
style = self.html if parse_mode.lower() == "html" else self.markdown
|
message, entities = self.parser.parse(text, parse_mode).values()
|
||||||
message, entities = style.parse(text).values()
|
|
||||||
|
|
||||||
r = self.send(
|
r = self.send(
|
||||||
functions.messages.SendMessage(
|
functions.messages.SendMessage(
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ class SendPhoto(BaseClient):
|
||||||
chat_id: Union[int, str],
|
chat_id: Union[int, str],
|
||||||
photo: str,
|
photo: str,
|
||||||
caption: str = "",
|
caption: str = "",
|
||||||
parse_mode: str = "",
|
parse_mode: Union[str, None] = "",
|
||||||
ttl_seconds: int = None,
|
ttl_seconds: int = None,
|
||||||
disable_notification: bool = None,
|
disable_notification: bool = None,
|
||||||
reply_to_message_id: int = None,
|
reply_to_message_id: int = None,
|
||||||
|
|
@ -112,7 +112,6 @@ class SendPhoto(BaseClient):
|
||||||
RPCError: In case of a Telegram RPC error.
|
RPCError: In case of a Telegram RPC error.
|
||||||
"""
|
"""
|
||||||
file = None
|
file = None
|
||||||
style = self.html if parse_mode.lower() == "html" else self.markdown
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if os.path.exists(photo):
|
if os.path.exists(photo):
|
||||||
|
|
@ -139,7 +138,7 @@ class SendPhoto(BaseClient):
|
||||||
reply_to_msg_id=reply_to_message_id,
|
reply_to_msg_id=reply_to_message_id,
|
||||||
random_id=self.rnd_id(),
|
random_id=self.rnd_id(),
|
||||||
reply_markup=reply_markup.write() if reply_markup else None,
|
reply_markup=reply_markup.write() if reply_markup else None,
|
||||||
**style.parse(caption)
|
**self.parser.parse(caption, parse_mode)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
except FilePartMissing as e:
|
except FilePartMissing as e:
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ class SendVideo(BaseClient):
|
||||||
chat_id: Union[int, str],
|
chat_id: Union[int, str],
|
||||||
video: str,
|
video: str,
|
||||||
caption: str = "",
|
caption: str = "",
|
||||||
parse_mode: str = "",
|
parse_mode: Union[str, None] = "",
|
||||||
duration: int = 0,
|
duration: int = 0,
|
||||||
width: int = 0,
|
width: int = 0,
|
||||||
height: int = 0,
|
height: int = 0,
|
||||||
|
|
@ -129,7 +129,6 @@ class SendVideo(BaseClient):
|
||||||
RPCError: In case of a Telegram RPC error.
|
RPCError: In case of a Telegram RPC error.
|
||||||
"""
|
"""
|
||||||
file = None
|
file = None
|
||||||
style = self.html if parse_mode.lower() == "html" else self.markdown
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if os.path.exists(video):
|
if os.path.exists(video):
|
||||||
|
|
@ -166,7 +165,7 @@ class SendVideo(BaseClient):
|
||||||
reply_to_msg_id=reply_to_message_id,
|
reply_to_msg_id=reply_to_message_id,
|
||||||
random_id=self.rnd_id(),
|
random_id=self.rnd_id(),
|
||||||
reply_markup=reply_markup.write() if reply_markup else None,
|
reply_markup=reply_markup.write() if reply_markup else None,
|
||||||
**style.parse(caption)
|
**self.parser.parse(caption, parse_mode)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
except FilePartMissing as e:
|
except FilePartMissing as e:
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ class SendVoice(BaseClient):
|
||||||
chat_id: Union[int, str],
|
chat_id: Union[int, str],
|
||||||
voice: str,
|
voice: str,
|
||||||
caption: str = "",
|
caption: str = "",
|
||||||
parse_mode: str = "",
|
parse_mode: Union[str, None] = "",
|
||||||
duration: int = 0,
|
duration: int = 0,
|
||||||
disable_notification: bool = None,
|
disable_notification: bool = None,
|
||||||
reply_to_message_id: int = None,
|
reply_to_message_id: int = None,
|
||||||
|
|
@ -110,7 +110,6 @@ class SendVoice(BaseClient):
|
||||||
RPCError: In case of a Telegram RPC error.
|
RPCError: In case of a Telegram RPC error.
|
||||||
"""
|
"""
|
||||||
file = None
|
file = None
|
||||||
style = self.html if parse_mode.lower() == "html" else self.markdown
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if os.path.exists(voice):
|
if os.path.exists(voice):
|
||||||
|
|
@ -142,7 +141,7 @@ class SendVoice(BaseClient):
|
||||||
reply_to_msg_id=reply_to_message_id,
|
reply_to_msg_id=reply_to_message_id,
|
||||||
random_id=self.rnd_id(),
|
random_id=self.rnd_id(),
|
||||||
reply_markup=reply_markup.write() if reply_markup else None,
|
reply_markup=reply_markup.write() if reply_markup else None,
|
||||||
**style.parse(caption)
|
**self.parser.parse(caption, parse_mode)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
except FilePartMissing as e:
|
except FilePartMissing as e:
|
||||||
|
|
|
||||||
|
|
@ -16,9 +16,11 @@
|
||||||
# You should have received a copy of the GNU Lesser General Public License
|
# You should have received a copy of the GNU Lesser General Public License
|
||||||
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
from pyrogram.api import types
|
from pyrogram.api import types
|
||||||
from .input_message_content import InputMessageContent
|
from .input_message_content import InputMessageContent
|
||||||
from ...style import HTML, Markdown
|
from ...parser import Parser
|
||||||
|
|
||||||
|
|
||||||
class InputTextMessageContent(InputMessageContent):
|
class InputTextMessageContent(InputMessageContent):
|
||||||
|
|
@ -38,7 +40,7 @@ class InputTextMessageContent(InputMessageContent):
|
||||||
|
|
||||||
__slots__ = ["message_text", "parse_mode", "disable_web_page_preview"]
|
__slots__ = ["message_text", "parse_mode", "disable_web_page_preview"]
|
||||||
|
|
||||||
def __init__(self, message_text: str, parse_mode: str = "", disable_web_page_preview: bool = None):
|
def __init__(self, message_text: str, parse_mode: Union[str, None] = "", disable_web_page_preview: bool = None):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
self.message_text = message_text
|
self.message_text = message_text
|
||||||
|
|
@ -49,5 +51,5 @@ class InputTextMessageContent(InputMessageContent):
|
||||||
return types.InputBotInlineMessageText(
|
return types.InputBotInlineMessageText(
|
||||||
no_webpage=self.disable_web_page_preview or None,
|
no_webpage=self.disable_web_page_preview or None,
|
||||||
reply_markup=reply_markup.write() if reply_markup else None,
|
reply_markup=reply_markup.write() if reply_markup else None,
|
||||||
**(HTML() if self.parse_mode.lower() == "html" else Markdown()).parse(self.message_text)
|
**(Parser(None)).parse(self.message_text, self.parse_mode)
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ from ..object import Object
|
||||||
from ..update import Update
|
from ..update import Update
|
||||||
from ..user_and_chats.chat import Chat
|
from ..user_and_chats.chat import Chat
|
||||||
from ..user_and_chats.user import User
|
from ..user_and_chats.user import User
|
||||||
from ...style import utils, Markdown, HTML
|
from ...parser import utils, Parser
|
||||||
|
|
||||||
|
|
||||||
class Str(str):
|
class Str(str):
|
||||||
|
|
@ -47,11 +47,11 @@ class Str(str):
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def markdown(self):
|
def markdown(self):
|
||||||
return Markdown.unparse(self, self.entities)
|
return Parser.unparse(self, self.entities, False)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def html(self):
|
def html(self):
|
||||||
return HTML.unparse(self, self.entities)
|
return Parser.unparse(self, self.entities, True)
|
||||||
|
|
||||||
def __getitem__(self, item):
|
def __getitem__(self, item):
|
||||||
return utils.remove_surrogates(utils.add_surrogates(self)[item])
|
return utils.remove_surrogates(utils.add_surrogates(self)[item])
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue