# HG changeset patch # User Daniele Nicolodi # Date 1452543294 -3600 # Node ID f3d6d73a7184841eea93f4b35a3872de60435437 # Parent 636ea715af1ebdb617980b6354524f8e43d145a1 Fixes diff -r 636ea715af1e -r f3d6d73a7184 bnpparibas.py --- a/bnpparibas.py Mon Jan 11 19:45:30 2016 +0100 +++ b/bnpparibas.py Mon Jan 11 21:14:54 2016 +0100 @@ -1,4 +1,5 @@ import cgi +import email import imp import itertools import json @@ -46,6 +47,13 @@ SEP = """-""" * len(HEADER) +# GPG encrypted text is ascii and as such does not require encoding +# but its decrypted form is utf-8 and therefore the charset header +# must be set accordingly. define an appropriate charset object +email.charset.add_charset('utf8 7bit', header_enc=email.charset.SHORTEST, + body_enc=None, output_charset='utf-8') + + def loadconf(filename): module = imp.new_module('conf') module.__file__ = filename @@ -339,22 +347,25 @@ # not seen before unseen.append(t) - lines = [] - lines.append(HEADER) - lines.append(SEP) - for t in unseen: - lines.append(str(t)) - lines.append(SEP) - lines.append(FOOTER.format(balance=balance)) - body = '\n'.join(lines) + if unseen: + lines = [] + lines.append(HEADER) + lines.append(SEP) + for t in unseen: + lines.append(str(t)) + lines.append(SEP) + lines.append(FOOTER.format(balance=balance)) - message = MIMEText(encrypt(body, conf['MAILFROM'], conf['MAILTO'])) - message['Subject'] = 'BNP Paribas Account update' - message['From'] = conf['MAILFROM'] - message['To'] = conf['MAILTO'] - message['Date'] = format_datetime(localtime()) + text = '\n'.join(lines) + payload = encrypt(text, conf['MAILFROM'], conf['MAILTO']) - sendmail(message) + message = MIMEText(payload, _charset='utf8 7bit') + message['Subject'] = 'BNP Paribas Account update' + message['From'] = conf['MAILFROM'] + message['To'] = conf['MAILTO'] + message['Date'] = format_datetime(localtime()) + + sendmail(message) curs.executemany('''INSERT INTO transactions (id) VALUES (?)''', ((x.id, ) for x in unseen)) db.commit() @@ -368,14 +379,15 @@ for m in data['messages']: curs = db.cursor() - curs.execute('''SELECT COUNT(*) FROM messages WHERE id = ?), 0)''', (m['id'], )) + curs.execute('''SELECT COUNT(*) FROM messages WHERE id = ?''', (m['id'], )) if curs.fetchone()[0]: # already handled continue - body = Message.fromjson(remote.message(m['id'])) + text = Message.fromjson(remote.message(m['id'])) + payload = encrypt(str(text), conf['MAILFROM'], conf['MAILTO']) - message = MIMEText(encrypt(str(body), conf['MAILFROM'], conf['MAILTO'])) + message = MIMEText(payload, _charset='utf8 7bit') message['Subject'] = 'BNP Paribas Message' message['From'] = conf['MAILFROM'] message['To'] = conf['MAILTO']