From 438a98fc496f0140d8c93b0d5069093f637ed79d Mon Sep 17 00:00:00 2001 From: firstof9 Date: Tue, 13 Jul 2021 11:16:02 -0700 Subject: [PATCH 1/3] fix: better international handling of amazon * added additional international tests --- custom_components/mail_and_packages/const.py | 2 +- .../mail_and_packages/helpers.py | 25 +++++++++++++++---- tests/test_helpers.py | 7 ++++++ 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/custom_components/mail_and_packages/const.py b/custom_components/mail_and_packages/const.py index 5a536468..be9f5dbe 100644 --- a/custom_components/mail_and_packages/const.py +++ b/custom_components/mail_and_packages/const.py @@ -73,7 +73,7 @@ AMAZON_HUB_CODE = "amazon_hub_code" AMAZON_HUB_EMAIL = "thehub@amazon.com" 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" diff --git a/custom_components/mail_and_packages/helpers.py b/custom_components/mail_and_packages/helpers.py index c832d8f3..b728f678 100644 --- a/custom_components/mail_and_packages/helpers.py +++ b/custom_components/mail_and_packages/helpers.py @@ -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 @@ -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 diff --git a/tests/test_helpers.py b/tests/test_helpers.py index 2d97a3e1..53b39cd3 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -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 From e7c6f0e429f45aebcd7c3301ebb934c58fb8c1c8 Mon Sep 17 00:00:00 2001 From: firstof9 Date: Tue, 13 Jul 2021 11:20:45 -0700 Subject: [PATCH 2/3] update to install Italian locale --- .github/workflows/pytest.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/pytest.yaml b/.github/workflows/pytest.yaml index a7033bc6..4471d456 100644 --- a/.github/workflows/pytest.yaml +++ b/.github/workflows/pytest.yaml @@ -24,6 +24,7 @@ jobs: python -m pip install --upgrade pip pip install -r requirements_test.txt + apt install language-pack-it - name: Generate coverage report run: | python -m pytest From b31a947f79201625364428d064054763045d4345 Mon Sep 17 00:00:00 2001 From: firstof9 Date: Tue, 13 Jul 2021 11:23:07 -0700 Subject: [PATCH 3/3] Update pytest.yaml --- .github/workflows/pytest.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pytest.yaml b/.github/workflows/pytest.yaml index 4471d456..5aff5744 100644 --- a/.github/workflows/pytest.yaml +++ b/.github/workflows/pytest.yaml @@ -24,7 +24,7 @@ jobs: python -m pip install --upgrade pip pip install -r requirements_test.txt - apt install language-pack-it + sudo apt-get -y install language-pack-it - name: Generate coverage report run: | python -m pytest