Skip to content

Commit

Permalink
fix: Modified the way dates are handled.
Browse files Browse the repository at this point in the history
  • Loading branch information
MPCodeWriter21 committed Oct 24, 2023
1 parent 202c7b4 commit 2cd8f4a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 21 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ Help this project by [Donation](DONATE.md)

Changes log
-----------
### 1.4.3

+ Fixed issue #5.
+ Dates are now lists.

### 1.4.2

Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,10 @@ python -m build .
Changes
-------

### 1.4.2
### 1.4.3

+ Added new attributes to `WHOIS` class: `emails`, `phone_numbers` and `fax_numbers`
+ Added a new property that contains expiration date(`expiration_date`) to `WHOIS` class
+ Improved WHOIS data parsing (For both human eyes and computer results)
+ Fixed issue #5.
+ Dates are now lists.

Usage Examples:
---------------
Expand Down Expand Up @@ -185,7 +184,8 @@ In order to support this project you can donate some crypto of your choice 8D

[Donate Addresses](https://github.com/MPCodeWriter21/whois21/blob/master/DONATE.md)

Or if you can't, give [this project](https://github.com/MPCodeWriter21/whois21) a star on GitHub :)
Or if you can't, simply give [this project](https://github.com/MPCodeWriter21/whois21)
one star on GitHub :)

References
----------
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ dependencies = [
"requests>=2.28.2",
"importlib_resources>=5.10.2"
]
version = "1.4.2"
version = "1.4.3"

[tool.setuptools]
packages = ["whois21"]
Expand Down
49 changes: 34 additions & 15 deletions whois21/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import json
import socket
import string
from typing import Any, Set, Dict, Tuple, Union, Optional, Sequence
from typing import Any, Set, Dict, List, Tuple, Union, Optional, Sequence
from datetime import datetime

import log21
Expand All @@ -26,7 +26,7 @@
domain_registration_data_lookup,
domain_registration_data_lookup_)

__version__ = '1.4.2'
__version__ = '1.4.3'
__github__ = 'https://github.com/MPCodeWriter21/whois21'
__author__ = 'CodeWriter21'
__email__ = 'CodeWriter21@gmail.com'
Expand Down Expand Up @@ -206,9 +206,9 @@ def __init__(
self.registry_domain_id = None
self.registrar_whois_server = None
self.registrar_url = None
self.updated_date: Optional[datetime] = None
self.creation_date: Optional[datetime] = None
self.expires_date: Optional[datetime] = None
self.updated_date: Optional[List[datetime]] = None
self.creation_date: Optional[List[datetime]] = None
self.expires_date: Optional[List[datetime]] = None
self.registrar_name: Union[str, set] = ''
self.registrar_iana_id = None
self.registrar_abuse_contact_email = None
Expand Down Expand Up @@ -324,16 +324,34 @@ def __set_attrs(self):
self.status = data.get('DOMAIN STATUS', [])
self.name_servers = data.get('NAME SERVER', []) + data.get('NSERVER', [])

def parse_time(date_time: str) -> Union[datetime, None]:
def parse_time(
date_time: Union[str, Sequence[str]]
) -> Union[List[datetime], None]:
"""Parses a date time string.
:param date_time: The date time string.
:return: The parsed date time.
"""
try:
return datetime.fromisoformat(date_time)
except ValueError:
return None
if isinstance(date_time, str):
try:
return [datetime.fromisoformat(date_time)]
except ValueError:
return None
else:
if isinstance(date_time, Sequence):
result = []
for date in date_time:
try:
result.append(datetime.fromisoformat(date))
except ValueError:
pass
except TypeError:
log21.debug(
"WHOIS: __set_attrs: parse_time: TypeError:",
f"{date_time = }, {type(date_time) = }"
)
return result if result else None
return None

# Convert the dates to datetime objects.
updated_date = (
Expand Down Expand Up @@ -577,10 +595,11 @@ def __parse_whois_data(self):
if (key := key_name.strip(STRIP_CHARS).upper()) not in data:
data[key] = value.strip(STRIP_CHARS)
else:
if isinstance(data[key], list):
data[key].append(value.strip(STRIP_CHARS))
elif isinstance(data[key], str):
data[key] = [data[key], value.strip(STRIP_CHARS)]
if (value := value.strip(STRIP_CHARS)):
if isinstance(data[key], list):
data[key].append(value)
elif isinstance(data[key], str):
data[key] = [data[key], value]
i += 1

if not data:
Expand Down Expand Up @@ -765,7 +784,7 @@ def domain(self) -> Union[str, int]:
return self.__domain

@property
def expiration_date(self) -> Optional[datetime]:
def expiration_date(self) -> Optional[List[datetime]]:
"""The expiration date of the domain (if available)."""
return self.expires_date

Expand Down

0 comments on commit 2cd8f4a

Please sign in to comment.