Browse Source

amazon.de: support delivery date range

Fabian Peter Hammerle 7 years ago
parent
commit
088a381259
1 changed files with 16 additions and 5 deletions
  1. 16 5
      dingguo/parser/amazon.py

+ 16 - 5
dingguo/parser/amazon.py

@@ -18,8 +18,9 @@ def parse_order_confirmation_mail(mail):
         raise Exception('no amazon order confirmation')
 
     orders = []
+    order_texts = re.split(ur'={32,}', msg_text)[1:-1]
 
-    for order_text in re.split(ur'={32,}', msg_text)[1:-1]:
+    for order_text in order_texts:
 
         order_id = re.search(r'Bestellnummer #(.+)', order_text).group(1)
 
@@ -76,22 +77,32 @@ def parse_order_confirmation_mail(mail):
                 order_text,
                 ).group(1) == '0,00'
         shipping_match = re.finditer(
-                ur'Zustellung:\s+(?P<t>[\W\w]+?)\s+'
+                ur'(Zustellung|Lieferung voraussichtlich):\s+(?P<t>[\W\w]+?)\s+'
                     + ur'Versandart',
-                order_text,
+                order_text if len(order_texts) > 1 else msg_text,
                 re.UNICODE,
                 )
         for shipping_attr in [m.groupdict() for m in shipping_match]:
+            r = shipping_attr['t'].split('-')
             with ioex.setlocale('de_AT.UTF-8'):
                 s = datetime.datetime.strptime(
-                        shipping_attr['t'].encode('utf8'),
+                        r[0].strip().encode('utf8'),
                         '%A, %d. %B',
                         ).replace(
                             year = order.order_date.year,
                             tzinfo = dateutil.tz.gettz('Europe/Berlin'),
                             )
                 assert s.date() > order.order_date
-            e = s + datetime.timedelta(days = 1)
+            if len(r) == 1:
+                e = s + datetime.timedelta(days = 1)
+            else:
+                with ioex.setlocale('de_AT.UTF-8'):
+                    e = datetime.datetime.strptime(
+                            r[1].strip().encode('utf8'),
+                            '%A, %d. %B',
+                            ).replace(year = s.year, tzinfo = s.tzinfo)
+                    e += datetime.timedelta(days = 1)
+            assert e > s
             order.items.append(dingguo.Shipping(
                 price_brutto = dingguo.Sum(0.0, u'EUR'),
                 estimated_arrival_time = ioex.datetimeex.Period(start = s, end = e),