social media crossposting tool. 3rd time's the charm
mastodon
misskey
crossposting
bluesky
1from typing import Any, override
2import cross.fragments as f
3from util.html import HTMLToFragmentsParser
4
5
6class StatusParser(HTMLToFragmentsParser):
7 def __init__(self, status: dict[str, Any]) -> None:
8 super().__init__()
9 self.tags: set[str] = set(tag["url"] for tag in status.get("tags", []))
10 self.mentions: set[str] = set(m["url"] for m in status.get("mentions", []))
11
12 @override
13 def handle_a_endtag(self):
14 current_end = len(self.builder)
15 start, _attr = self._tag_stack.pop("a")
16
17 href = _attr.get("href")
18 if href and current_end > start:
19 cls = _attr.get("class", "")
20 if cls:
21 if "hashtag" in cls and href in self.tags:
22 tag = self.builder[start:current_end]
23 tag = tag[1:] if tag.startswith(b"#") else tag
24
25 self.fragments.append(
26 f.TagFragment(
27 start=start, end=current_end, tag=tag.decode("utf-8")
28 )
29 )
30 return
31 if "mention" in cls:
32 if href in self.mentions:
33 self.fragments.append(
34 f.MentionFragment(start=start, end=current_end, uri=href)
35 )
36 return
37 self.fragments.append(
38 f.LinkFragment(start=start, end=current_end, url=href)
39 )