12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- # -*- coding: utf-8 -*-
- import BeautifulSoup
- import datetime
- import dateutil.parser
- import dingguo
- import email.message
- import ioex
- import pytz
- import re
- def parse_order_confirmation_mail(mail):
- assert isinstance(mail, email.message.Message)
- html = mail.get_payload()[1].get_payload(decode = True).decode('utf-8')
- if not 'indiegogo' in html:
- raise Exception()
- email_recipient_address = mail['delivered-to'].decode('utf8')
- email_date = dateutil.parser.parse(mail['date'])
- order = dingguo.Order(
- platform = u'indiegogo',
- order_id = '%s-%sZ' % (
- email_recipient_address,
- email_date.astimezone(pytz.utc).replace(tzinfo = None).isoformat(),
- ),
- order_date = email_date,
- customer_id = email_recipient_address,
- )
- doc = BeautifulSoup.BeautifulSoup(html)
- campaign_name_tag = doc.find(
- text = re.compile(u'(vielen Dank für deinen Beitrag|thank you for contributing)')
- ).findNext('a')
- reward_label_tag = doc.find(text = re.compile(u'Perk:'))
- order.items.append(dingguo.Contribution(
- campaign = dingguo.Campaign(
- name = campaign_name_tag.text,
- founder = doc.find(
- text = re.compile(u'(Kontaktiere den Kampagnenstarter|Contact the campaign owner)')
- ).findNext().text,
- website_url = campaign_name_tag['href'],
- ),
- price_brutto = dingguo.Sum.parse_text(
- doc.find(text = re.compile(u'(Unterstützung|Contribution):')).findNext().text
- ),
- reward = reward_label_tag.findNext().text if reward_label_tag else None,
- ))
- shipping_costs_label_tag = doc.find(text = re.compile(u'Versand:'))
- if shipping_costs_label_tag:
- with ioex.setlocale('de_AT.utf8'):
- estimated_arrival_start = datetime.datetime.strptime(
- doc.find(text = re.compile('Voraussichtliche Lieferung:')).findNext().text,
- '%B %Y'
- ).replace(tzinfo = email_date.tzinfo)
- order.items.append(dingguo.Shipping(
- price_brutto = dingguo.Sum.parse_text(
- shipping_costs_label_tag.findNext().text
- ),
- estimated_arrival_time = ioex.datetimeex.Period(
- start = estimated_arrival_start,
- end = (estimated_arrival_start + datetime.timedelta(days = 32)).replace(day = 1),
- ),
- destination_point = doc.find(text = re.compile('Lieferadresse:'))
- .findNext().text.replace('\r\n', '\n'),
- ))
- return [order]
|