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/>.
|
|
|
|
|
2019-06-24 08:07:28 +00:00
|
|
|
import html
|
2018-01-21 23:26:43 +00:00
|
|
|
import re
|
2018-12-31 16:13:50 +00:00
|
|
|
from collections import OrderedDict
|
2019-06-24 08:07:28 +00:00
|
|
|
from html.parser import HTMLParser
|
2018-01-21 23:26:43 +00:00
|
|
|
|
2019-03-26 12:32:30 +00:00
|
|
|
import pyrogram
|
2019-06-24 08:07:28 +00:00
|
|
|
from pyrogram.api import types
|
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
|
|
|
|
|
|
|
|
2019-06-24 08:07:28 +00:00
|
|
|
class Parser(HTMLParser):
|
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-06-24 08:07:28 +00:00
|
|
|
def __init__(self, client: "pyrogram.BaseClient"):
|
|
|
|
super().__init__()
|
|
|
|
|
2019-03-26 12:32:30 +00:00
|
|
|
self.client = client
|
2018-01-21 23:26:43 +00:00
|
|
|
|
2019-06-24 08:07:28 +00:00
|
|
|
self.text = ""
|
|
|
|
self.entities = []
|
2019-06-25 05:08:38 +00:00
|
|
|
self.tag_entities = {}
|
2019-06-24 08:07:28 +00:00
|
|
|
|
|
|
|
def handle_starttag(self, tag, attrs):
|
|
|
|
attrs = dict(attrs)
|
|
|
|
extra = {}
|
|
|
|
|
|
|
|
if tag in ["b", "strong"]:
|
|
|
|
entity = types.MessageEntityBold
|
|
|
|
elif tag in ["i", "em"]:
|
|
|
|
entity = types.MessageEntityItalic
|
|
|
|
elif tag == "u":
|
|
|
|
entity = types.MessageEntityUnderline
|
|
|
|
elif tag in ["s", "del", "strike"]:
|
|
|
|
entity = types.MessageEntityStrike
|
|
|
|
elif tag == "blockquote":
|
|
|
|
entity = types.MessageEntityBlockquote
|
|
|
|
elif tag == "code":
|
|
|
|
entity = types.MessageEntityCode
|
|
|
|
elif tag == "pre":
|
|
|
|
entity = types.MessageEntityPre
|
|
|
|
extra["language"] = ""
|
|
|
|
elif tag == "a":
|
|
|
|
url = attrs.get("href", "")
|
|
|
|
|
|
|
|
mention = Parser.MENTION_RE.match(url)
|
|
|
|
|
|
|
|
if mention:
|
2019-06-25 09:47:45 +00:00
|
|
|
entity = types.InputMessageEntityMentionName
|
|
|
|
extra["user_id"] = int(mention.group(1))
|
2019-06-24 08:07:28 +00:00
|
|
|
else:
|
|
|
|
entity = types.MessageEntityTextUrl
|
|
|
|
extra["url"] = url
|
|
|
|
else:
|
|
|
|
return
|
2018-01-21 23:26:43 +00:00
|
|
|
|
2019-06-25 05:08:38 +00:00
|
|
|
if tag not in self.tag_entities:
|
|
|
|
self.tag_entities[tag] = []
|
|
|
|
|
|
|
|
self.tag_entities[tag].append(entity(offset=len(self.text), length=0, **extra))
|
2018-01-23 13:43:12 +00:00
|
|
|
|
2019-06-24 08:07:28 +00:00
|
|
|
def handle_data(self, data):
|
|
|
|
data = html.unescape(data)
|
2018-01-23 13:43:12 +00:00
|
|
|
|
2019-06-25 05:08:38 +00:00
|
|
|
for entities in self.tag_entities.values():
|
|
|
|
for entity in entities:
|
|
|
|
entity.length += len(data)
|
2019-03-26 12:32:30 +00:00
|
|
|
|
2019-06-24 08:07:28 +00:00
|
|
|
self.text += data
|
2018-01-21 23:26:43 +00:00
|
|
|
|
2019-06-24 08:07:28 +00:00
|
|
|
def handle_endtag(self, tag):
|
2019-06-25 05:41:48 +00:00
|
|
|
try:
|
|
|
|
self.entities.append(self.tag_entities[tag].pop())
|
|
|
|
except (KeyError, IndexError):
|
|
|
|
line, offset = self.getpos()
|
|
|
|
offset += 1
|
|
|
|
|
|
|
|
raise ValueError("Unmatched closing tag </{}> at line {}:{}".format(tag, line, offset))
|
|
|
|
else:
|
|
|
|
if not self.tag_entities[tag]:
|
|
|
|
self.tag_entities.pop(tag)
|
2019-06-24 08:07:28 +00:00
|
|
|
|
|
|
|
def error(self, message):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class HTML:
|
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-06-25 09:48:43 +00:00
|
|
|
async def parse(self, text: str):
|
2019-06-25 03:47:57 +00:00
|
|
|
text = utils.add_surrogates(str(text or "").strip())
|
2018-01-21 23:26:43 +00:00
|
|
|
|
2019-06-24 08:07:28 +00:00
|
|
|
parser = Parser(self.client)
|
|
|
|
parser.feed(text)
|
2019-06-24 12:17:46 +00:00
|
|
|
parser.close()
|
2018-01-23 13:43:12 +00:00
|
|
|
|
2019-06-25 05:41:48 +00:00
|
|
|
if parser.tag_entities:
|
|
|
|
unclosed_tags = []
|
2018-01-23 13:43:12 +00:00
|
|
|
|
2019-06-25 05:41:48 +00:00
|
|
|
for tag, entities in parser.tag_entities.items():
|
|
|
|
unclosed_tags.append("<{}> (x{})".format(tag, len(entities)))
|
2019-03-26 12:32:30 +00:00
|
|
|
|
2019-06-25 05:41:48 +00:00
|
|
|
raise ValueError("Unclosed tags: {}".format(", ".join(unclosed_tags)))
|
2018-01-21 23:26:43 +00:00
|
|
|
|
2019-06-25 09:47:45 +00:00
|
|
|
entities = []
|
|
|
|
|
|
|
|
for entity in parser.entities:
|
|
|
|
if isinstance(entity, types.InputMessageEntityMentionName):
|
|
|
|
try:
|
2019-06-25 09:48:43 +00:00
|
|
|
entity.user_id = await self.client.resolve_peer(entity.user_id)
|
2019-06-25 09:47:45 +00:00
|
|
|
except PeerIdInvalid:
|
2018-01-23 13:43:12 +00:00
|
|
|
continue
|
2018-01-21 23:26:43 +00:00
|
|
|
|
|
|
|
entities.append(entity)
|
|
|
|
|
2019-06-24 08:07:28 +00:00
|
|
|
# TODO: OrderedDict to be removed in Python 3.6
|
2018-12-31 16:13:50 +00:00
|
|
|
return OrderedDict([
|
2019-06-25 03:47:57 +00:00
|
|
|
("message", utils.remove_surrogates(parser.text)),
|
2018-12-31 16:13:50 +00:00
|
|
|
("entities", entities)
|
|
|
|
])
|
2018-05-11 11:37:49 +00:00
|
|
|
|
2019-06-25 08:24:19 +00:00
|
|
|
@staticmethod
|
|
|
|
def unparse(text: str, entities: list):
|
|
|
|
text = utils.add_surrogates(text)
|
|
|
|
copy = text
|
2018-05-11 11:37:49 +00:00
|
|
|
|
|
|
|
for entity in entities:
|
2019-06-25 08:24:19 +00:00
|
|
|
start = entity.offset
|
|
|
|
end = start + entity.length
|
|
|
|
|
2018-05-11 11:37:49 +00:00
|
|
|
type = entity.type
|
2019-06-25 08:24:19 +00:00
|
|
|
|
2018-05-11 11:37:49 +00:00
|
|
|
url = entity.url
|
|
|
|
user = entity.user
|
2019-06-25 08:24:19 +00:00
|
|
|
|
|
|
|
sub = copy[start:end]
|
2018-05-11 11:37:49 +00:00
|
|
|
|
|
|
|
if type == "bold":
|
|
|
|
style = "b"
|
|
|
|
elif type == "italic":
|
|
|
|
style = "i"
|
2019-06-23 19:40:52 +00:00
|
|
|
elif type == "underline":
|
|
|
|
style = "u"
|
|
|
|
elif type == "strike":
|
|
|
|
style = "s"
|
2019-06-25 08:24:19 +00:00
|
|
|
elif type == "code":
|
|
|
|
style = "code"
|
|
|
|
elif type == "pre":
|
|
|
|
style = "pre"
|
2019-06-23 19:40:52 +00:00
|
|
|
elif type == "blockquote":
|
|
|
|
style = "blockquote"
|
2018-05-11 11:37:49 +00:00
|
|
|
elif type == "text_link":
|
2019-06-25 08:24:19 +00:00
|
|
|
text = text[:start] + text[start:].replace(sub, '<a href="{}">{}</a>'.format(url, sub), 1)
|
2018-05-11 11:37:49 +00:00
|
|
|
continue
|
|
|
|
elif type == "text_mention":
|
2019-06-25 08:24:19 +00:00
|
|
|
text = text[:start] + text[start:].replace(
|
|
|
|
sub, '<a href="tg://user?id={}">{}</a>'.format(user.id, sub), 1)
|
2018-05-11 11:37:49 +00:00
|
|
|
continue
|
|
|
|
else:
|
|
|
|
continue
|
|
|
|
|
2019-06-25 08:24:19 +00:00
|
|
|
text = text[:start] + text[start:].replace(sub, "<{0}>{1}</{0}>".format(style, sub), 1)
|
2018-05-11 11:37:49 +00:00
|
|
|
|
2019-06-25 08:24:19 +00:00
|
|
|
return utils.remove_surrogates(text)
|