1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- import datetime
- import dingguo
- import email
- import email.utils
- import ioex
- import re
- class UtcOffsetTimezone(datetime.tzinfo):
- def __init__(self, seconds):
- assert type(seconds) is int
- self.seconds = seconds
- def utcoffset(self, dt):
- return datetime.timedelta(seconds = self.seconds)
- def dst(self, dt):
- return None
- def tzname(self, dt):
- return self.__class__.__name__ + '(seconds = %d)' % self.seconds
- def parse_order_confirmation_mail(mail):
- assert isinstance(mail, email.message.Message)
- msg_text = mail.get_payload(decode = True).decode('utf8')
- if not 'Wiener Linien' in msg_text:
- raise Exception()
- order_match = re.search(
- r'(?P<name>Single-Ticket)\s+'
- + r'Valid from: (?P<valid_from>\d{1,2} \w+ \d{4} \d{1,2}:\d{2})\s+'
- + r'Type: (?P<type>Mobile ticket)\s+'
- + r'For\:\s+'
- + r'(?P<passenger_first_name>Fabian Peter)\s+'
- + r'(?P<passenger_last_name>Hammerle)\s+'
- + ur'Price: (?P<currency>€)(?P<price>2.80)\s+'
- + r'Quantity: (?P<quantity>1)\s+'
- ,
- msg_text,
- re.MULTILINE | re.UNICODE
- )
- if not order_match:
- print(repr(msg_text))
- order_match_groups = order_match.groupdict()
- email_recipient_address = mail['delivered-to'].decode('utf8')
- email_date = datetime.datetime.utcfromtimestamp(
-
- email.utils.mktime_tz(email.utils.parsedate_tz(mail['date']))
- )
- order = dingguo.Order(
- platform = u'wiener_linien',
- order_id = '%s-%s' % (email_recipient_address, email_date.isoformat()),
- order_date = email_date,
- customer_id = email_recipient_address,
- )
- assert int(order_match_groups['quantity']) == 1
- with ioex.setlocale('en_US.UTF-8'):
- import pytz
- order.items.append(dingguo.Transportation(
- name = order_match_groups['name'],
- price_brutto = dingguo.Sum(
- float(order_match_groups['price']),
- order_match_groups['currency'],
- ),
- passenger = dingguo.Person(
- first_name = order_match_groups['passenger_first_name'],
- last_name = order_match_groups['passenger_last_name'],
- ),
- valid_from = datetime.datetime.strptime(order_match_groups['valid_from'], '%d %B %Y %H:%M')
- .replace(tzinfo = UtcOffsetTimezone(email.utils.parsedate_tz(mail['date'])[9]))
- ))
- return [order]
|