1
0

indiegogo.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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:'))
  45. if shipping_costs_label_tag:
  46. with ioex.setlocale('de_AT.utf8'):
  47. estimated_arrival_start = datetime.datetime.strptime(
  48. doc.find(text = re.compile('Voraussichtliche Lieferung:')).findNext().text,
  49. '%B %Y'
  50. ).replace(tzinfo = email_date.tzinfo)
  51. order.items.append(dingguo.Shipping(
  52. price_brutto = dingguo.Sum.parse_text(
  53. shipping_costs_label_tag.findNext().text
  54. ),
  55. estimated_arrival_time = ioex.datetimeex.Period(
  56. start = estimated_arrival_start,
  57. end = (estimated_arrival_start + datetime.timedelta(days = 32)).replace(day = 1),
  58. ),
  59. destination_point = doc.find(text = re.compile('Lieferadresse:'))
  60. .findNext().text.replace('\r\n', '\n'),
  61. ))
  62. return [order]