From ded58add590ed009011941a639f50190ef3fa4b6 Mon Sep 17 00:00:00 2001 From: Daniel Langbein Date: Wed, 14 Dec 2022 20:25:41 +0100 Subject: [PATCH] support for two different Toot URLs --- src/mastodon_toot_follower/mastodon_util.py | 22 +++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/mastodon_toot_follower/mastodon_util.py b/src/mastodon_toot_follower/mastodon_util.py index 6fbc466..d1f5ee6 100644 --- a/src/mastodon_toot_follower/mastodon_util.py +++ b/src/mastodon_toot_follower/mastodon_util.py @@ -2,13 +2,31 @@ import re 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 """ - pattern = re.compile(r'(^https://[^/]+)/([^/]+)/([0-9]+)$') match = pattern.match(url) if match is None: raise ValueError(f'Could not parse toot url: {url}') 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)