social media crossposting tool. 3rd time's the charm
mastodon misskey crossposting bluesky
at next 1.2 kB view raw
1from typing import Any, override 2 3from cross.tokens import LinkToken, MentionToken, TagToken 4from util.html import HTMLToTokensParser 5 6 7class StatusParser(HTMLToTokensParser): 8 def __init__(self, status: dict[str, Any]) -> None: 9 super().__init__() 10 self.tags: set[str] = set(tag["url"] for tag in status.get("tags", [])) 11 self.mentions: set[str] = set(m["url"] for m in status.get("mentions", [])) 12 13 @override 14 def handle_a_endtag(self): 15 label, _attr = self._tag_stack.pop("a") 16 17 href = _attr.get("href") 18 if href: 19 cls = _attr.get("class", "") 20 if cls: 21 if "hashtag" in cls and href in self.tags: 22 tag = label[1:] if label.startswith("#") else label 23 24 self.tokens.append(TagToken(tag=tag)) 25 return 26 if "mention" in cls and href in self.mentions: 27 username = label[1:] if label.startswith("@") else label 28 29 self.tokens.append(MentionToken(username=username, uri=href)) 30 return 31 self.tokens.append(LinkToken(href=href, label=label))