# -*- coding: utf-8 -*- import BeautifulSoup import datetime import dingguo import email.message import ioex import re def parse_order_confirmation_mail(mail): assert isinstance(mail, email.message.Message) html = mail.get_payload()[1].get_payload(decode = True).decode('utf-8') doc = BeautifulSoup.BeautifulSoup(html) """ Your Order#13998883(placed on Saturday 16 April, 2016)""" order_attr = re.search( ur'Your Order#(?P\d+)\(placed on (?P.*)\)', doc.find(text = re.compile(r'placed on')).parent.text, re.UNICODE, ).groupdict() with ioex.setlocale('en_US.UTF-8'): order = dingguo.Order( platform = u'banggood', order_id = order_attr['id'], order_date = datetime.datetime.strptime(order_attr['date'], '%A %d %B, %Y').date(), customer_id = None, ) """ 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 """ for price_tag in doc.find(text = re.compile(r'Subtotal of Items')).findParent('td').findAll('strong'): article_match = re.search( ur'^(?P\d+) x (?P.*)' + ur' \((\d+x)?(?P[^\)]+)\)', price_tag.previousSibling.replace(' ', ' ').strip(), re.UNICODE, ) potential_option = price_tag.nextSibling.findNext(text = re.compile(ur'\w+')).replace(' ', ' ').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 = option, price_brutto = dingguo.Sum.parse_text(price_tag.text) / float(attr['quantity']), product_id = attr['id'], quantity = int(attr['quantity']), )) shipping_line_tag = doc.find(text = re.compile('Sub-Total')).findNext('tr') shipping_option_tag, shipping_price_tag = shipping_line_tag.findAll('td') order.items.append(dingguo.Shipping( name = shipping_option_tag.text, price_brutto = dingguo.Sum.parse_text(shipping_price_tag.text), )) insurance_label_tag = shipping_line_tag.findNext(text = re.compile('Insurance')) if insurance_label_tag: order.items.append(dingguo.Service( name = insurance_label_tag.parent.text, price_brutto = dingguo.Sum.parse_text(insurance_label_tag.findNext().text), )) points_discount_label_tag = doc.find(text = re.compile('Discount Points')) if points_discount_label_tag: order.discounts.append(dingguo.Discount( name = u'redeem %d banggood points' % int(points_discount_label_tag.split(':')[1]), amount = dingguo.Sum.parse_text(points_discount_label_tag.findNext().text) * -1, )) return [order]