|
@@ -17,26 +17,47 @@ import subprocess
|
|
import HTMLParser
|
|
import HTMLParser
|
|
import argcomplete
|
|
import argcomplete
|
|
|
|
|
|
-# strptime
|
|
|
|
-locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8')
|
|
|
|
|
|
+class Order(object):
|
|
|
|
+
|
|
|
|
+ def __init__(self, platform, order_id, order_date, customer_id = None):
|
|
|
|
+ assert type(platform) is unicode
|
|
|
|
+ self.platform = platform
|
|
|
|
+ assert type(order_id) is unicode
|
|
|
|
+ self.order_id = order_id
|
|
|
|
+ assert type(order_date) is datetime.datetime
|
|
|
|
+ self.order_date = order_date
|
|
|
|
+ assert customer_id is None or type(customer_id) is unicode
|
|
|
|
+ self.customer_id = customer_id
|
|
|
|
+ self.articles = []
|
|
|
|
+
|
|
|
|
+ def dict_repr(self):
|
|
|
|
+ return {k: v for (k, v) in {
|
|
|
|
+ 'articles': self.articles,
|
|
|
|
+ 'customer_id': self.customer_id,
|
|
|
|
+ 'order_date': self.order_date.strftime('%Y-%m-%d'),
|
|
|
|
+ 'order_id': self.order_id,
|
|
|
|
+ 'platform': self.platform,
|
|
|
|
+ }.items() if v is not None}
|
|
|
|
+
|
|
|
|
+yaml.SafeDumper.add_representer(Order, lambda dumper, order: dumper.represent_dict(order.dict_repr()))
|
|
|
|
|
|
def parse_amazon(msg):
|
|
def parse_amazon(msg):
|
|
|
|
|
|
- order = {
|
|
|
|
- 'platform': 'amazon.de',
|
|
|
|
- }
|
|
|
|
|
|
+ msg_text = msg.get_payload()[0].get_payload(decode = True).decode('utf-8')
|
|
|
|
|
|
- msg_text = msg.get_payload()[0].get_payload(decode = True)
|
|
|
|
|
|
+ order_id = re.search(r'Bestellnummer #(.+)', msg_text).group(1)
|
|
|
|
|
|
- order['order_id'] = re.search(r'Bestellnummer #(.+)', msg_text).group(1)
|
|
|
|
|
|
+ order_date_formatted = re.search(ur'Aufgegeben am (.+)', msg_text, re.UNICODE).group(1)
|
|
|
|
+ locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8')
|
|
|
|
+ order_date = datetime.datetime.strptime(order_date_formatted.encode('utf-8'), '%d. %B %Y')
|
|
|
|
|
|
- order_date = datetime.datetime.strptime(
|
|
|
|
- re.search(r'Aufgegeben am (.+)', msg_text).group(1),
|
|
|
|
- '%d. %B %Y'
|
|
|
|
|
|
+ order = Order(
|
|
|
|
+ u'amazon.de',
|
|
|
|
+ order_id,
|
|
|
|
+ order_date
|
|
)
|
|
)
|
|
- order['order_date'] = order_date.strftime('%Y-%m-%d')
|
|
|
|
|
|
|
|
- order['articles'] = []
|
|
|
|
|
|
+ articles = []
|
|
articles_text = msg_text.split('Bestellte(r) Artikel:')[1].split('_' * 10)[0].strip()
|
|
articles_text = msg_text.split('Bestellte(r) Artikel:')[1].split('_' * 10)[0].strip()
|
|
for article_text in articles_text.split('\n\n'):
|
|
for article_text in articles_text.split('\n\n'):
|
|
article_match = re.match(
|
|
article_match = re.match(
|
|
@@ -58,7 +79,7 @@ def parse_amazon(msg):
|
|
else:
|
|
else:
|
|
del article['authors']
|
|
del article['authors']
|
|
article['price_brutto'] = float(article['price_brutto'].replace(',', '.'))
|
|
article['price_brutto'] = float(article['price_brutto'].replace(',', '.'))
|
|
- order['articles'].append(article)
|
|
|
|
|
|
+ order.articles.append(article)
|
|
|
|
|
|
return order
|
|
return order
|
|
|
|
|
|
@@ -79,13 +100,20 @@ def parse_oebb(msg):
|
|
msg_text,
|
|
msg_text,
|
|
re.MULTILINE | re.UNICODE
|
|
re.MULTILINE | re.UNICODE
|
|
)
|
|
)
|
|
- order = order_match.groupdict()
|
|
|
|
- order['platform'] = 'oebb.at'
|
|
|
|
|
|
+ order_match_groups = order_match.groupdict()
|
|
|
|
+
|
|
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
|
|
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
|
|
- order['order_date'] = datetime.datetime.strptime(
|
|
|
|
- order['order_date'],
|
|
|
|
|
|
+ order_date = datetime.datetime.strptime(
|
|
|
|
+ order_match_groups['order_date'],
|
|
'%b %d, %Y'
|
|
'%b %d, %Y'
|
|
- ).strftime('%Y-%m-%d')
|
|
|
|
|
|
+ )
|
|
|
|
+
|
|
|
|
+ order = Order(
|
|
|
|
+ u'oebb',
|
|
|
|
+ order_match_groups['order_id'],
|
|
|
|
+ order_date,
|
|
|
|
+ customer_id = order_match_groups['customer_id'],
|
|
|
|
+ )
|
|
|
|
|
|
article_match = re.search(
|
|
article_match = re.search(
|
|
ur'(?P<price_brutto_currency>.)(?P<price_brutto>\d+\.\d+)'
|
|
ur'(?P<price_brutto_currency>.)(?P<price_brutto>\d+\.\d+)'
|
|
@@ -102,7 +130,7 @@ def parse_oebb(msg):
|
|
article['price_brutto_currency'] = 'EUR'
|
|
article['price_brutto_currency'] = 'EUR'
|
|
else:
|
|
else:
|
|
raise Exception('currency %s is not supported' % article['price_brutto_currency'])
|
|
raise Exception('currency %s is not supported' % article['price_brutto_currency'])
|
|
- order['articles'] = [article]
|
|
|
|
|
|
+ order.articles.append(article)
|
|
|
|
|
|
return order
|
|
return order
|
|
|
|
|
|
@@ -125,8 +153,7 @@ def parse_mytaxi(msg):
|
|
pdf_uncompressed,
|
|
pdf_uncompressed,
|
|
re.MULTILINE | re.UNICODE
|
|
re.MULTILINE | re.UNICODE
|
|
)
|
|
)
|
|
- order = order_match.groupdict()
|
|
|
|
- order['platform'] = 'mytaxi'
|
|
|
|
|
|
+ order_id = order_match.groupdict()['order_id']
|
|
|
|
|
|
article_match = re.search(
|
|
article_match = re.search(
|
|
ur'\(Bruttobetrag\)'
|
|
ur'\(Bruttobetrag\)'
|
|
@@ -154,14 +181,18 @@ def parse_mytaxi(msg):
|
|
'%d.%m.%y %H:%M'
|
|
'%d.%m.%y %H:%M'
|
|
)
|
|
)
|
|
article['arrival_time'] = arrival_time.strftime('%Y-%m-%d %H:%M')
|
|
article['arrival_time'] = arrival_time.strftime('%Y-%m-%d %H:%M')
|
|
- order['order_date'] = arrival_time.strftime('%Y-%m-%d')
|
|
|
|
article['price_brutto'] = float(article['price_brutto'].replace(',', '.'))
|
|
article['price_brutto'] = float(article['price_brutto'].replace(',', '.'))
|
|
if article['price_brutto_currency'] in [u'€', u'\x80']:
|
|
if article['price_brutto_currency'] in [u'€', u'\x80']:
|
|
article['price_brutto_currency'] = 'EUR'
|
|
article['price_brutto_currency'] = 'EUR'
|
|
else:
|
|
else:
|
|
- raise exception('currency %s is not supported' % article['price_brutto_currency'])
|
|
|
|
- order['articles'] = [article]
|
|
|
|
|
|
+ raise Exception('currency %s is not supported' % article['price_brutto_currency'])
|
|
|
|
|
|
|
|
+ order = Order(
|
|
|
|
+ u'mytaxi',
|
|
|
|
+ order_id,
|
|
|
|
+ arrival_time,
|
|
|
|
+ )
|
|
|
|
+ order.articles.append(article)
|
|
return order
|
|
return order
|
|
|
|
|
|
def parse(msg):
|
|
def parse(msg):
|
|
@@ -184,7 +215,7 @@ def parse(msg):
|
|
tracebacks['mytaxi'] = traceback.format_exc()
|
|
tracebacks['mytaxi'] = traceback.format_exc()
|
|
|
|
|
|
for parser_name in tracebacks:
|
|
for parser_name in tracebacks:
|
|
- print('%s parser: \n%s' % (parser_name, tracebacks[parser_name]))
|
|
|
|
|
|
+ sys.stderr.write('%s parser: \n%s\n' % (parser_name, tracebacks[parser_name]))
|
|
|
|
|
|
raise Exception('failed to parse')
|
|
raise Exception('failed to parse')
|
|
|
|
|