From 7490f6cfa3b8b401c00316bdd5a17389a1b6ee01 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Tue, 25 Jun 2019 11:47:45 +0200 Subject: [PATCH] Update the HTML parser: make it easy for asyncio to deal with mentions We can't await coroutines inside HTMLParser overridden methods, such as handle_starttag, because they can't be async. This commit moves the resolve_peer call into the parse method of the HTML class, which can be defined async. --- pyrogram/client/style/html.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/pyrogram/client/style/html.py b/pyrogram/client/style/html.py index 17a5daa6..5617cb54 100644 --- a/pyrogram/client/style/html.py +++ b/pyrogram/client/style/html.py @@ -64,16 +64,8 @@ class Parser(HTMLParser): mention = Parser.MENTION_RE.match(url) if mention: - user_id = int(mention.group(1)) - - try: - user = self.client.resolve_peer(user_id) - except PeerIdInvalid: - entity = types.MessageEntityMentionName - extra["user_id"] = user_id - else: - entity = types.InputMessageEntityMentionName - extra["user_id"] = user + entity = types.InputMessageEntityMentionName + extra["user_id"] = int(mention.group(1)) else: entity = types.MessageEntityTextUrl extra["url"] = url @@ -129,10 +121,21 @@ class HTML: raise ValueError("Unclosed tags: {}".format(", ".join(unclosed_tags))) + entities = [] + + for entity in parser.entities: + if isinstance(entity, types.InputMessageEntityMentionName): + try: + entity.user_id = self.client.resolve_peer(entity.user_id) + except PeerIdInvalid: + continue + + entities.append(entity) + # TODO: OrderedDict to be removed in Python 3.6 return OrderedDict([ ("message", utils.remove_surrogates(parser.text)), - ("entities", parser.entities) + ("entities", entities) ]) @staticmethod