amazon.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # -*- coding: utf-8 -*-
  2. import datetime
  3. import dateutil
  4. import dateutil.tz
  5. import dingguo
  6. import email
  7. import ioex
  8. import re
  9. def parse_order_confirmation_mail(mail):
  10. assert isinstance(mail, email.message.Message)
  11. msg_text = mail.get_payload()[0].get_payload(decode = True).decode('utf-8')
  12. if not u'Amazon.de Bestellbestätigung' in msg_text:
  13. raise Exception('no amazon order confirmation')
  14. orders = []
  15. for order_text in re.split(ur'={32,}', msg_text)[1:-1]:
  16. order_id = re.search(r'Bestellnummer #(.+)', order_text).group(1)
  17. order_date_formatted = re.search(ur'Aufgegeben am (.+)', order_text, re.UNICODE).group(1)
  18. mail_date = dateutil.parser.parse(mail['date'])
  19. with ioex.setlocale('de_DE.UTF-8'):
  20. order_date = datetime.datetime.strptime(
  21. order_date_formatted.encode('utf-8'),
  22. '%d. %B %Y',
  23. )
  24. assert order_date.date() == mail_date.date()
  25. order = dingguo.Order(
  26. u'amazon.de',
  27. order_id,
  28. order_date.date(),
  29. )
  30. for articles_text in re.findall(
  31. ur'Bestellte\(r\) Artikel:\s+'
  32. + ur'([\W\w]+?)\s+'
  33. + ur'(Lieferung \d|_{10,})',
  34. order_text,
  35. re.UNICODE,
  36. ):
  37. for article_text in re.split(ur'\n\t*\n', articles_text[0]):
  38. article_match = re.match(
  39. ur' *((?P<quantity>\d+) x )?(?P<name>.*)\n'
  40. + ur'( *von (?P<authors>.*)\n)?'
  41. + ur' *(?P<price_brutto_currency>[A-Z]+) (?P<price_brutto>\d+,\d+)\n'
  42. + ur'( *Zustand: (?P<state>.*)\n)?'
  43. + ur' *Verkauft von: (?P<reseller>.*)'
  44. + ur'(\n *Versand durch (?P<shipper>.*))?',
  45. article_text,
  46. re.MULTILINE | re.UNICODE
  47. )
  48. assert article_match is not None, repr(article_text)
  49. article = article_match.groupdict()
  50. order.items.append(dingguo.Article(
  51. name = article['name'],
  52. price_brutto = dingguo.Sum(
  53. float(article['price_brutto'].replace(',', '.')),
  54. article['price_brutto_currency']
  55. ),
  56. quantity = int(article['quantity']) if article['quantity'] else 1,
  57. authors = article['authors'].split(',') if article['authors'] else None,
  58. state = article['state'],
  59. reseller = article['reseller'],
  60. shipper = article['shipper'],
  61. ))
  62. assert re.search(
  63. ur'Verpackung und Versand: \w+ (\d+,\d+)',
  64. order_text,
  65. ).group(1) == '0,00'
  66. shipping_match = re.finditer(
  67. ur'Zustellung:\s+(?P<t>[\W\w]+?)\s+'
  68. + ur'Versandart',
  69. order_text,
  70. re.UNICODE,
  71. )
  72. for shipping_attr in [m.groupdict() for m in shipping_match]:
  73. with ioex.setlocale('de_AT.UTF-8'):
  74. s = datetime.datetime.strptime(
  75. shipping_attr['t'].encode('utf8'),
  76. '%A, %d. %B',
  77. ).replace(
  78. year = order.order_date.year,
  79. tzinfo = dateutil.tz.gettz('Europe/Berlin'),
  80. )
  81. assert s.date() > order.order_date
  82. e = s + datetime.timedelta(days = 1)
  83. order.items.append(dingguo.Shipping(
  84. price_brutto = dingguo.Sum(0.0, u'EUR'),
  85. estimated_arrival_time = ioex.datetimeex.Period(start = s, end = e),
  86. ))
  87. orders.append(order)
  88. return orders