2018-01-23 14:17:48 +00:00
|
|
|
# Pyrogram - Telegram MTProto API Client Library for Python
|
2019-01-01 11:36:16 +00:00
|
|
|
# Copyright (C) 2017-2019 Dan Tès <https://github.com/delivrance>
|
2018-01-23 14:17:48 +00:00
|
|
|
#
|
|
|
|
# This file is part of Pyrogram.
|
|
|
|
#
|
|
|
|
# Pyrogram is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser General Public License as published
|
|
|
|
# by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Pyrogram is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public License
|
|
|
|
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2018-01-21 23:26:43 +00:00
|
|
|
import re
|
2018-12-31 16:13:50 +00:00
|
|
|
from collections import OrderedDict
|
2018-01-21 23:26:43 +00:00
|
|
|
|
2019-03-26 12:32:30 +00:00
|
|
|
import pyrogram
|
2018-01-21 23:26:43 +00:00
|
|
|
from pyrogram.api.types import (
|
|
|
|
MessageEntityBold as Bold,
|
|
|
|
MessageEntityItalic as Italic,
|
|
|
|
MessageEntityCode as Code,
|
|
|
|
MessageEntityTextUrl as Url,
|
|
|
|
MessageEntityPre as Pre,
|
2019-06-23 19:40:52 +00:00
|
|
|
MessageEntityUnderline as Underline,
|
|
|
|
MessageEntityStrike as Strike,
|
|
|
|
MessageEntityBlockquote as Blockquote,
|
2018-01-23 13:43:12 +00:00
|
|
|
MessageEntityMentionName as MentionInvalid,
|
|
|
|
InputMessageEntityMentionName as Mention,
|
2018-01-21 23:26:43 +00:00
|
|
|
)
|
2019-03-26 12:32:30 +00:00
|
|
|
from pyrogram.errors import PeerIdInvalid
|
2018-01-23 14:17:48 +00:00
|
|
|
from . import utils
|
2018-01-21 23:26:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
class HTML:
|
2018-02-14 07:32:02 +00:00
|
|
|
HTML_RE = re.compile(r"<(\w+)(?: href=([\"'])([^<]+)\2)?>([^>]+)</\1>")
|
2018-01-23 13:43:12 +00:00
|
|
|
MENTION_RE = re.compile(r"tg://user\?id=(\d+)")
|
2018-01-21 23:26:43 +00:00
|
|
|
|
2019-03-26 12:32:30 +00:00
|
|
|
def __init__(self, client: "pyrogram.BaseClient" = None):
|
|
|
|
self.client = client
|
2018-01-21 23:26:43 +00:00
|
|
|
|
2019-03-27 13:59:55 +00:00
|
|
|
async def parse(self, message: str):
|
2019-03-21 12:53:07 +00:00
|
|
|
message = utils.add_surrogates(str(message or ""))
|
2019-03-27 13:59:55 +00:00
|
|
|
entities = []
|
2018-01-21 23:26:43 +00:00
|
|
|
offset = 0
|
|
|
|
|
2019-01-03 19:53:48 +00:00
|
|
|
for match in self.HTML_RE.finditer(message):
|
2018-01-21 23:26:43 +00:00
|
|
|
start = match.start() - offset
|
2018-02-14 07:32:02 +00:00
|
|
|
style, url, body = match.group(1, 3, 4)
|
2018-01-23 13:43:12 +00:00
|
|
|
|
|
|
|
if url:
|
|
|
|
mention = self.MENTION_RE.match(url)
|
|
|
|
|
|
|
|
if mention:
|
|
|
|
user_id = int(mention.group(1))
|
2019-03-26 12:32:30 +00:00
|
|
|
|
|
|
|
try:
|
2019-03-27 13:59:55 +00:00
|
|
|
input_user = await self.client.resolve_peer(user_id)
|
2019-03-26 12:32:30 +00:00
|
|
|
except PeerIdInvalid:
|
|
|
|
input_user = None
|
2018-01-21 23:26:43 +00:00
|
|
|
|
2018-01-23 13:43:12 +00:00
|
|
|
entity = (
|
2019-03-16 15:50:40 +00:00
|
|
|
Mention(offset=start, length=len(body), user_id=input_user)
|
|
|
|
if input_user else MentionInvalid(offset=start, length=len(body), user_id=user_id)
|
2018-01-23 13:43:12 +00:00
|
|
|
)
|
|
|
|
else:
|
2019-03-16 15:50:40 +00:00
|
|
|
entity = Url(offset=start, length=len(body), url=url)
|
2018-01-21 23:26:43 +00:00
|
|
|
else:
|
2018-01-23 13:43:12 +00:00
|
|
|
if style == "b" or style == "strong":
|
2019-03-16 15:50:40 +00:00
|
|
|
entity = Bold(offset=start, length=len(body))
|
2018-01-23 13:43:12 +00:00
|
|
|
elif style == "i" or style == "em":
|
2019-03-16 15:50:40 +00:00
|
|
|
entity = Italic(offset=start, length=len(body))
|
2018-01-23 13:43:12 +00:00
|
|
|
elif style == "code":
|
2019-03-16 15:50:40 +00:00
|
|
|
entity = Code(offset=start, length=len(body))
|
2018-01-23 13:43:12 +00:00
|
|
|
elif style == "pre":
|
2019-03-16 15:50:40 +00:00
|
|
|
entity = Pre(offset=start, length=len(body), language="")
|
2019-06-23 19:40:52 +00:00
|
|
|
elif style == "u":
|
|
|
|
entity = Underline(offset=start, length=len(body))
|
|
|
|
elif style in ["strike", "s", "del"]:
|
|
|
|
entity = Strike(offset=start, length=len(body))
|
|
|
|
elif style == "blockquote":
|
|
|
|
entity = Blockquote(offset=start, length=len(body))
|
2018-01-23 13:43:12 +00:00
|
|
|
else:
|
|
|
|
continue
|
2018-01-21 23:26:43 +00:00
|
|
|
|
|
|
|
entities.append(entity)
|
2019-01-03 19:53:48 +00:00
|
|
|
message = message.replace(match.group(), body)
|
2018-01-23 13:43:12 +00:00
|
|
|
offset += len(style) * 2 + 5 + (len(url) + 8 if url else 0)
|
2018-01-21 23:26:43 +00:00
|
|
|
|
2018-12-31 16:13:50 +00:00
|
|
|
# TODO: OrderedDict to be removed in Python3.6
|
|
|
|
return OrderedDict([
|
2019-01-03 19:53:48 +00:00
|
|
|
("message", utils.remove_surrogates(message)),
|
2018-12-31 16:13:50 +00:00
|
|
|
("entities", entities)
|
|
|
|
])
|
2018-05-11 11:37:49 +00:00
|
|
|
|
|
|
|
def unparse(self, message: str, entities: list):
|
|
|
|
message = utils.add_surrogates(message).strip()
|
|
|
|
offset = 0
|
|
|
|
|
|
|
|
for entity in entities:
|
|
|
|
start = entity.offset + offset
|
|
|
|
type = entity.type
|
|
|
|
url = entity.url
|
|
|
|
user = entity.user
|
|
|
|
sub = message[start: start + entity.length]
|
|
|
|
|
|
|
|
if type == "bold":
|
|
|
|
style = "b"
|
|
|
|
elif type == "italic":
|
|
|
|
style = "i"
|
|
|
|
elif type == "code":
|
|
|
|
style = "code"
|
|
|
|
elif type == "pre":
|
|
|
|
style = "pre"
|
2019-06-23 19:40:52 +00:00
|
|
|
elif type == "underline":
|
|
|
|
style = "u"
|
|
|
|
elif type == "strike":
|
|
|
|
style = "s"
|
|
|
|
elif type == "blockquote":
|
|
|
|
style = "blockquote"
|
2018-05-11 11:37:49 +00:00
|
|
|
elif type == "text_link":
|
|
|
|
offset += 15 + len(url)
|
|
|
|
message = message[:start] + message[start:].replace(
|
|
|
|
sub, "<a href=\"{}\">{}</a>".format(url, sub), 1)
|
|
|
|
continue
|
|
|
|
elif type == "text_mention":
|
|
|
|
offset += 28 + len(str(user.id))
|
|
|
|
message = message[:start] + message[start:].replace(
|
|
|
|
sub, "<a href=\"tg://user?id={}\">{}</a>".format(user.id, sub), 1)
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
continue
|
|
|
|
|
|
|
|
offset += len(style) * 2 + 5
|
|
|
|
message = message[:start] + message[start:].replace(
|
|
|
|
sub, "<{0}>{1}</{0}>".format(style, sub), 1)
|
|
|
|
|
|
|
|
return utils.remove_surrogates(message)
|