diff options
author | 2022-09-28 08:29:21 +0100 | |
---|---|---|
committer | 2022-09-28 08:29:21 +0100 | |
commit | b7a468d88c296e6603e7ae1962265b07a24f1782 (patch) | |
tree | 05083dd40bd660084af87afde1f85ae9a373d42c | |
parent | Merge pull request #2232 from python-discord/bot-2231-enhancements (diff) |
add remove_subdomain_from_url in bot util helpers
-rw-r--r-- | bot/utils/helpers.py | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/bot/utils/helpers.py b/bot/utils/helpers.py index 3501a3933..3e45a71a3 100644 --- a/bot/utils/helpers.py +++ b/bot/utils/helpers.py @@ -1,5 +1,6 @@ from abc import ABCMeta from typing import Optional +from urllib.parse import urlparse from discord.ext.commands import CogMeta @@ -30,3 +31,14 @@ def has_lines(string: str, count: int) -> bool: def pad_base64(data: str) -> str: """Return base64 `data` with padding characters to ensure its length is a multiple of 4.""" return data + "=" * (-len(data) % 4) + + +def remove_subdomain_from_url(url: str) -> str: + """Transforms potential relative urls to absolute ones.""" + parsed_url = urlparse(url) + netloc_components = parsed_url.netloc.split(".") + # Eliminate subdomain and use the second level domain and top level domain only + netloc_components[:] = netloc_components[-2:] + netloc = ".".join(netloc_components) + parsed_url = parsed_url._replace(netloc=netloc) + return parsed_url.geturl() |