support for two different Toot URLs

This commit is contained in:
Daniel Langbein 2022-12-14 20:25:41 +01:00
parent 9b4e7a0603
commit ded58add59

View File

@ -2,13 +2,31 @@ import re
def parse_toot_url(url: str) -> tuple[str, str, str]: def parse_toot_url(url: str) -> tuple[str, str, str]:
if '/users/' in url and '/statuses' in url:
return parse2(url)
else:
return parse1(url)
def parse1(url: str) -> tuple[str, str, str]:
""" """
:param url: E.g. https://mastodon.instance/@user@another.instance/123456 :param url: E.g. https://mastodon.instance/@username@another.instance/123456
:return: instance, username, toot_id :return: instance, username, toot_id
""" """
pattern = re.compile(r'(^https://[^/]+)/([^/]+)/([0-9]+)$') pattern = re.compile(r'(^https://[^/]+)/([^/]+)/([0-9]+)$')
match = pattern.match(url) match = pattern.match(url)
if match is None: if match is None:
raise ValueError(f'Could not parse toot url: {url}') raise ValueError(f'Could not parse toot url: {url}')
return match.group(1), match.group(2), match.group(3) return match.group(1), match.group(2), match.group(3)
def parse2(url: str) -> tuple[str, str, str]:
"""
:param url: E.g. https://mastodon.instance/users/username/statuses/123456
:return: instance, username, toot_id
"""
pattern = re.compile(r'(^https://[^/]+)/users/([^/]+)/statuses/([0-9]+)$')
match = pattern.match(url)
if match is None:
raise ValueError(f'Could not parse toot url: {url}')
return match.group(1), f'@{match.group(2)}', match.group(3)