indiegogo.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # -*- coding: utf-8 -*-
  2. import BeautifulSoup
  3. import datetime
  4. import dateutil.parser
  5. import dingguo
  6. import email.message
  7. import ioex
  8. import pytz
  9. import re
  10. def parse_order_confirmation_mail(mail):
  11. assert isinstance(mail, email.message.Message)
  12. html = mail.get_payload()[1].get_payload(decode = True).decode('utf-8')
  13. if not 'indiegogo' in html:
  14. raise Exception()
  15. email_recipient_address = mail['delivered-to'].decode('utf8')
  16. email_date = dateutil.parser.parse(mail['date'])
  17. order = dingguo.Order(
  18. platform = u'indiegogo',
  19. order_id = '%s-%sZ' % (
  20. email_recipient_address,
  21. email_date.astimezone(pytz.utc).replace(tzinfo = None).isoformat(),
  22. ),
  23. order_date = email_date,
  24. customer_id = email_recipient_address,
  25. )
  26. doc = BeautifulSoup.BeautifulSoup(html)
  27. campaign_name_tag = doc.find(
  28. text = re.compile(u'(vielen Dank für deinen Beitrag|thank you for contributing)')
  29. ).findNext('a')
  30. reward_label_tag = doc.find(text = re.compile(u'Perk:'))
  31. order.items.append(dingguo.Contribution(
  32. campaign = dingguo.Campaign(
  33. name = campaign_name_tag.text,
  34. founder = doc.find(
  35. text = re.compile(u'(Kontaktiere den Kampagnenstarter|Contact the campaign owner)')
  36. ).findNext().text,
  37. website_url = campaign_name_tag['href'],
  38. ),
  39. price_brutto = dingguo.Sum.parse_text(
  40. doc.find(text = re.compile(u'(Unterstützung|Contribution):')).findNext().text
  41. ),
  42. reward = reward_label_tag.findNext().text if reward_label_tag else None,
  43. ))
  44. shipping_costs_label_tag = doc.find(text = re.compile(u'(Versand|Shipping):'))
  45. if shipping_costs_label_tag:
  46. with ioex.setlocale('de_AT.utf8' if 'Versand' in shipping_costs_label_tag else 'en_US.utf8'):
  47. estimated_arrival_start = datetime.datetime.strptime(
  48. doc.find(
  49. text = re.compile('(Voraussichtliche Lieferung|Estimated Delivery):')
  50. ).findNext().text,
  51. '%B %Y'
  52. ).replace(tzinfo = email_date.tzinfo)
  53. order.items.append(dingguo.Shipping(
  54. price_brutto = dingguo.Sum.parse_text(
  55. shipping_costs_label_tag.findNext().text
  56. ),
  57. estimated_arrival_time = ioex.datetimeex.Period(
  58. start = estimated_arrival_start,
  59. end = (estimated_arrival_start + datetime.timedelta(days = 32)).replace(day = 1),
  60. ),
  61. destination_point = doc.find(text = re.compile('(Lieferadresse|Shipping Address):'))
  62. .findNext().text.replace('\r\n', '\n'),
  63. ))
  64. return [order]