banggood.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. for price_tag in doc.find(text = re.compile(r'Subtotal of Items')).findParent('td').findAll('strong'):
  27. article_match = re.search(
  28. ur'^(?P<quantity>\d+) x (?P<name>.*)'
  29. + ur' \((\d+x)?(?P<id>[^\)]+)\)',
  30. price_tag.previousSibling.replace('&nbsp;', ' ').strip(),
  31. re.UNICODE,
  32. )
  33. potential_option = price_tag.nextSibling.findNext(text = re.compile(ur'\w+')).replace('&nbsp;', ' ').strip()
  34. if (not 'IMPORTANT' in potential_option
  35. and not re.search(ur'^\d+ x', potential_option)):
  36. option = potential_option
  37. else:
  38. option = None
  39. attr = article_match.groupdict()
  40. order.items.append(dingguo.Article(
  41. name = attr['name'],
  42. option = option,
  43. price_brutto = dingguo.Sum.parse_text(price_tag.text) / float(attr['quantity']),
  44. product_id = attr['id'],
  45. quantity = int(attr['quantity']),
  46. ))
  47. shipping_line_tag = doc.find(text = re.compile('Sub-Total')).findNext('tr')
  48. shipping_option_tag, shipping_price_tag = shipping_line_tag.findAll('td')
  49. order.items.append(dingguo.Shipping(
  50. name = shipping_option_tag.text,
  51. price_brutto = dingguo.Sum.parse_text(shipping_price_tag.text),
  52. ))
  53. insurance_label_tag = shipping_line_tag.findNext(text = re.compile('Insurance'))
  54. if insurance_label_tag:
  55. order.items.append(dingguo.Service(
  56. name = insurance_label_tag.parent.text,
  57. price_brutto = dingguo.Sum.parse_text(insurance_label_tag.findNext().text),
  58. ))
  59. points_discount_label_tag = doc.find(text = re.compile('Discount Points'))
  60. if points_discount_label_tag:
  61. order.discounts.append(dingguo.Discount(
  62. name = u'redeem %d banggood points' % int(points_discount_label_tag.split(':')[1]),
  63. amount = dingguo.Sum.parse_text(points_discount_label_tag.findNext().text) * -1,
  64. ))
  65. return [order]