|
@@ -26,45 +26,63 @@ def parse_order_confirmation_mail(mail):
|
|
|
def get_text(tag):
|
|
|
return u''.join([c if type(c) is BeautifulSoup.NavigableString else get_text(c) for c in tag.contents])
|
|
|
|
|
|
- order = dingguo.Order(
|
|
|
- platform = u'amazon.cn',
|
|
|
- order_id = doc.find(text = re.compile(u'订单号')).findNext().text,
|
|
|
- order_date = dateutil.parser.parse(mail['date']),
|
|
|
- )
|
|
|
+ orders = []
|
|
|
|
|
|
- article_table = doc.find(text = re.compile(u'订购日期')).findNext('table')
|
|
|
- for a in article_table.find('tbody').findChildren(recursive = False):
|
|
|
- image_col_tag, details_tag, price_tag = a.findAll('td', recursive = False)
|
|
|
- name_tag = details_tag.find('a')
|
|
|
- order.items.append(dingguo.Article(
|
|
|
- name = name_tag.text.strip(),
|
|
|
- price_brutto = dingguo.Sum.parse_text(price_tag.text),
|
|
|
- quantity = 1,
|
|
|
- reseller = name_tag.findNext(text = re.compile(u'由')).findNext().text,
|
|
|
- ))
|
|
|
+ order_id_label_tags = doc.findAll(text = re.compile(u'订单号'))
|
|
|
+ assert len(order_id_label_tags) % 2 == 0
|
|
|
+ order_id_label_tags = order_id_label_tags[len(order_id_label_tags)/2:]
|
|
|
+ for order_id_label_tag in order_id_label_tags:
|
|
|
|
|
|
- with ioex.setlocale('zh_CN.utf-8'):
|
|
|
- estimated_arrival_date_start = pytz.timezone('Asia/Shanghai').localize(
|
|
|
- datetime.datetime.strptime(
|
|
|
- doc.find(text = re.compile(u'预计送达日期:')).findNext('b').text.encode('utf-8'),
|
|
|
- '%A, %m/%d',
|
|
|
- ).replace(year = order.order_date.year)
|
|
|
+ order = dingguo.Order(
|
|
|
+ platform = u'amazon.cn',
|
|
|
+ order_id = order_id_label_tag.findNext().text,
|
|
|
+ order_date = dateutil.parser.parse(mail['date']),
|
|
|
)
|
|
|
- assert estimated_arrival_date_start >= order.order_date
|
|
|
- order.items.append(dingguo.Shipping(
|
|
|
- name = doc.find(text = re.compile(u'送货方式:')).findNext('b').text,
|
|
|
- price_brutto = dingguo.Sum.parse_text(
|
|
|
- doc.find(text = re.compile(u'配送费:')).findNext().text
|
|
|
- ),
|
|
|
- estimated_arrival_time = ioex.datetimeex.Period(
|
|
|
- start = estimated_arrival_date_start,
|
|
|
- end = (estimated_arrival_date_start + datetime.timedelta(days = 1)),
|
|
|
- ),
|
|
|
- destination_point = '\n'.join([l.strip() for l in
|
|
|
- get_text(
|
|
|
- doc.find(text = re.compile(u'您的订单发送至:')).findNext('b')
|
|
|
- ).split('\n')
|
|
|
- ]),
|
|
|
- ))
|
|
|
|
|
|
- return [order]
|
|
|
+ order_detail_tag = (order_id_label_tag.findNext().findNext('table')
|
|
|
+ if len(order_id_label_tags) > 1
|
|
|
+ else order_id_label_tag.findParent('table').findPrevious('table'))
|
|
|
+ article_table = order_detail_tag.findNext('table')
|
|
|
+ if len(order_id_label_tags) == 1:
|
|
|
+ article_table = article_table.findNext('table')
|
|
|
+ for article_tag in article_table.find('tbody').findChildren(recursive = False):
|
|
|
+ image_col_tag, details_tag, price_tag = article_tag.findAll('td', recursive = False)
|
|
|
+ name_tag = details_tag.find('a')
|
|
|
+ order.items.append(dingguo.Article(
|
|
|
+ name = name_tag.text.strip(),
|
|
|
+ price_brutto = dingguo.Sum.parse_text(price_tag.text),
|
|
|
+ quantity = 1,
|
|
|
+ reseller = name_tag.findNext(text = re.compile(u'由')).findNext().text,
|
|
|
+ ))
|
|
|
+
|
|
|
+ del_date_str = order_detail_tag.find(text = re.compile(u'预计送达日期:')).findNext().text
|
|
|
+ def parse_date(s):
|
|
|
+ with ioex.setlocale('zh_CN.utf-8'):
|
|
|
+ return pytz.timezone('Asia/Shanghai').localize(
|
|
|
+ datetime.datetime.strptime(
|
|
|
+ s.encode('utf-8'),
|
|
|
+ '%A, %m/%d',
|
|
|
+ ).replace(year = order.order_date.year)
|
|
|
+ )
|
|
|
+ del_date_range = [parse_date(s.strip()) for s in del_date_str.split('-')]
|
|
|
+ if len(del_date_range) == 1:
|
|
|
+ del_date_range.append(del_date_range[0])
|
|
|
+ order.items.append(dingguo.Shipping(
|
|
|
+ name = order_detail_tag.find(text = re.compile(u'送货方式:')).findNext('b').text,
|
|
|
+ price_brutto = dingguo.Sum.parse_text(
|
|
|
+ order_detail_tag.findNext(text = re.compile(u'配送费:')).findNext().text
|
|
|
+ ),
|
|
|
+ estimated_arrival_time = ioex.datetimeex.Period(
|
|
|
+ start = del_date_range[0],
|
|
|
+ end = (del_date_range[1] + datetime.timedelta(days = 1)),
|
|
|
+ ),
|
|
|
+ destination_point = '\n'.join([l.strip() for l in
|
|
|
+ get_text(
|
|
|
+ order_detail_tag.find(text = re.compile(u'您的订单发送至:')).findNext('b')
|
|
|
+ ).split('\n')
|
|
|
+ ]),
|
|
|
+ ))
|
|
|
+
|
|
|
+ orders.append(order)
|
|
|
+
|
|
|
+ return orders
|