File: util.py

package info (click to toggle)
python-url-matcher 0.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 372 kB
  • sloc: python: 627; makefile: 17
file content (28 lines) | stat: -rw-r--r-- 692 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from __future__ import annotations

from functools import lru_cache
from urllib.parse import urlparse

from tldextract import tldextract


@lru_cache(100)
def get_domain(url: str) -> str:
    """
    Return the domain without any subdomain

    >>> get_domain("http://blog.example.com")
    'example.com'
    >>> get_domain("http://www.example.com")
    'example.com'
    >>> get_domain("http://deeper.blog.example.co.uk")
    'example.co.uk'
    >>> get_domain("http://127.0.0.1")
    '127.0.0.1'
    """
    parts = tldextract.extract(url)
    return ".".join(part for part in (parts.domain, parts.suffix) if part)


def is_absolute(url: str) -> bool:
    return bool(urlparse(url).netloc)