oebb.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # -*- coding: utf-8 -*-
  2. import datetime
  3. import dingguo
  4. import email
  5. import ioex
  6. import re
  7. def parse_order_confirmation_mail(mail):
  8. assert isinstance(mail, email.message.Message)
  9. msg = mail.get_payload()[0]
  10. if type(msg.get_payload()) is list:
  11. msg = msg.get_payload()[0]
  12. msg_text = msg.get_payload(decode = True).decode('utf8')
  13. # msg_text = re.sub(
  14. # r'<[^>]+>',
  15. # '',
  16. # HTMLParser.HTMLParser().unescape(msg.get_payload(decode = True).decode('utf8'))
  17. # )
  18. order_match = re.search(
  19. ur'Booking code:\s+(?P<order_id>[\d ]+)\s+'
  20. + ur'Customer number:\s+(?P<customer_id>PV\d+)\s+'
  21. + ur'Booking date:\s+(?P<order_date>.* \d{4})\s',
  22. msg_text,
  23. re.MULTILINE | re.UNICODE
  24. )
  25. order_match_groups = order_match.groupdict()
  26. with ioex.setlocale('en_US.UTF-8'):
  27. order_date = datetime.datetime.strptime(
  28. order_match_groups['order_date'],
  29. '%b %d, %Y',
  30. )
  31. order = dingguo.Order(
  32. u'oebb',
  33. order_match_groups['order_id'],
  34. order_date,
  35. customer_id = order_match_groups['customer_id'],
  36. )
  37. item_match = re.search(
  38. ur'(?P<price_brutto_currency>.)(?P<price_brutto>\d+\.\d+)'
  39. + ur'[\W\w]+'
  40. + ur'Your Booking\s+'
  41. + ur'(?P<departure_point>.*)\s+>\s+(?P<destination_point>.*)',
  42. msg_text,
  43. re.MULTILINE | re.UNICODE
  44. )
  45. item = item_match.groupdict()
  46. order.items.append(dingguo.Transportation(
  47. name = u'Train Ticket',
  48. price_brutto = dingguo.Sum(
  49. float(item['price_brutto']),
  50. item['price_brutto_currency'],
  51. ),
  52. departure_point = item['departure_point'],
  53. destination_point = item['destination_point'],
  54. ))
  55. return [order]