social media crossposting tool. 3rd time's the charm
mastodon
misskey
crossposting
bluesky
1from dataclasses import dataclass, field
2from cross.attachments import Attachment
3from cross.fragments import Fragment
4from typing import TypeVar
5
6T = TypeVar('T', bound=Attachment)
7
8class AttachmentKeeper:
9 def __init__(self) -> None:
10 self._map: dict[type, Attachment] = {}
11
12 def put(self, cls: type[T], attachment: T) -> None:
13 self._map[cls] = attachment
14
15 def get(self, cls: type[T]) -> T | None:
16 instance = self._map.get(cls)
17 if instance is None:
18 return None
19 if not isinstance(instance, cls):
20 raise TypeError(f"Expected {cls.__name__}, got {type(instance).__name__}")
21 return instance
22
23@dataclass
24class Post:
25 id: str
26 parent_id: str | None
27 text: str # utf-8 text
28 attachments: AttachmentKeeper
29 fragments: list[Fragment] = field(default_factory=list)