Source code for ppc_robot_lib.utils.url

from urllib.parse import urlsplit


WWW_PREFIX = 'www.'


[docs] def extract_domain(url: str, strip_www: bool = False) -> str: """ Extracts a domain from the given URL. :param url: Input URL. :param strip_www: Should we strip the leading ``www.`` from the domain? :return: Domain. """ values = urlsplit(url) if not values.scheme and not values.hostname and values.path and values.path[0] != '/': values = urlsplit(f'//{url}') if values.hostname: domain = values.hostname else: raise ValueError(f'URL {repr(url)} is malformed, the domain could not be extracted.') if strip_www and domain.startswith(WWW_PREFIX): domain = domain[len(WWW_PREFIX) :] return domain