From ec9941d7ca5d44d7756e6eca430c10f09558613a Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Thu, 13 Aug 2020 17:41:02 +0100 Subject: [PATCH] More actor parsing --- tests.py | 24 ++++++++++++++++++++++++ utils.py | 23 ++++++++++++++++++----- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/tests.py b/tests.py index 947d9d98..f7a27aaa 100644 --- a/tests.py +++ b/tests.py @@ -1585,6 +1585,30 @@ def testActorParsing(): nickname = getNicknameFromActor(actor) assert nickname == 'mynick' + actor = 'https://element/accounts/badger' + domain, port = getDomainFromActor(actor) + assert domain == 'element' + nickname = getNicknameFromActor(actor) + assert nickname == 'badger' + + actor = 'egg@chicken.com' + domain, port = getDomainFromActor(actor) + assert domain == 'chicken.com' + nickname = getNicknameFromActor(actor) + assert nickname == 'egg' + + actor = '@waffle@cardboard' + domain, port = getDomainFromActor(actor) + assert domain == 'cardboard' + nickname = getNicknameFromActor(actor) + assert nickname == 'waffle' + + actor = 'https://astral/channel/sky' + domain, port = getDomainFromActor(actor) + assert domain == 'astral' + nickname = getNicknameFromActor(actor) + assert nickname == 'sky' + actor = 'https://randomain/users/rando' domain, port = getDomainFromActor(actor) assert domain == 'randomain' diff --git a/utils.py b/utils.py index dd1a330b..d5601f65 100644 --- a/utils.py +++ b/utils.py @@ -215,6 +215,8 @@ def getDisplayName(baseDir: str, actor: str, personCache: {}) -> str: def getNicknameFromActor(actor: str) -> str: """Returns the nickname from an actor url """ + if actor.startswith('@'): + actor = actor[1:] if '/users/' not in actor: if '/profile/' in actor: nickStr = actor.split('/profile/')[1].replace('@', '') @@ -240,6 +242,9 @@ def getNicknameFromActor(actor: str) -> str: if '/' in nickStr: nickStr = nickStr.split('/')[0] return nickStr + elif '@' in actor: + nickStr = actor.split('@')[0] + return nickStr return None nickStr = actor.split('/users/')[1].replace('@', '') if '/' not in nickStr: @@ -251,6 +256,8 @@ def getNicknameFromActor(actor: str) -> str: def getDomainFromActor(actor: str) -> (str, int): """Returns the domain name from an actor url """ + if actor.startswith('@'): + actor = actor[1:] port = None prefixes = getProtocolPrefixes() if '/profile/' in actor: @@ -265,16 +272,22 @@ def getDomainFromActor(actor: str) -> (str, int): domain = actor.split('/channel/')[0] for prefix in prefixes: domain = domain.replace(prefix, '') - elif '/users/' not in actor: + elif '/users/' in actor: + domain = actor.split('/users/')[0] + for prefix in prefixes: + domain = domain.replace(prefix, '') + elif '/@' in actor: + domain = actor.split('/@')[0] + for prefix in prefixes: + domain = domain.replace(prefix, '') + elif '@' in actor: + domain = actor.split('@')[1].strip() + else: domain = actor for prefix in prefixes: domain = domain.replace(prefix, '') if '/' in actor: domain = domain.split('/')[0] - else: - domain = actor.split('/users/')[0] - for prefix in prefixes: - domain = domain.replace(prefix, '') if ':' in domain: portStr = domain.split(':')[1] if not portStr.isdigit():