-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main'
- Loading branch information
Showing
10 changed files
with
304 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,53 @@ | ||
from pydantic.functional_validators import AfterValidator | ||
from typing_extensions import Annotated | ||
import re | ||
from datetime import datetime | ||
|
||
import dateparser | ||
from pydantic.functional_validators import AfterValidator | ||
from typing_extensions import Annotated | ||
|
||
|
||
class ParserTypeDate: | ||
def __new__(self): | ||
return Annotated[str, AfterValidator(self.validate_type)] | ||
def __new__(cls): | ||
return Annotated[str, AfterValidator(cls.validate_type)] | ||
|
||
def validate_type(date: str): | ||
if not isinstance(date, str): | ||
raise ValueError("Wrong input type") | ||
@staticmethod | ||
def validate_type(date: str) -> str: | ||
# Cast to string incase the date is a datetime float/number | ||
date = str(date) | ||
|
||
# Trim whitespaces | ||
date = date.strip() | ||
|
||
# Make sure it's not empty string | ||
# Make sure it's not an empty string | ||
if len(date) == 0: | ||
raise ValueError("Empty input") | ||
|
||
# Attempt to parse date string | ||
try: | ||
dateparser.parse(date) | ||
return date | ||
except ValueError: | ||
pass | ||
|
||
raise ValueError(f"Unable to parse input as date: {date}") | ||
# Attempt to parse date string using dateparser | ||
parsed_date = dateparser.parse(date) | ||
|
||
if parsed_date is None: | ||
# Remove timezone abbreviation in parentheses if present | ||
date = re.sub(r"\s*\(.*\)$", "", date).strip() | ||
|
||
# List of datetime formats to try | ||
datetime_formats = [ | ||
"%m/%d/%Y %I:%M:%S %p", # 4/30/2024 09:00:02 AM | ||
"%Y-%m-%dT%H:%M:%S", # 2024-04-30T09:00:02 | ||
"%Y-%m-%d %H:%M:%S", # 2024-04-30 09:00:02 | ||
"%B %d, %Y - %I:%M%p", # May 14, 2024 - 2:00pm | ||
"%m/%d/%Y", # 4/30/2024 | ||
] | ||
|
||
# Attempt to parse using datetime with specific formats | ||
for date_format in datetime_formats: | ||
try: | ||
parsed_date = datetime.strptime(date, date_format) | ||
break | ||
except ValueError: | ||
continue | ||
|
||
if parsed_date is None: | ||
raise ValueError(f"Unable to parse input as date: {date}") | ||
|
||
# Return the date in ISO 8601 format | ||
return parsed_date.isoformat() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "harambe-sdk" | ||
version = "0.13.6" | ||
version = "0.14.0" | ||
description = "Data extraction SDK for Playwright 🐒🍌" | ||
authors = ["awtkns <[email protected]>"] | ||
readme = "README.md" | ||
|
@@ -20,6 +20,7 @@ requests = "^2.32.3" | |
playwright-stealth = "^1.0.6" # TODO: self host this package | ||
aiohttp = "^3.9.5" | ||
email-validator = "^2.2.0" | ||
phonenumbers = "^8.13.39" | ||
|
||
[tool.poetry.group.dev.dependencies] | ||
ruff = "^0.4.10" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.