banggood.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # -*- coding: utf-8 -*-
  2. import BeautifulSoup
  3. import datetime
  4. import dingguo
  5. import email.message
  6. import ioex
  7. import re
  8. def parse_order_confirmation_mail(mail):
  9. assert isinstance(mail, email.message.Message)
  10. html = mail.get_payload()[1].get_payload(decode = True).decode('utf-8')
  11. doc = BeautifulSoup.BeautifulSoup(html)
  12. """ Your Order#13998883(placed on Saturday 16 April, 2016)"""
  13. order_attr = re.search(
  14. ur'Your Order#(?P<id>\d+)\(placed on (?P<date>.*)\)',
  15. doc.find(text = re.compile(r'placed on')).parent.text,
  16. re.UNICODE,
  17. ).groupdict()
  18. with ioex.setlocale('en_US.UTF-8'):
  19. order = dingguo.Order(
  20. platform = u'banggood',
  21. order_id = order_attr['id'],
  22. order_date = datetime.datetime.strptime(order_attr['date'], '%A %d %B, %Y').date(),
  23. customer_id = None,
  24. )
  25. """ 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 """
  26. articles_text = doc.find(text = re.compile(r'Subtotal of Items')) \
  27. .parent.parent.text.replace('&nbsp;', ' ') \
  28. .split('Subtotal of Items:')[1].split('IMPORTANT NOTICE')[0]
  29. for article_match in re.finditer(
  30. ur'(?P<quantity>\d+) x (?P<name>[^\(]*) \((\d+x)?(?P<id>[^\)]+)\)'
  31. + ur'(?P<currency>[^\d]+)(?P<price>\d+.\d\d)(?P<option>((?!\d+ x).)+)?',
  32. articles_text,
  33. re.UNICODE,
  34. ):
  35. attr = article_match.groupdict()
  36. order.items.append(dingguo.Article(
  37. name = attr['name'],
  38. option = attr['option'],
  39. price_brutto = dingguo.Sum(
  40. float(attr['price']) / float(attr['quantity']),
  41. attr['currency'],
  42. ),
  43. product_id = attr['id'],
  44. quantity = int(attr['quantity']),
  45. ))
  46. shipping_line_tag = doc.find(text = re.compile('Sub-Total')).findNext('tr')
  47. shipping_option_tag, shipping_price_tag = shipping_line_tag.findAll('td')
  48. order.items.append(dingguo.Shipping(
  49. name = shipping_option_tag.text,
  50. price_brutto = dingguo.Sum.parse_text(shipping_price_tag.text),
  51. ))
  52. insurance_label_tag = shipping_line_tag.findNext(text = re.compile('Insurance'))
  53. if insurance_label_tag:
  54. order.items.append(dingguo.Service(
  55. name = insurance_label_tag.parent.text,
  56. price_brutto = dingguo.Sum.parse_text(insurance_label_tag.findNext().text),
  57. ))
  58. points_discount_label_tag = doc.find(text = re.compile('Discount Points'))
  59. if points_discount_label_tag:
  60. order.discounts.append(dingguo.Discount(
  61. name = u'redeem %d banggood points' % int(points_discount_label_tag.split(':')[1]),
  62. amount = dingguo.Sum.parse_text(points_discount_label_tag.findNext().text) * -1,
  63. ))
  64. return [order]