thomann.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # -*- coding: utf-8 -*-
  2. import BeautifulSoup
  3. import datetime
  4. import dingguo
  5. import email
  6. import re
  7. def parse_order_confirmation_mail(mail):
  8. assert isinstance(mail, email.message.Message)
  9. doc = BeautifulSoup.BeautifulSoup(
  10. mail.get_payload()[1].get_payload(decode = True).decode('utf-8')
  11. )
  12. if doc.find(text = re.compile(ur'Thomann')) is None:
  13. raise Exception('no thomann order confirmation')
  14. order = dingguo.Order(
  15. customer_id = doc.find(text = re.compile(ur'Kundennummer')).parent.nextSibling.text,
  16. order_id = doc.find(text = re.compile(ur'Auftragsnummer')).parent.nextSibling.text,
  17. order_date = datetime.datetime.strptime(
  18. doc.find(text = re.compile(ur'Auftragsdatum')).parent.nextSibling.text,
  19. '%d.%m.%Y %H:%M',
  20. ).date(),
  21. platform = u'thomann',
  22. )
  23. for product_id_label_tag in doc.findAll(text = re.compile(ur'Artikelnummer')):
  24. article_tag = product_id_label_tag.findParents('td')[1]
  25. name_quantity_tag = article_tag.find('strong')
  26. quantity, name = re.match(ur'(\d)x (.*)', name_quantity_tag.text).groups()
  27. currency, price = re.match(
  28. ur'(.) (\d+,\d+)',
  29. product_id_label_tag.parent.nextSibling.text,
  30. ).groups()
  31. order.items.append(dingguo.Article(
  32. features = name_quantity_tag.parent.nextSibling.text,
  33. name = name,
  34. price_brutto = dingguo.Sum(float(price.replace(',', '.')), currency),
  35. product_id = product_id_label_tag.nextSibling.text,
  36. quantity = int(quantity),
  37. ))
  38. return [order]