123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- # -*- 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|Shipping):'))
- if shipping_costs_label_tag:
- with ioex.setlocale('de_AT.utf8' if 'Versand' in shipping_costs_label_tag else 'en_US.utf8'):
- estimated_arrival_start = datetime.datetime.strptime(
- doc.find(
- text = re.compile('(Voraussichtliche Lieferung|Estimated Delivery):')
- ).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|Shipping Address):'))
- .findNext().text.replace('\r\n', '\n'),
- ))
- return [order]
|