wienerlinien.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # -*- coding: utf-8 -*-
  2. import datetime
  3. import dingguo
  4. import email
  5. import email.utils
  6. import ioex
  7. import re
  8. class UtcOffsetTimezone(datetime.tzinfo):
  9. def __init__(self, seconds):
  10. assert type(seconds) is int
  11. self.seconds = seconds
  12. def utcoffset(self, dt):
  13. return datetime.timedelta(seconds = self.seconds)
  14. def dst(self, dt):
  15. return None
  16. def tzname(self, dt):
  17. return self.__class__.__name__ + '(seconds = %d)' % self.seconds
  18. def parse_order_confirmation_mail(mail):
  19. assert isinstance(mail, email.message.Message)
  20. msg_text = mail.get_payload(decode = True).decode('utf8')
  21. if not 'Wiener Linien' in msg_text:
  22. raise Exception()
  23. order_match = re.search(
  24. r'(?P<name>.*Ticket.*)\s+'
  25. + r'Valid from: (?P<valid_from>\d{1,2} \w+ \d{4} \d{1,2}:\d{2})\s+'
  26. + r'(Valid until: (?P<valid_until>\d{1,2} \w+ \d{4} \d{1,2}:\d{2})\s+)?'
  27. + r'Type: (?P<type>.* ticket)\s+'
  28. + r'For\:\s+'
  29. + r'(?P<passenger_first_name>Fabian Peter)\s+'
  30. + r'(?P<passenger_last_name>Hammerle)\s+'
  31. + ur'Price: (?P<currency>€)(?P<price>\d+.\d{2})\s+'
  32. + r'Quantity: (?P<quantity>1)\s+'
  33. ,
  34. msg_text,
  35. re.MULTILINE | re.UNICODE
  36. )
  37. if not order_match:
  38. print(repr(msg_text))
  39. order_match_groups = order_match.groupdict()
  40. email_recipient_address = mail['delivered-to'].decode('utf8')
  41. email_date = datetime.datetime.utcfromtimestamp(
  42. # seconds since the Epoch in UTC
  43. email.utils.mktime_tz(email.utils.parsedate_tz(mail['date']))
  44. )
  45. order = dingguo.Order(
  46. platform = u'wiener_linien',
  47. order_id = '%s-%s' % (email_recipient_address, email_date.isoformat()),
  48. order_date = email_date,
  49. customer_id = email_recipient_address,
  50. )
  51. assert int(order_match_groups['quantity']) == 1
  52. ticket_url_match = re.search(
  53. ur'following link:\s+(http[^\s]+)',
  54. msg_text,
  55. re.MULTILINE | re.UNICODE,
  56. )
  57. with ioex.setlocale('en_US.UTF-8'):
  58. timezone = UtcOffsetTimezone(email.utils.parsedate_tz(mail['date'])[9])
  59. order.items.append(dingguo.Transportation(
  60. name = order_match_groups['name'],
  61. price_brutto = dingguo.Sum(
  62. float(order_match_groups['price']),
  63. order_match_groups['currency'],
  64. ),
  65. passenger = dingguo.Person(
  66. first_name = order_match_groups['passenger_first_name'],
  67. last_name = order_match_groups['passenger_last_name'],
  68. ),
  69. valid_from = datetime.datetime.strptime(order_match_groups['valid_from'], '%d %B %Y %H:%M')
  70. .replace(tzinfo = timezone),
  71. valid_until = datetime.datetime.strptime(order_match_groups['valid_until'], '%d %B %Y %H:%M')
  72. .replace(tzinfo = timezone) if order_match_groups['valid_until'] else None,
  73. ticket_url = ticket_url_match.group(1) if ticket_url_match else None,
  74. ))
  75. return [order]