Skip to content

Commit 1a1ba14

Browse files
committed
Expand mastodon support
1 parent 1b8cf4d commit 1a1ba14

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

src/models/europython.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,26 @@ def extract_twitter_url(text: str) -> str:
9393
@staticmethod
9494
def extract_mastodon_url(text: str) -> str:
9595
"""
96-
Extract the Mastodon URL from the answer, handle @username@instance format
96+
Normalize Mastodon handle or URL to the format: https://<instance>/@<username>
9797
"""
98-
if not text.startswith(("https://", "http://")) and text.count("@") == 2:
99-
mastodon_url = f"https://{text.split('@')[2]}/@{text.split('@')[1]}"
100-
else:
101-
mastodon_url = (
102-
f"https://{text.removeprefix('https://').removeprefix('http://')}"
103-
)
98+
text = text.strip().split("?", 1)[0]
99+
100+
# Handle @username@instance or username@instance formats
101+
if "@" in text and not text.startswith("http"):
102+
parts = text.split("@")
103+
if len(parts) == 3: # @username@instance
104+
_, username, instance = parts
105+
elif len(parts) == 2: # username@instance
106+
username, instance = parts
107+
else:
108+
raise ValueError("Invalid Mastodon handle format")
109+
return f"https://{instance}/@{username}"
110+
111+
# Handle full URLs
112+
if text.startswith("http://"):
113+
text = "https://" + text[len("http://") :]
104114

105-
return mastodon_url.split("?")[0]
115+
return text
106116

107117
@staticmethod
108118
def extract_linkedin_url(text: str) -> str:
@@ -126,7 +136,7 @@ def extract_bluesky_url(text: str) -> str:
126136
Returns a normalized BlueSky URL in the form https://bsky.app/profile/<USERNAME>.bsky.social,
127137
or uses the entire domain if it's custom (e.g., .dev).
128138
"""
129-
text = text.split("?", 1)[0].strip()
139+
text = text.strip().split("?", 1)[0]
130140

131141
if text.startswith("https://"):
132142
text = text[8:]

tests/test_social_media_extractions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"https://mastodon.social/@username",
1414
),
1515
("@[email protected]", "https://mastodon.social/@username"),
16+
("[email protected]", "https://mastodon.social/@username"),
1617
],
1718
)
1819
def test_extract_mastodon_url(input_string: str, result: str) -> None:

0 commit comments

Comments
 (0)