Coverage for src/platform_adapter/twitter_adapter.py: 100%

1 from __future__ import annotations

2

3 from typing import Any

4

5 from src.platform_adapter.base_adapter import BaseAdapter, PlatformContent

6

7

8 class TwitterAdapter(BaseAdapter):

9 def get_max_length(self) -> int:

10 return 280

11

12 def format_content(self, content: str, **kwargs: Any) -> PlatformContent:

13 if kwargs.get("format") == "thread":

14 return self._create_thread(content)

15 truncated = self.truncate_content(content)

16 return PlatformContent(title=None, body=truncated, metadata={"platform": "twitter"})

17

18 def _create_thread(self, content: str) -> PlatformContent:

19 tweets = []

20 sentences = content.replace("\n", " ").split(". ")

21 current = ""

22 for s in sentences:

23 if len(current) + len(s) < 270:

24 current += " " + s if current else s

25 else:

26 if current:

27 tweets.append(current)

28 current = s

29 if current:

30 tweets.append(current)

31 numbered = [f"{i+1}/{len(tweets)} {t}" for i, t in enumerate(tweets)]

32 return PlatformContent(

33 title=None,

34 body="\n\n---\n\n".join(numbered),

35 metadata={"tweet_count": len(tweets), "is_thread": True},

36 )