12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- # -*- coding: utf-8 -*-
- import BeautifulSoup
- import datetime
- import dateutil.parser
- import dingguo
- import email.message
- import ioex
- import pytz
- 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')
- if not u'订单详情' in html:
- raise Exception()
- doc = BeautifulSoup.BeautifulSoup(html)
- for br in doc.findAll('br'):
- br.replaceWith('\n')
- def get_text(tag):
- return u''.join([c if type(c) is BeautifulSoup.NavigableString else get_text(c) for c in tag.contents])
- orders = []
- 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:
- order = dingguo.Order(
- platform = u'amazon.cn',
- order_id = order_id_label_tag.findNext().text,
- order_date = dateutil.parser.parse(mail['date']),
- )
- 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
|