@@ -122,16 +122,19 @@ def get_edit_datetime(message: str) -> datetime:
122122 """Parse the edit deadline datetime."""
123123 pattern = fr"(?:You\scan\sedit\sthis\sorder\suntil:?\s)(?P<time>{ REGEX_TIME } )(?:\son\s)(?P<day>{ REGEX_DATE } )(?:{ REGEX_ORDINALS } )\s(?P<month>{ REGEX_MONTH_FULL } )\s(?P<year>{ REGEX_YEAR } )"
124124 raw = re .search (pattern , message )
125+ _LOGGER .debug ("Trying to get edit datetime" )
125126 if raw :
127+ _LOGGER .debug ("First attempt found datetime" )
126128 edit_datetime_raw = raw .group ('year' ) + '-' + raw .group ('month' ) + '-' + raw .group ('day' ) + ' ' + raw .group ('time' )
127129 return datetime .strptime (edit_datetime_raw ,'%Y-%B-%d %H:%M' )
128130 else :
129- pattern = fr"(?:You\scan\sedit\sthis\sorder\suntil:?\s)(?P<time>{ REGEX_NOT_ISO_TIME } )(?:,\s)(?P<day>{ REGEX_DATE } )\s(?P<month>{ REGEX_MONTH_FULL } )\s(?P<year>{ REGEX_YEAR } )"
130- raw = re .search (pattern , message , re .MULTILINE )
131+ _LOGGER .debug ("Trying backup pattern" )
132+ pattern = fr"(?:You\scan\sedit\sthis\sorder\suntil:?\s)(?P<time>{ REGEX_NOT_ISO_TIME } )(?:\son\s|,\s)?(?P<day>{ REGEX_DATE } )(?:{ REGEX_ORDINALS } )?\s?(?P<month>{ REGEX_MONTH_FULL } )\s(?P<year>{ REGEX_YEAR } )"
133+ raw = re .search (pattern , message , re .MULTILINE )
131134 if raw :
132- edit_datetime_raw = raw .group ('year' ) + '-' + raw .group ('month' ) + '-' + raw .group ('day' ) + ' 0' + raw .group ('time' )
135+ _LOGGER .debug ("Second attempt found datetime" )
136+ edit_datetime_raw = raw .group ('year' ) + '-' + raw .group ('month' ) + '-' + raw .group ('day' ) + ' 0' + raw .group ('time' ).replace (" " ,"" ).replace ("." ,":" )
133137 edit_datetime_raw = re .sub (r"pm" ,r"PM" ,re .sub (r"am" ,r"AM" ,edit_datetime_raw ))
134- _LOGGER .debug (edit_datetime_raw )
135138 return datetime .strptime (edit_datetime_raw ,'%Y-%B-%d %I:%M%p' )
136139 _LOGGER .error ("No edit date found in message." )
137140 raise ValueError ("No edit date found in message." )
@@ -177,14 +180,20 @@ def email_triage(self) -> tuple[list[Any], OcadoEmails | None]:
177180 server .close ()
178181 server .logout ()
179182 return message_ids , None
183+ total = len (message_ids [0 ].split ())
184+ _LOGGER .debug ("Beginning triaging of %s emails retrieved." , str (total ))
185+ i = 0
180186 for message_id in reversed (message_ids [0 ].split ()):
187+ i += 1
188+ _LOGGER .debug ("Starting on message %s/%s" , str (i ), str (total ))
181189 result , message_data = server .fetch (message_id ,"(RFC822)" )
182190 if message_data is None :
183191 continue
184192 message_data = message_data [0 ][1 ] # type: ignore
185193 ocado_email = _parse_email (message_id , message_data ) # type: ignore
186194 # If the type of email is a cancellation, add the order number to check for later
187195 if ocado_email .type == "cancellation" :
196+ _LOGGER .debug ("Cancellation email found and added to cancelled orders." )
188197 ocado_cancelled .append (ocado_email .order_number )
189198 # If the order number isn't in the list of cancelled order numbers
190199 if ocado_email .order_number not in ocado_cancelled :
@@ -193,6 +202,7 @@ def email_triage(self) -> tuple[list[Any], OcadoEmails | None]:
193202 # We only care about the most recent receipt
194203 # This is currently broken due to Ocado changes with the PDF no longer included in emails
195204 if ocado_receipt is None :
205+ _LOGGER .debug ("Ocado order (%s) added to receipts." , ocado_email .order_number )
196206 ocado_receipt = OcadoReceipt (ocado_email .date , ocado_email .order_number )
197207 # email_message = BytesParser(policy=policy.default).parsebytes(message_data) # type: ignore
198208 # for part in email_message.iter_attachments():
@@ -246,20 +256,22 @@ def email_triage(self) -> tuple[list[Any], OcadoEmails | None]:
246256 # day_list = getattr(fridge, day) + getattr(cupboard, day)
247257 # setattr(ocado_receipt, day, day_list)
248258 # setattr(ocado_receipt, "date_dict", fridge.date_dict)
249- elif ocado_email .type == "confirmation" :
259+ elif ocado_email .type == "confirmation" or ocado_email . type == "update" :
250260 # Make sure we're not adding an older version of an order we already have
261+ _LOGGER .debug ("Confirmed order is not in the list of confirmed orders? %s" , ocado_email .order_number not in ocado_confirmed_orders )
251262 if ocado_email .order_number not in ocado_confirmed_orders :
252263 ocado_confirmed_orders .append (ocado_email .order_number )
253264 ocado_confirmations .append (ocado_email )
265+ _LOGGER .debug ("Ocado order (%s) added to confirmations." , ocado_email .order_number )
254266 elif ocado_email .type == "new_total" :
255267 # We only care about the most recent new total
256268 if ocado_total is None :
257269 ocado_confirmed_orders .append (ocado_email .order_number )
258270 ocado_total = ocado_email
259-
260-
271+ _LOGGER .debug ("Ocado order (%s) added to totals." , ocado_email .order_number )
261272 server .close ()
262273 server .logout ()
274+ _LOGGER .debug ("Finished with IMAP and closed the connection" )
263275 # It's possible the total order number is repeated, so remove it
264276 ocado_orders = list (set (ocado_confirmed_orders ))
265277 triaged_emails = OcadoEmails (
0 commit comments