Browse Source

added thomann support

Fabian Peter Hammerle 8 years ago
parent
commit
852b5a3330

+ 4 - 0
dingguo/__init__.py

@@ -288,6 +288,7 @@ class Article(Item):
             color = None,
             delivery_date = None,
             depth = None,
+            features = None,
             height  = None,
             maximum_load = None,
             option = None,
@@ -341,6 +342,9 @@ class Article(Item):
         if maximum_load is not None:
             assert type(maximum_load) is ScalarFigure
             self.maximum_load = maximum_load
+        if features is not None:
+            assert type(features) is unicode
+            self.features = features
         assert delivery_date is None or type(delivery_date) is datetime.date
         self.delivery_date = delivery_date
 

+ 2 - 0
dingguo/parser/__init__.py

@@ -11,6 +11,7 @@ import ingdiba
 import lieferservice
 import mytaxi
 import oebb
+import thomann
 import uber
 import yipbee
 
@@ -22,6 +23,7 @@ order_confirmation_parsers = [
     lieferservice.parse_order_confirmation_mail,
     mytaxi.parse_order_confirmation_mail,
     oebb.parse_order_confirmation_mail,
+    thomann.parse_order_confirmation_mail,
     uber.parse_order_confirmation_mail,
     yipbee.parse_order_confirmation_mail,
     ]

+ 46 - 0
dingguo/parser/thomann.py

@@ -0,0 +1,46 @@
+# -*- coding: utf-8 -*-
+
+import BeautifulSoup
+import datetime
+import dingguo
+import email
+import re
+
+def parse_order_confirmation_mail(mail):
+
+    assert isinstance(mail, email.message.Message)
+
+    doc = BeautifulSoup.BeautifulSoup(
+        mail.get_payload()[1].get_payload(decode = True).decode('utf-8')
+        )
+    
+    if doc.find(text = re.compile(ur'Thomann')) is None:
+        raise Exception('no thomann order confirmation')
+   
+    order = dingguo.Order(
+        customer_id = doc.find(text = re.compile(ur'Kundennummer')).parent.nextSibling.text,
+        order_id = doc.find(text = re.compile(ur'Auftragsnummer')).parent.nextSibling.text,
+        order_date = datetime.datetime.strptime(
+                doc.find(text = re.compile(ur'Auftragsdatum')).parent.nextSibling.text,
+                '%d.%m.%Y %H:%M',
+                ).date(),
+        platform = u'thomann',
+        )
+    
+    for product_id_label_tag in doc.findAll(text = re.compile(ur'Artikelnummer')):
+        article_tag = product_id_label_tag.findParents('td')[1]
+        name_quantity_tag = article_tag.find('strong')
+        quantity, name = re.match(ur'(\d)x (.*)', name_quantity_tag.text).groups()
+        currency, price = re.match(
+                ur'(.) (\d+,\d+)', 
+                product_id_label_tag.parent.nextSibling.text,
+                ).groups()
+        order.items.append(dingguo.Article(
+            features = name_quantity_tag.parent.nextSibling.text,
+            name = name, 
+            price_brutto = dingguo.Sum(float(price.replace(',', '.')), currency),
+            product_id = product_id_label_tag.nextSibling.text,
+            quantity = int(quantity),
+            ))
+
+    return [order]

+ 1 - 0
tests/test_parser.py

@@ -17,6 +17,7 @@ test_data_path = os.path.join(project_root_path, 'tests', 'data')
     ('lieferservice.at', os.path.join(test_data_path, 'lieferservice.at', 'mail_1.eml')),
     ('mytaxi', os.path.join(test_data_path, 'mytaxi', 'mail_1.eml')),
     ('oebb', os.path.join(test_data_path, 'oebb', 'mail_1.eml')),
+    ('thomann', os.path.join(test_data_path, 'thomann', '1.eml')),
     ('uber', os.path.join(test_data_path, 'uber', 'mail_1.eml')),
     ('yipbee', os.path.join(test_data_path, 'yipbee', 'mail_1.eml')),
     ])

+ 24 - 0
tests/test_parser_thomann.py

@@ -0,0 +1,24 @@
+# -*- coding: utf-8 -*-
+
+import pytest
+
+import dingguo.parser.thomann
+import email
+import glob
+import os
+import test_yaml
+import yaml
+
+project_root_path = os.path.realpath(os.path.join(__file__, '..', '..'))
+test_data_path = os.path.join(project_root_path, 'tests', 'data', 'thomann')
+
+@pytest.mark.parametrize('mail_path', glob.glob(os.path.join(test_data_path, '*.eml')))
+def test_parse_confirmation_mail(mail_path):
+    with open(mail_path) as mail:
+        parsed_orders = dingguo.parser.thomann.parse_order_confirmation_mail(
+                email.message_from_file(mail)
+                )
+    with open(mail_path.replace('.eml', '.yml')) as yaml_file:
+        expected_orders = yaml.load(yaml_file.read())
+    assert expected_orders == parsed_orders, \
+            test_yaml.yaml_diff(expected_orders, parsed_orders)

+ 6 - 3
tests/test_yaml.py

@@ -35,6 +35,10 @@ def get_item_b():
 def get_article():
     return dingguo.Article(
             authors = ['a', 'b'],
+            depth = dingguo.ScalarFigure(12.3, u'dm'),
+            features = u'supergeil',
+            height = dingguo.ScalarFigure(123., u'cm'),
+            maximum_load = dingguo.ScalarFigure(40., u'kg'),
             name = u'article name',
             price_brutto = get_sum_a(),
             product_id = u'0815',
@@ -43,9 +47,6 @@ def get_article():
             shipper = u'shipper',
             state = u'goood',
             width = dingguo.ScalarFigure(1.23, u'm'),
-            depth = dingguo.ScalarFigure(12.3, u'dm'),
-            height = dingguo.ScalarFigure(123., u'cm'),
-            maximum_load = dingguo.ScalarFigure(40., u'kg'),
             )
 
 def get_transportation():
@@ -284,6 +285,7 @@ authors:
 - b
 delivery_date: null
 depth: !scalar '12.3 dm'
+features: supergeil
 height: !scalar '123.0 cm'
 maximum_load: !scalar '40.0 kg'
 name: article name
@@ -302,6 +304,7 @@ authors:
 - a
 - b
 delivery_date: null
+features: supergeil
 name: article name
 price_brutto: !sum '1.23 EUR'
 product_id: 0815