Browse Source

added support for h&m

Fabian Peter Hammerle 8 years ago
parent
commit
1d49539004
5 changed files with 92 additions and 13 deletions
  1. 8 13
      dingguo/__init__.py
  2. 2 0
      dingguo/parser/__init__.py
  3. 57 0
      dingguo/parser/hm.py
  4. 1 0
      tests/test_parser.py
  5. 24 0
      tests/test_parser_hm.py

+ 8 - 13
dingguo/__init__.py

@@ -285,12 +285,14 @@ class Article(Item):
     def __init__(
             self,
             authors = None,
+            color = None,
             delivery_date = None,
             option = None,
             product_id = None,
             quantity = None,
             reseller = None,
             shipper = None,
+            size = None,
             state = None,
             **kwargs
             ):
@@ -317,22 +319,15 @@ class Article(Item):
         if option is not None:
             assert type(option) is unicode
             self.option = option
+        if color is not None:
+            assert type(color) is unicode
+            self.color = color
+        if size is not None:
+            assert type(size) is unicode
+            self.size = size
         assert delivery_date is None or type(delivery_date) is datetime.date
         self.delivery_date = delivery_date
 
-    def dict_repr(self):
-        attr = super(Article, self).dict_repr()
-        attr.update({
-            'delivery_date': self.delivery_date,
-            'quantity': self.quantity,
-            'reseller': self.reseller if hasattr(self, 'reseller') else None,
-            'shipper': self.shipper if hasattr(self, 'shipper') else None,
-            'state': self.state if hasattr(self, 'state') else None,
-            })
-        if hasattr(self, 'authors') and len(self.authors) > 0:
-            attr['authors'] = self.authors
-        return attr
-
 class Transportation(Item):
 
     yaml_tag = u'!transportation'

+ 2 - 0
dingguo/parser/__init__.py

@@ -5,6 +5,7 @@ import traceback
 
 import amazon
 import banggood
+import hm
 import ingdiba
 import lieferservice
 import mytaxi
@@ -15,6 +16,7 @@ import yipbee
 order_confirmation_parsers = [
     amazon.parse_order_confirmation_mail,
     banggood.parse_order_confirmation_mail,
+    hm.parse_order_confirmation_mail,
     lieferservice.parse_order_confirmation_mail,
     mytaxi.parse_order_confirmation_mail,
     oebb.parse_order_confirmation_mail,

+ 57 - 0
dingguo/parser/hm.py

@@ -0,0 +1,57 @@
+# -*- coding: utf-8 -*-
+
+import datetime
+import dingguo
+import string
+import email
+import ioex
+import re
+import BeautifulSoup
+
+def parse_order_confirmation_mail(mail):
+
+    assert isinstance(mail, email.message.Message)
+
+    html_mail = mail.get_payload()[0].get_payload()[1].get_payload(decode = True)
+
+    doc = BeautifulSoup.BeautifulSoup(html_mail)
+
+    order_attr = re.search(
+            ur'Bestelldatum:(?P<date>\d+)'
+                + ur'Benutzername:(?P<user>.*)'
+                + ur'Name',
+            doc.find(text = 'Bestelldatum:').findParent('table').text,
+            re.UNICODE,
+            ).groupdict()
+    user = ''.join([c if (c in string.printable) else '' for c in order_attr['user']])
+    order = dingguo.Order(
+            customer_id = user,
+            order_date = datetime.datetime.strptime(order_attr['date'], '%d%m%y'),
+            order_id = u'%s-%s' % (user, order_attr['date']),
+            platform = u'hm',
+            )
+
+    for article_tag in [t.findParents('tr')[2] for t in doc.findAll(text = 'Art.-Nr.:')]:
+        article_subtags = article_tag.findAll(recursive = False)
+        article = re.search(
+            ur'Art.-Nr.:(?P<id>[\d-]+)'
+                + ur'Beschreibung:(?P<label>.*)'
+                + ur'Größe:(?P<size>.*)'
+                + ur'Farbe:(?P<color>[^\d]+)',
+            article_subtags[1].text,
+            re.UNICODE,
+            ).groupdict()
+        order.items.append(dingguo.Article(
+            color = article['color'],
+            name = article['label'],
+            price_brutto = dingguo.Sum(
+                float(article_subtags[3].text.replace(',', '.')), 
+                u'EUR',
+                ),
+            product_id = article['id'],
+            quantity = int(article_subtags[2].text),
+            reseller = u'H&M',
+            size = article['size'],
+            ))
+
+    return [order]

+ 1 - 0
tests/test_parser.py

@@ -12,6 +12,7 @@ test_data_path = os.path.join(project_root_path, 'tests', 'data')
 @pytest.mark.parametrize('platform,mail_path', [
     ('amazon.de', os.path.join(test_data_path, 'amazon', 'mail_1.eml')),
     ('banggood', os.path.join(test_data_path, 'banggood', '1.eml')),
+    ('hm', os.path.join(test_data_path, 'hm', '1.eml')),
     ('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')),

+ 24 - 0
tests/test_parser_hm.py

@@ -0,0 +1,24 @@
+# -*- coding: utf-8 -*-
+
+import pytest
+
+import dingguo.parser.hm
+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', 'hm')
+
+@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.hm.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)