| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from functools import lru_cache |
| 4 |
from urllib.parse import urlparse |
| 5 |
|
| 6 |
from tldextract import tldextract |
| 7 |
|
| 8 |
|
| 9 |
@lru_cache(100) |
| 10 |
def get_domain(url: str) -> str: |
| 11 |
""" |
| 12 |
Return the domain without any subdomain |
| 13 |
|
| 14 |
>>> get_domain("http://blog.example.com") |
| 15 |
'example.com' |
| 16 |
>>> get_domain("http://www.example.com") |
| 17 |
'example.com' |
| 18 |
>>> get_domain("http://deeper.blog.example.co.uk") |
| 19 |
'example.co.uk' |
| 20 |
>>> get_domain("http://127.0.0.1") |
| 21 |
'127.0.0.1' |
| 22 |
""" |
| 23 |
parts = tldextract.extract(url) |
| 24 |
return ".".join(part for part in (parts.domain, parts.suffix) if part) |
| 25 |
|
| 26 |
|
| 27 |
def is_absolute(url: str) -> bool: |
| 28 |
return bool(urlparse(url).netloc) |