amazon_cn.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # -*- coding: utf-8 -*-
  2. import BeautifulSoup
  3. import datetime
  4. import dateutil.parser
  5. import dingguo
  6. import email.message
  7. import ioex
  8. import pytz
  9. import re
  10. def parse_order_confirmation_mail(mail):
  11. assert isinstance(mail, email.message.Message)
  12. html = mail.get_payload()[1].get_payload(decode = True).decode('utf-8')
  13. if not u'订单详情' in html:
  14. raise Exception()
  15. doc = BeautifulSoup.BeautifulSoup(html)
  16. for br in doc.findAll('br'):
  17. br.replaceWith('\n')
  18. def get_text(tag):
  19. return u''.join([c if type(c) is BeautifulSoup.NavigableString else get_text(c) for c in tag.contents])
  20. orders = []
  21. order_id_label_tags = doc.findAll(text = re.compile(u'订单号'))
  22. assert len(order_id_label_tags) % 2 == 0
  23. order_id_label_tags = order_id_label_tags[len(order_id_label_tags)/2:]
  24. for order_id_label_tag in order_id_label_tags:
  25. order = dingguo.Order(
  26. platform = u'amazon.cn',
  27. order_id = order_id_label_tag.findNext().text,
  28. order_date = dateutil.parser.parse(mail['date']),
  29. )
  30. order_detail_tag = (order_id_label_tag.findNext().findNext('table')
  31. if len(order_id_label_tags) > 1
  32. else order_id_label_tag.findParent('table').findPrevious('table'))
  33. article_table = order_detail_tag.findNext('table')
  34. if len(order_id_label_tags) == 1:
  35. article_table = article_table.findNext('table')
  36. for article_tag in article_table.find('tbody').findChildren(recursive = False):
  37. image_col_tag, details_tag, price_tag = article_tag.findAll('td', recursive = False)
  38. name_tag = details_tag.find('a')
  39. order.items.append(dingguo.Article(
  40. name = name_tag.text.strip(),
  41. price_brutto = dingguo.Sum.parse_text(price_tag.text),
  42. quantity = 1,
  43. reseller = name_tag.findNext(text = re.compile(u'由')).findNext().text,
  44. ))
  45. del_date_str = order_detail_tag.find(text = re.compile(u'预计送达日期:')).findNext().text
  46. def parse_date(s):
  47. with ioex.setlocale('zh_CN.utf-8'):
  48. return pytz.timezone('Asia/Shanghai').localize(
  49. datetime.datetime.strptime(
  50. s.encode('utf-8'),
  51. '%A, %m/%d',
  52. ).replace(year = order.order_date.year)
  53. )
  54. del_date_range = [parse_date(s.strip()) for s in del_date_str.split('-')]
  55. if len(del_date_range) == 1:
  56. del_date_range.append(del_date_range[0])
  57. order.items.append(dingguo.Shipping(
  58. name = order_detail_tag.find(text = re.compile(u'送货方式:')).findNext('b').text,
  59. price_brutto = dingguo.Sum.parse_text(
  60. order_detail_tag.findNext(text = re.compile(u'配送费:')).findNext().text
  61. ),
  62. estimated_arrival_time = ioex.datetimeex.Period(
  63. start = del_date_range[0],
  64. end = (del_date_range[1] + datetime.timedelta(days = 1)),
  65. ),
  66. destination_point = '\n'.join([l.strip() for l in
  67. get_text(
  68. order_detail_tag.find(text = re.compile(u'您的订单发送至:')).findNext('b')
  69. ).split('\n')
  70. ]),
  71. ))
  72. orders.append(order)
  73. return orders