Skip to content

Commit

Permalink
fix: better international handling of amazon (#480)
Browse files Browse the repository at this point in the history
  • Loading branch information
firstof9 authored Jul 13, 2021
2 parents a8ec0b0 + b31a947 commit d3e4233
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
1 change: 1 addition & 0 deletions .github/workflows/pytest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
python -m pip install --upgrade pip
pip install -r requirements_test.txt
sudo apt-get -y install language-pack-it
- name: Generate coverage report
run: |
python -m pytest
Expand Down
2 changes: 1 addition & 1 deletion custom_components/mail_and_packages/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
AMAZON_HUB_CODE = "amazon_hub_code"
AMAZON_HUB_EMAIL = "[email protected]"
AMAZON_HUB_SUBJECT = "(You have a package to pick up)(.*)- (\\d{6})"
AMAZON_TIME_PATTERN = "will arrive:,estimated delivery date is:,guaranteed delivery date is:,Arriving:,Arriver:"
AMAZON_TIME_PATTERN = "will arrive:,estimated delivery date is:,guaranteed delivery date is:,Arriving:,Arriverà:"
AMAZON_EXCEPTION_SUBJECT = "Delivery update:"
AMAZON_EXCEPTION_BODY = "running late"
AMAZON_EXCEPTION = "amazon_exception"
Expand Down
25 changes: 20 additions & 5 deletions custom_components/mail_and_packages/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1177,15 +1177,21 @@ def get_items(
email_msg = email_msg.decode("utf-8", "ignore")
searches = const.AMAZON_TIME_PATTERN.split(",")
for search in searches:
_LOGGER.debug("Looking for: %s", search)
if search not in email_msg:
continue

start = email_msg.find(search) + len(search)
end = email_msg.find("Track your")
end = -1
if email_msg.find("Track your") != -1:
end = email_msg.find("Track your")
elif email_msg.find("Per tracciare il tuo pacco") != -1:
end = email_msg.find("Per tracciare il tuo pacco")

arrive_date = email_msg[start:end].strip()
arrive_date = arrive_date.split(" ")
arrive_date = arrive_date[0:3]
arrive_date[2] = arrive_date[2][:2]
# arrive_date[2] = arrive_date[2][:3]
arrive_date = " ".join(arrive_date).strip()
time_format = None
new_arrive_date = None
Expand All @@ -1205,13 +1211,22 @@ def get_items(
elif arrive_date.endswith(","):
new_arrive_date = arrive_date.rstrip(",")
time_format = "%A, %B %d"
elif "," not in arrive_date:
new_arrive_date = arrive_date
time_format = "%A %d %B"
else:
new_arrive_date = arrive_date
time_format = "%A, %B %d"

dateobj = datetime.datetime.strptime(
new_arrive_date, time_format
)
try:
dateobj = datetime.datetime.strptime(
new_arrive_date, time_format
)
except ValueError as err:
_LOGGER.warn(
"International dates not supported. (%s)", err
)
continue

if (
dateobj.day == datetime.date.today().day
Expand Down
7 changes: 7 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,13 @@ async def test_amazon_shipped_order_it(hass, mock_imap_amazon_shipped_it):
assert result == ["405-5236882-9395563"]


async def test_amazon_shipped_order_it_count(hass, mock_imap_amazon_shipped_it):
with patch("datetime.date") as mock_date:
mock_date.today.return_value = date(2021, 12, 1)
result = get_items(mock_imap_amazon_shipped_it, "count")
assert result == 6


async def test_amazon_search(hass, mock_imap_no_email):
result = amazon_search(mock_imap_no_email, "test/path", hass, "testfilename.jpg")
assert result == 0
Expand Down

0 comments on commit d3e4233

Please sign in to comment.