Browse Source

banggood: fail when round bracket in article name fixed

Fabian Peter Hammerle 7 years ago
parent
commit
839df0421a
3 changed files with 34 additions and 13 deletions
  1. 7 0
      dingguo/__init__.py
  2. 14 13
      dingguo/parser/banggood.py
  3. 13 0
      tests/test_.py

+ 7 - 0
dingguo/__init__.py

@@ -118,6 +118,13 @@ class ScalarFigure(Figure):
                 value = self.value * factor,
                 )
 
+    def __div__(self, divisor):
+        assert type(divisor) in [int, float]
+        return self.__class__(
+                unit = self.unit,
+                value = self.value / float(divisor),
+                )
+
 class Distance(ScalarFigure):
 
     yaml_tag = u'!distance'

+ 14 - 13
dingguo/parser/banggood.py

@@ -30,23 +30,24 @@ def parse_order_confirmation_mail(mail):
                 )
 
     """ 1 x 10X E27 LED Bulb 7W Warm White 36 SMD 5730 AC 220V Corn Light (10xPOA162664)€23.431 x 5X E14 7W Warm White 36 SMD 5730 LED Corn Light Lamp Bulbs AC 220V (5xPOA162668)€12.31 """
-    articles_text = doc.find(text = re.compile(r'Subtotal of Items')) \
-            .parent.parent.text.replace(' ', ' ') \
-            .split('Subtotal of Items:')[1].split('IMPORTANT NOTICE')[0]
-    for article_match in re.finditer(
-            ur'(?P<quantity>\d+) x (?P<name>[^\(]*) \((\d+x)?(?P<id>[^\)]+)\)'
-                + ur'(?P<currency>[^\d]+)(?P<price>\d+.\d\d)(?P<option>((?!\d+ x).)+)?',
-            articles_text,
+    for price_tag in doc.find(text = re.compile(r'Subtotal of Items')).findParent('td').findAll('strong'):
+        article_match = re.search(
+            ur'^(?P<quantity>\d+) x (?P<name>.*)'
+                + ur' \((\d+x)?(?P<id>[^\)]+)\)',
+            price_tag.previousSibling.replace('&nbsp;', ' ').strip(),
             re.UNICODE,
-            ):
+            )
+        potential_option = price_tag.nextSibling.findNext(text = re.compile(ur'\w+')).replace('&nbsp;', ' ').strip()
+        if (not 'IMPORTANT' in potential_option
+                and not re.search(ur'^\d+ x', potential_option)):
+            option = potential_option
+        else:
+            option = None
         attr = article_match.groupdict()
         order.items.append(dingguo.Article(
             name = attr['name'],
-            option = attr['option'],
-            price_brutto = dingguo.Sum(
-                float(attr['price']) / float(attr['quantity']),
-                attr['currency'],
-                ),
+            option = option,
+            price_brutto = dingguo.Sum.parse_text(price_tag.text) / float(attr['quantity']),
             product_id = attr['id'],
             quantity = int(attr['quantity']),
             ))

+ 13 - 0
tests/test_.py

@@ -110,6 +110,19 @@ def test_scalar_figure_mul(factor_a, factor_b, product):
     assert factor_a_copy == factor_a
     assert factor_b_copy == factor_b
 
+@pytest.mark.parametrize(('dividend', 'divisor', 'quotient'), [
+    [dingguo.Sum(5.0, u'USD'), 2.5, dingguo.Sum(2.0, u'USD')],
+    [dingguo.Sum(5.0, u'USD'), 2, dingguo.Sum(2.5, u'USD')],
+    [dingguo.ScalarFigure(5.0, u'cm'), -0.5, dingguo.ScalarFigure(-10.0, u'cm')],
+    [dingguo.ScalarFigure(1.0, u'kg'), 10, dingguo.ScalarFigure(0.1, u'kg')],
+    ])
+def test_scalar_figure_div(dividend, divisor, quotient):
+    dividend_copy = copy.deepcopy(dividend)
+    divisor_copy = copy.deepcopy(divisor)
+    assert (dividend / divisor) == quotient
+    assert dividend_copy == dividend
+    assert divisor_copy == divisor
+
 @pytest.mark.parametrize(('factor_a', 'factor_b'), [
     [dingguo.Sum(5.0, u'USD'), dingguo.Sum(2.0, u'EUR')],
     [dingguo.ScalarFigure(5.0, u'cm'), dingguo.ScalarFigure(2.0, u'cm')],