order-confirmation-mail-parser 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # PYTHON_ARGCOMPLETE_OK
  4. import re
  5. import os
  6. import sys
  7. import yaml
  8. import email
  9. import pprint
  10. import random
  11. import locale
  12. import argparse
  13. import datetime
  14. import traceback
  15. import argcomplete
  16. # strptime
  17. locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8')
  18. def parse_amazon(msg):
  19. order = {
  20. 'platform': 'amazon.de',
  21. }
  22. msg_text = msg.get_payload(decode = True)
  23. order['order_id'] = re.search(r'Bestellnummer #(.+)', msg_text).group(1)
  24. order_date = datetime.datetime.strptime(
  25. re.search(r'Aufgegeben am (.+)', msg_text).group(1),
  26. '%d. %B %Y'
  27. )
  28. order['order_date'] = order_date.strftime('%Y-%m-%d')
  29. order['articles'] = []
  30. articles_text = msg_text.split('Bestellte(r) Artikel:')[1].split('_' * 10)[0].strip()
  31. for article_text in articles_text.split('\n\n'):
  32. article_match = re.match(
  33. ur' *(?P<name>.*)\n'
  34. + ur'( *von (?P<authors>.*)\n)?'
  35. + ur' *(?P<price_brutto_currency>[A-Z]+) (?P<price_brutto>\d+,\d+)\n'
  36. + ur'( *Zustand: (?P<state>.*)\n)?'
  37. + ur' *Verkauft von: (?P<reseller>.*)'
  38. + ur'(\n *Versand durch (?P<shipper>.*))?',
  39. article_text,
  40. re.MULTILINE | re.UNICODE
  41. )
  42. if article_match is None:
  43. sys.stderr.write(repr(article_text) + '\n')
  44. raise Exception('could not match article')
  45. article = article_match.groupdict()
  46. if article['authors']:
  47. article['authors'] = article['authors'].split(',')
  48. else:
  49. del article['authors']
  50. article['price_brutto'] = float(article['price_brutto'].replace(',', '.'))
  51. order['articles'].append(article)
  52. return order
  53. def parse_oebb(msg):
  54. msg_text = msg.get_payload(decode = True).decode('utf8')
  55. order_match = re.search(
  56. ur'Booking code: (?P<order_id>[\d ]+)\s+'
  57. + ur'Customer number: (?P<customer_id>PV\d+)\s+'
  58. + ur'Booking date: (?P<order_date>.* \d{4})\s',
  59. msg_text,
  60. re.MULTILINE | re.UNICODE
  61. )
  62. order = order_match.groupdict()
  63. order['platform'] = 'oebb.at'
  64. locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
  65. order['order_date'] = datetime.datetime.strptime(
  66. order['order_date'],
  67. '%b %d, %Y'
  68. ).strftime('%Y-%m-%d')
  69. article_match = re.search(
  70. ur'(?P<price_brutto_currency>.)(?P<price_brutto>\d+\.\d+)'
  71. + ur'[\W\w]+'
  72. + ur'Your Booking\s+'
  73. + ur'(?P<departure_point>.*) > (?P<destination_point>.*)',
  74. msg_text,
  75. re.MULTILINE | re.UNICODE
  76. )
  77. article = article_match.groupdict()
  78. article['name'] = 'Train Ticket'
  79. article['price_brutto'] = float(article['price_brutto'])
  80. if article['price_brutto_currency'] == u'€':
  81. article['price_brutto_currency'] = 'EUR'
  82. else:
  83. raise Exception('currency %s is not supported' % article['price_brutto_currency'])
  84. order['articles'] = [article]
  85. return order
  86. def parse(msg):
  87. tracebacks = {}
  88. try:
  89. return parse_amazon(msg)
  90. except:
  91. tracebacks['amazon'] = traceback.format_exc()
  92. try:
  93. return parse_oebb(msg)
  94. except:
  95. tracebacks['oebb'] = traceback.format_exc()
  96. for parser_name in tracebacks:
  97. print('%s parser: \n%s' % (parser_name, tracebacks[parser_name]))
  98. print('failed')
  99. # raise Exception('failed to parse')
  100. def compute():
  101. msg = email.message_from_string(sys.stdin.read())
  102. orders = []
  103. if msg.is_multipart():
  104. for part in msg.get_payload():
  105. orders.append(parse(part))
  106. else:
  107. orders.append(parse(msg))
  108. print(yaml.safe_dump(orders, default_flow_style = False))
  109. def _init_argparser():
  110. argparser = argparse.ArgumentParser(description = None)
  111. return argparser
  112. def main(argv):
  113. argparser = _init_argparser()
  114. argcomplete.autocomplete(argparser)
  115. args = argparser.parse_args(argv)
  116. compute(**vars(args))
  117. return 0
  118. if __name__ == "__main__":
  119. sys.exit(main(sys.argv[1:]))