comparison bnpparibas.py @ 11:f3d6d73a7184

Fixes
author Daniele Nicolodi <daniele@grinta.net>
date Mon, 11 Jan 2016 21:14:54 +0100
parents 636ea715af1e
children 4747393db602
comparison
equal deleted inserted replaced
10:636ea715af1e 11:f3d6d73a7184
1 import cgi 1 import cgi
2 import email
2 import imp 3 import imp
3 import itertools 4 import itertools
4 import json 5 import json
5 import os.path 6 import os.path
6 import requests 7 import requests
42 # transactions table footer 43 # transactions table footer
43 FOOTER = """{:14s} {:10s} {:54s} {{balance:8.2f}}""".format('', '', 'BALANCE') 44 FOOTER = """{:14s} {:10s} {:54s} {{balance:8.2f}}""".format('', '', 'BALANCE')
44 45
45 # transactions table horizontal separator 46 # transactions table horizontal separator
46 SEP = """-""" * len(HEADER) 47 SEP = """-""" * len(HEADER)
48
49
50 # GPG encrypted text is ascii and as such does not require encoding
51 # but its decrypted form is utf-8 and therefore the charset header
52 # must be set accordingly. define an appropriate charset object
53 email.charset.add_charset('utf8 7bit', header_enc=email.charset.SHORTEST,
54 body_enc=None, output_charset='utf-8')
47 55
48 56
49 def loadconf(filename): 57 def loadconf(filename):
50 module = imp.new_module('conf') 58 module = imp.new_module('conf')
51 module.__file__ = filename 59 module.__file__ = filename
337 curs.execute('''SELECT COUNT(*) FROM transactions WHERE id = ?''', (t.id, )) 345 curs.execute('''SELECT COUNT(*) FROM transactions WHERE id = ?''', (t.id, ))
338 if not curs.fetchone()[0]: 346 if not curs.fetchone()[0]:
339 # not seen before 347 # not seen before
340 unseen.append(t) 348 unseen.append(t)
341 349
342 lines = [] 350 if unseen:
343 lines.append(HEADER) 351 lines = []
344 lines.append(SEP) 352 lines.append(HEADER)
345 for t in unseen: 353 lines.append(SEP)
346 lines.append(str(t)) 354 for t in unseen:
347 lines.append(SEP) 355 lines.append(str(t))
348 lines.append(FOOTER.format(balance=balance)) 356 lines.append(SEP)
349 body = '\n'.join(lines) 357 lines.append(FOOTER.format(balance=balance))
350 358
351 message = MIMEText(encrypt(body, conf['MAILFROM'], conf['MAILTO'])) 359 text = '\n'.join(lines)
352 message['Subject'] = 'BNP Paribas Account update' 360 payload = encrypt(text, conf['MAILFROM'], conf['MAILTO'])
353 message['From'] = conf['MAILFROM'] 361
354 message['To'] = conf['MAILTO'] 362 message = MIMEText(payload, _charset='utf8 7bit')
355 message['Date'] = format_datetime(localtime()) 363 message['Subject'] = 'BNP Paribas Account update'
356 364 message['From'] = conf['MAILFROM']
357 sendmail(message) 365 message['To'] = conf['MAILTO']
366 message['Date'] = format_datetime(localtime())
367
368 sendmail(message)
358 369
359 curs.executemany('''INSERT INTO transactions (id) VALUES (?)''', ((x.id, ) for x in unseen)) 370 curs.executemany('''INSERT INTO transactions (id) VALUES (?)''', ((x.id, ) for x in unseen))
360 db.commit() 371 db.commit()
361 372
362 ## messages 373 ## messages
366 377
367 data = remote.messages() 378 data = remote.messages()
368 for m in data['messages']: 379 for m in data['messages']:
369 380
370 curs = db.cursor() 381 curs = db.cursor()
371 curs.execute('''SELECT COUNT(*) FROM messages WHERE id = ?), 0)''', (m['id'], )) 382 curs.execute('''SELECT COUNT(*) FROM messages WHERE id = ?''', (m['id'], ))
372 if curs.fetchone()[0]: 383 if curs.fetchone()[0]:
373 # already handled 384 # already handled
374 continue 385 continue
375 386
376 body = Message.fromjson(remote.message(m['id'])) 387 text = Message.fromjson(remote.message(m['id']))
377 388 payload = encrypt(str(text), conf['MAILFROM'], conf['MAILTO'])
378 message = MIMEText(encrypt(str(body), conf['MAILFROM'], conf['MAILTO'])) 389
390 message = MIMEText(payload, _charset='utf8 7bit')
379 message['Subject'] = 'BNP Paribas Message' 391 message['Subject'] = 'BNP Paribas Message'
380 message['From'] = conf['MAILFROM'] 392 message['From'] = conf['MAILFROM']
381 message['To'] = conf['MAILTO'] 393 message['To'] = conf['MAILTO']
382 message['Date'] = format_datetime(localtime()) 394 message['Date'] = format_datetime(localtime())
383 395