Skip to content

Commit

Permalink
Handling urls that are only a TLD
Browse files Browse the repository at this point in the history
  • Loading branch information
Yomguithereal committed Oct 28, 2019
1 parent 2921d54 commit ad965d6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
18 changes: 18 additions & 0 deletions src/tld/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,24 @@ def setUp(self):
'tld': 'xn--11b4c3d',
'kwargs': {'fail_silently': True},
},
{
'url': 'http://cloud.fedoraproject.org',
'fld': 'cloud.fedoraproject.org',
'subdomain': '',
'domain': '',
'suffix': 'cloud.fedoraproject.org',
'tld': 'cloud.fedoraproject.org',
'kwargs': {'fail_silently': True}
},
{
'url': 'github.io',
'fld': 'github.io',
'subdomain': '',
'domain': '',
'suffix': 'github.io',
'tld': 'github.io',
'kwargs': {'fail_silently': True, 'fix_protocol': True}
},
]

self.bad_patterns = {
Expand Down
33 changes: 25 additions & 8 deletions src/tld/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ def __init__(self, tld, domain, subdomain, parsed_url):
self.domain = domain
self.subdomain = subdomain
self.parsed_url = parsed_url
self.__fld = "{0}.{1}".format(self.domain, self.tld)

if domain:
self.__fld = "{0}.{1}".format(self.domain, self.tld)
else:
self.__fld = self.tld

@property
def extension(self):
Expand Down Expand Up @@ -350,7 +354,10 @@ def process_url(url,
else:
raise TldDomainNotFound(domain_name=domain_name)

non_zero_i = max(1, len(domain_parts) - tld_length)
if len(domain_parts) == tld_length:
non_zero_i = -1
else:
non_zero_i = max(1, len(domain_parts) - tld_length)

return domain_parts, non_zero_i, parsed_url

Expand Down Expand Up @@ -402,6 +409,9 @@ def get_fld(url,
if domain_parts is None:
return None

if non_zero_i < 0:
return text_type(parsed_url.netloc)

return text_type(".").join(domain_parts[non_zero_i-1:])


Expand Down Expand Up @@ -450,13 +460,20 @@ def get_tld(url,
return None

if not as_object:
if non_zero_i < 0:
return text_type(parsed_url.netloc)
return text_type(".").join(domain_parts[non_zero_i:])

subdomain = text_type(".").join(domain_parts[:non_zero_i-1])
domain = text_type(".").join(
domain_parts[non_zero_i-1:non_zero_i]
)
_tld = text_type(".").join(domain_parts[non_zero_i:])
if non_zero_i < 0:
subdomain = text_type("")
domain = text_type("")
_tld = text_type(parsed_url.netloc)
else:
subdomain = text_type(".").join(domain_parts[:non_zero_i-1])
domain = text_type(".").join(
domain_parts[non_zero_i-1:non_zero_i]
)
_tld = text_type(".").join(domain_parts[non_zero_i:])

return Result(
subdomain=subdomain,
Expand Down Expand Up @@ -522,7 +539,7 @@ def is_tld(value,
:rtype: bool
"""
_tld = get_tld(
url='www.{}'.format(value),
url=value,
fail_silently=True,
fix_protocol=True,
search_public=search_public,
Expand Down

0 comments on commit ad965d6

Please sign in to comment.