Skip to content

Commit

Permalink
fix: add regex searching fucntion for amazon emails (#1079)
Browse files Browse the repository at this point in the history
* fix: add regex searching fucntion for amazon emails

* add capost tracking # and out for delivery emails

* properly escape regex searches
  • Loading branch information
firstof9 authored Feb 27, 2025
1 parent 1efc3e1 commit 5ca8db8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
16 changes: 14 additions & 2 deletions custom_components/mail_and_packages/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
"Arrivée :",
"Verwachte bezorgdatum:",
"Votre date de livraison prévue est :",
"Arriving",
]
AMAZON_TIME_PATTERN_END = [
"Previously expected:",
Expand All @@ -166,6 +167,10 @@
"Volg je pakket",
"Je pakket volgen",
]
AMAZON_TIME_PATTERN_REGEX = [
"Arriving (\\w+ \\d+) - (\\w+ \\d+)",
"Arriving (\\w+ \\d+)",
]
AMAZON_EXCEPTION_SUBJECT = "Delivery update:"
AMAZON_EXCEPTION_BODY = "running late"
AMAZON_EXCEPTION = "amazon_exception"
Expand Down Expand Up @@ -296,9 +301,16 @@
"Delivery Notification",
],
},
"capost_delivering": {},
"capost_delivering": {
"email": [
"donotreply-nepasrepondre@notifications.canadapost-postescanada.ca",
],
"subject": [
"Your parcel is out for delivery",
],
},
"capost_packages": {},
"capost_tracking": {},
"capost_tracking": {"pattern": ["\\d{16}"]},
"capost_mail": {
"email": ["donotreply-nepasrepondre@communications.canadapost-postescanada.ca"],
"subject": ["You have mail on the way"],
Expand Down
35 changes: 28 additions & 7 deletions custom_components/mail_and_packages/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
AMAZON_SHIPMENT_TRACKING,
AMAZON_TIME_PATTERN,
AMAZON_TIME_PATTERN_END,
AMAZON_TIME_PATTERN_REGEX,
ATTR_AMAZON_IMAGE,
ATTR_BODY,
ATTR_BODY_COUNT,
Expand Down Expand Up @@ -1500,6 +1501,20 @@ def amazon_date_search(email_msg: str) -> int:
return -1


def amazon_date_regex(email_msg: str) -> str | None:
"""Look for regex strings in email message and return them."""
for body_regex in AMAZON_TIME_PATTERN_REGEX:
pattern = re.compile(rf"{body_regex}")
search = pattern.search(email_msg)
if search is not None and len(search.groups() > 0):
_LOGGER.debug(
"Amazon Regex: %s Count: %s", body_regex, len(search.groups())
)
# return the first group match (first date from a date range)
return search.group(1)
return None


def amazon_date_format(arrive_date: str, lang: str) -> tuple:
"""Return the date format."""
if "de_" in lang:
Expand Down Expand Up @@ -1627,14 +1642,20 @@ def get_items(
if search not in email_msg:
continue

start = email_msg.find(search) + len(search)
end = amazon_date_search(email_msg)
amazon_regex_result = amazon_date_regex(email_msg)
if amazon_regex_result is not None:
_LOGGER.debug("Found regex result: %s", amazon_regex_result)
arrive_date = amazon_regex_result

arrive_date = email_msg[start:end].replace(">", "").strip()
_LOGGER.debug("First pass: %s", arrive_date)
arrive_date = arrive_date.split(" ")
arrive_date = arrive_date[0:3]
arrive_date = " ".join(arrive_date).strip()
else:
start = email_msg.find(search) + len(search)
end = amazon_date_search(email_msg)

arrive_date = email_msg[start:end].replace(">", "").strip()
_LOGGER.debug("First pass: %s", arrive_date)
arrive_date = arrive_date.split(" ")
arrive_date = arrive_date[0:3]
arrive_date = " ".join(arrive_date).strip()

# Get the date object
dateobj = dateparser.parse(arrive_date)
Expand Down

0 comments on commit 5ca8db8

Please sign in to comment.