Explorar el Código

yipbee: parse txt

Fabian Peter Hammerle hace 8 años
padre
commit
f6de167155
Se han modificado 1 ficheros con 66 adiciones y 0 borrados
  1. 66 0
      order-confirmation-mail-parser

+ 66 - 0
order-confirmation-mail-parser

@@ -346,6 +346,72 @@ def parse_mytaxi(msg):
 
 def parse_yipbee(msg):
 
+    text = msg.get_payload()[0].get_payload()[0].get_payload(decode = True).decode('utf-8')
+    if not u'Vielen Dank für deine Bestellung bei yipbee' in text:
+        raise Exception('no yipbee confirmation')
+
+    order_match_groups = re.search(
+        ur'[\W\w]+'
+            + ur'BESTELLUNG: (?P<order_id>\w+) vom (?P<order_time>\d\d.\d\d.\d{4} \d\d:\d\d:\d\d)'
+            + ur'[\W\w]+'
+            + ur'GESAMTPREIS\s+'
+            + ur'(?P<articles_and_discount_text>[\W\w]+)'
+            + ur'(?P<summary_text>ARTIKEL [\W\w]+)',
+        text,
+        re.UNICODE
+        ).groupdict()
+
+    order = Order(
+        u'yipbee',
+        order_match_groups['order_id'],
+        datetime.datetime.strptime(order_match_groups['order_time'], '%d.%m.%Y %H:%M:%S'),
+        )
+
+    for article_match in re.finditer(
+            ur'(?P<name>[\w\-\.\:,%\(\) ]+ (Klasse \d|[\w\-\. ]+[^\d ]))'
+                + ur'(?P<total_price>\d+,\d\d) €(?P<quantity>\d)(?P<total_price_2>\d+,\d\d) €',
+            order_match_groups['articles_and_discount_text'].replace('\n', ' '),
+            re.UNICODE,
+            ):
+        article_match_groups = article_match.groupdict()
+        total_price = float(article_match_groups['total_price'].replace(',', '.'))
+        total_price_2 = float(article_match_groups['total_price_2'].replace(',', '.'))
+        assert abs(total_price - total_price_2) < 0.01, 'expected %f, received %f' % (total_price, total_price_2)
+        quantity = int(article_match_groups['quantity'])
+        order.items.append(Article(
+            name = article_match_groups['name'],
+            price_brutto = Sum(round(total_price / quantity, 2), u'EUR'),
+            quantity = quantity,
+            reseller = u'yipbee',
+            shipper = u'yipbee',
+            ))
+
+    articles_price = float(text.split('RABATTE')[0].split('ARTIKEL')[-1].strip().split(' ')[0].replace(',', '.'))
+    assert abs(articles_price - sum([a.price_brutto.value * a.quantity for a in order.items])) < 0.01
+
+    discount_tag = BeautifulSoup.BeautifulSoup(
+        order_match_groups['articles_and_discount_text'],
+        convertEntities = BeautifulSoup.BeautifulSoup.HTML_ENTITIES,
+        ).find('tr')
+    if discount_tag:
+        name_tag, value_tag = discount_tag.findAll('td', recursive = False)
+        value, currency = value_tag.text.split(' ')
+        order.discounts.append(Discount(
+            name = name_tag.text,
+            amount = Sum(float(value.replace(',', '.')) * -1, currency),
+            ))
+
+    delivery_price = order_match_groups['summary_text'].split('VERSAND')[1].split('STEUERN')[0].strip()
+    delivery_price_value, delivery_price_currency = delivery_price.split(' ')
+    order.items.append(Item(
+        name = u'Delivery',
+        price_brutto = Sum(float(delivery_price_value.replace(',', '.')), delivery_price_currency),
+        ))
+
+    return order
+
+def parse_yipbee_html(msg):
+
     html = msg.get_payload()[0].get_payload()[1].get_payload(decode = True)
 
     if not 'yipbee' in html: