Browse Source

amazon: added support for multiple deliveries

Fabian Peter Hammerle 8 years ago
parent
commit
bfb4b812e3
1 changed files with 32 additions and 26 deletions
  1. 32 26
      dingguo/parser/amazon.py

+ 32 - 26
dingguo/parser/amazon.py

@@ -34,32 +34,38 @@ def parse_order_confirmation_mail(mail):
             order_date
             )
 
-        articles_text = order_text.split('Bestellte(r) Artikel:')[1].split('_' * 10)[0].strip()
-        for article_text in re.split(ur'\n\t*\n', articles_text):
-            article_match = re.match(
-                ur' *((?P<quantity>\d+) x )?(?P<name>.*)\n'
-                    + ur'( *von (?P<authors>.*)\n)?'
-                    + ur' *(?P<price_brutto_currency>[A-Z]+) (?P<price_brutto>\d+,\d+)\n'
-                    + ur'( *Zustand: (?P<state>.*)\n)?'
-                    + ur' *Verkauft von: (?P<reseller>.*)'
-                    + ur'(\n *Versand durch (?P<shipper>.*))?',
-                article_text,
-                re.MULTILINE | re.UNICODE
-                )
-            assert article_match is not None, repr(article_text)
-            article = article_match.groupdict()
-            order.items.append(dingguo.Article(
-                name = article['name'],
-                price_brutto = dingguo.Sum(
-                    float(article['price_brutto'].replace(',', '.')),
-                    article['price_brutto_currency']
-                    ),
-                quantity = int(article['quantity']) if article['quantity'] else 1,
-                authors = article['authors'].split(',') if article['authors'] else None,
-                state = article['state'],
-                reseller = article['reseller'],
-                shipper = article['shipper'],
-                ))
+        for articles_text in re.findall(
+                    ur'Bestellte\(r\) Artikel:\s+'
+                        + ur'([\W\w]+?)\s+'
+                        + ur'(Lieferung \d|_{10,})',
+                    order_text,
+                    re.UNICODE,
+                    ):
+            for article_text in re.split(ur'\n\t*\n', articles_text[0]):
+                article_match = re.match(
+                    ur' *((?P<quantity>\d+) x )?(?P<name>.*)\n'
+                        + ur'( *von (?P<authors>.*)\n)?'
+                        + ur' *(?P<price_brutto_currency>[A-Z]+) (?P<price_brutto>\d+,\d+)\n'
+                        + ur'( *Zustand: (?P<state>.*)\n)?'
+                        + ur' *Verkauft von: (?P<reseller>.*)'
+                        + ur'(\n *Versand durch (?P<shipper>.*))?',
+                    article_text,
+                    re.MULTILINE | re.UNICODE
+                    )
+                assert article_match is not None, repr(article_text)
+                article = article_match.groupdict()
+                order.items.append(dingguo.Article(
+                    name = article['name'],
+                    price_brutto = dingguo.Sum(
+                        float(article['price_brutto'].replace(',', '.')),
+                        article['price_brutto_currency']
+                        ),
+                    quantity = int(article['quantity']) if article['quantity'] else 1,
+                    authors = article['authors'].split(',') if article['authors'] else None,
+                    state = article['state'],
+                    reseller = article['reseller'],
+                    shipper = article['shipper'],
+                    ))
 
         orders.append(order)