Mercurial > hg > bnpparibas
annotate bnpparibas.py @ 11:f3d6d73a7184
Fixes
author | Daniele Nicolodi <daniele@grinta.net> |
---|---|
date | Mon, 11 Jan 2016 21:14:54 +0100 |
parents | 636ea715af1e |
children | 4747393db602 |
rev | line source |
---|---|
7
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
1 import cgi |
11 | 2 import email |
3
1311f6533978
Load configuration from file specified on the command line
Daniele Nicolodi <daniele@grinta.net>
parents:
2
diff
changeset
|
3 import imp |
7
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
4 import itertools |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
5 import json |
0 | 6 import os.path |
7
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
7 import requests |
0 | 8 import smtplib |
9 import sqlite3 | |
9 | 10 import subprocess |
11 import sys | |
0 | 12 import textwrap |
7
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
13 import time |
0 | 14 |
15 from contextlib import contextmanager | |
16 from email.mime.text import MIMEText | |
17 from email.utils import format_datetime, localtime, parseaddr | |
18 from io import BytesIO | |
7
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
19 from pprint import pprint |
0 | 20 from urllib.parse import urljoin |
21 | |
7
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
22 from html2text import HTML2Text |
0 | 23 from PIL import Image |
24 | |
25 | |
7
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
26 URL = 'https://mabanque.bnpparibas/' |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
27 |
0 | 28 # message template |
29 MESSAGE = """\ | |
7
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
30 From: {message.sender:} |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
31 Subject: {message.subject:} |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
32 Date: {message.date:} |
0 | 33 |
7
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
34 {message.body:} |
0 | 35 """ |
36 | |
7
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
37 # transactions table row template |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
38 TRANSACTION = """{xact.id:14d} {xact.date:10s} {xact.description:54s} {xact.amount:+8.2f}""" |
0 | 39 |
7
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
40 # transactions table header |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
41 HEADER = """{:14s} {:10s} {:54s} {:>8s}""".format('Id', 'Date', 'Description', 'Amount') |
0 | 42 |
7
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
43 # transactions table footer |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
44 FOOTER = """{:14s} {:10s} {:54s} {{balance:8.2f}}""".format('', '', 'BALANCE') |
0 | 45 |
7
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
46 # transactions table horizontal separator |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
47 SEP = """-""" * len(HEADER) |
0 | 48 |
49 | |
11 | 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') | |
55 | |
56 | |
7
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
57 def loadconf(filename): |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
58 module = imp.new_module('conf') |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
59 module.__file__ = filename |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
60 with open(filename) as fd: |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
61 exec(compile(fd.read(), filename, 'exec'), module.__dict__) |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
62 conf = {} |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
63 for key in dir(module): |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
64 if key.isupper(): |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
65 conf[key] = getattr(module, key) |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
66 return conf |
0 | 67 |
68 | |
7
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
69 def wrap(p, indent): |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
70 return textwrap.fill(p, 72, initial_indent=indent, subsequent_indent=indent) |
0 | 71 |
72 | |
7
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
73 def html2text(html): |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
74 # the html2text module does an ok job, but it can be improved in |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
75 # the quality of the transformation and in the coding style |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
76 conv = HTML2Text() |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
77 conv.ignore_links = True |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
78 conv.ignore_images = True |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
79 conv.ignore_emphasis = True |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
80 return conv.handle(html) |
0 | 81 |
82 | |
83 class Mailer: | |
7
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
84 def __init__(self, host='localhost', port=25, starttls=True, username=None, password=None): |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
85 self.host = host |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
86 self.port = port |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
87 self.starttls = starttls |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
88 self.username = username |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
89 self.password = password |
0 | 90 |
91 @contextmanager | |
92 def connect(self): | |
7
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
93 smtp = smtplib.SMTP(self.host, self.port) |
0 | 94 if self.starttls: |
95 smtp.starttls() | |
96 if self.username: | |
97 smtp.login(self.username, self.password) | |
98 yield smtp | |
99 smtp.quit() | |
100 | |
101 def send(self, message, fromaddr=None, toaddr=None): | |
102 if not fromaddr: | |
103 fromaddr = message['From'] | |
104 if not toaddr: | |
105 toaddr = message['To'] | |
106 with self.connect() as conn: | |
107 conn.sendmail(fromaddr, toaddr, str(message)) | |
108 | |
109 | |
3
1311f6533978
Load configuration from file specified on the command line
Daniele Nicolodi <daniele@grinta.net>
parents:
2
diff
changeset
|
110 class GPG: |
1311f6533978
Load configuration from file specified on the command line
Daniele Nicolodi <daniele@grinta.net>
parents:
2
diff
changeset
|
111 def __init__(self, homedir): |
1311f6533978
Load configuration from file specified on the command line
Daniele Nicolodi <daniele@grinta.net>
parents:
2
diff
changeset
|
112 self.homedir = homedir |
7
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
113 |
3
1311f6533978
Load configuration from file specified on the command line
Daniele Nicolodi <daniele@grinta.net>
parents:
2
diff
changeset
|
114 def encrypt(self, message, sender, recipient): |
1311f6533978
Load configuration from file specified on the command line
Daniele Nicolodi <daniele@grinta.net>
parents:
2
diff
changeset
|
115 sender = parseaddr(sender)[1] |
1311f6533978
Load configuration from file specified on the command line
Daniele Nicolodi <daniele@grinta.net>
parents:
2
diff
changeset
|
116 recipient = parseaddr(recipient)[1] |
10 | 117 cmd = [ "gpg2", "--homedir", self.homedir, "--sign", "--encrypt", |
7
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
118 "--batch", "--no-options", "--yes", "--armor", |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
119 "--local-user", sender, "--recipient", recipient, ] |
3
1311f6533978
Load configuration from file specified on the command line
Daniele Nicolodi <daniele@grinta.net>
parents:
2
diff
changeset
|
120 p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
5 | 121 encdata, err = p.communicate(input=message.encode('utf-8')) |
4
b4c2db70bbf2
Detect and report gpg invocations errors
Daniele Nicolodi <daniele@grinta.net>
parents:
3
diff
changeset
|
122 if p.returncode: |
b4c2db70bbf2
Detect and report gpg invocations errors
Daniele Nicolodi <daniele@grinta.net>
parents:
3
diff
changeset
|
123 raise RuntimeError(p.returncode, err) |
5 | 124 return encdata.decode('ascii') |
0 | 125 |
126 | |
7
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
127 class Transaction: |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
128 __slots__ = 'id', 'date', 'category', 'description', 'amount', 'currency' |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
129 |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
130 def __init__(self, id, date, category, description, amount, currency): |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
131 self.id = id |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
132 self.date = date |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
133 self.category = category |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
134 self.description = description |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
135 self.amount = amount |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
136 self.currency = currency |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
137 |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
138 @classmethod |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
139 def fromjson(cls, x): |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
140 data = {'id': int(x['idOperation']), |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
141 'date': x['dateOperation'], |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
142 'category': int(x['idCategorie']), |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
143 'description': x['libelleOperation'].strip().replace('VIREMENT', 'VIR'), |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
144 'amount': x['montant']['montant'], |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
145 'currency': x['montant']['currency'], } |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
146 return cls(**data) |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
147 |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
148 def __str__(self): |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
149 return TRANSACTION.format(xact=self) |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
150 |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
151 |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
152 class Message: |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
153 __slots__ = 'id', 'date', 'subject', 'sender', 'content', 'quoted' |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
154 |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
155 def __init__(self, id, date, subject, sender, content, quoted): |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
156 self.id = id |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
157 self.date = date |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
158 self.subject = subject |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
159 self.sender = sender |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
160 self.content = content |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
161 self.quoted = quoted |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
162 |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
163 @classmethod |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
164 def fromjson(cls, x): |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
165 data = {'id': x['msg']['id'], |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
166 'date': x['msg']['id'], |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
167 'subject': x['msg']['objet'], |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
168 'sender': x['msg']['emetteur']['nom'], |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
169 'content': x['msg']['contenu'], |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
170 'quoted': None, } |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
171 quoted = x.get('msgAttache') |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
172 if quoted: |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
173 data['quoted'] = quoted['contenu'] |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
174 return cls(**data) |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
175 |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
176 @staticmethod |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
177 def normalize(txt, indent=''): |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
178 if '<div' in txt: |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
179 txt = txt.replace('<br></br>','<br/><br/>') |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
180 return html2text(txt) |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
181 |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
182 txt = txt.replace(r'<br/>', '\n') |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
183 parts = [] |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
184 for p in txt.split('\n'): |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
185 p = p.strip() |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
186 if p: |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
187 p = wrap(p, indent) |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
188 else: |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
189 p = indent |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
190 parts.append(p) |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
191 return '\n'.join(parts) |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
192 |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
193 @property |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
194 def body(self): |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
195 body = self.normalize(self.content) |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
196 if self.quoted is not None: |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
197 body = body + '\n\n' + self.normalize(self.quoted, '> ') |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
198 return body |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
199 |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
200 def __str__(self): |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
201 return MESSAGE.format(message=self) |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
202 |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
203 |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
204 class Keypad: |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
205 def __init__(self, data): |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
206 # reference keypad |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
207 fname = os.path.join(os.path.dirname(__file__), 'keypad.jpeg') |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
208 reference = Image.open(fname).convert('L') |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
209 symbols = [ 8, 4, 1, 6, 3, 7, 9, 0, 5, 2 ] |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
210 self.keypad = dict(zip(symbols, self.imslice(reference))) |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
211 |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
212 # decode keypad |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
213 image = Image.open(BytesIO(data)).convert('L') |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
214 self.keymap = {} |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
215 for n, tile in enumerate(self.imslice(image), 1): |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
216 # compare to reference tiles |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
217 for sym, key in self.keypad.items(): |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
218 if tile == key: |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
219 self.keymap[sym] = n |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
220 break |
0 | 221 |
7
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
222 # verify |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
223 if sorted(self.keymap.keys()) != list(range(10)): |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
224 raise ValueError('keypad decode failed') |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
225 |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
226 @staticmethod |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
227 def imslice(image): |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
228 for j, i in itertools.product(range(2), range(5)): |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
229 yield image.crop((83 * i, 80 * j, 83 * (i + 1), 80 * (j + 1))) |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
230 |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
231 def encode(self, passwd): |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
232 return ''.join('%02d' % self.keymap[d] for d in map(int, passwd)) |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
233 |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
234 |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
235 class BNPParibas: |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
236 def __init__(self): |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
237 self.session = requests.Session() |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
238 self.session.headers.update({'X-Requested-With': 'XMLHttpRequest'}) |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
239 |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
240 @staticmethod |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
241 def validate(response): |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
242 response.raise_for_status() |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
243 ctype, params = cgi.parse_header(response.headers.get('content-type')) |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
244 if ctype == 'application/json': |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
245 data = response.json() |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
246 # the status code may sometime be represented as string not int |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
247 code = data.get('codeRetour', -1) |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
248 if int(code) != 0: |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
249 raise requests.HTTPError() |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
250 return data |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
251 |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
252 def login(self, username, password): |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
253 url = urljoin(URL, 'identification-wspl-pres/identification') |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
254 r = self.session.get(url, params={'timestamp': int(time.time())}) |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
255 v = self.validate(r) |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
256 |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
257 keypadid = v['data']['grille']['idGrille'] |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
258 authtemplate = v['data']['authTemplate'] |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
259 |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
260 url = urljoin(URL, 'identification-wspl-pres/grille/' + keypadid) |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
261 r = self.session.get(url) |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
262 r.raise_for_status() |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
263 |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
264 keypad = Keypad(r.content) |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
265 |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
266 # fill authentication template |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
267 auth = authtemplate |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
268 auth = auth.replace('{{ idTelematique }}', username) |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
269 auth = auth.replace('{{ password }}', keypad.encode(password)) |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
270 auth = auth.replace('{{ clientele }}', '') |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
271 |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
272 url = urljoin(URL, 'SEEA-pa01/devServer/seeaserver') |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
273 r = self.session.post(url, data={'AUTH': auth}) |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
274 v = self.validate(r) |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
275 return v['data'] |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
276 |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
277 def info(self): |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
278 url = urljoin(URL, 'serviceinfosclient-wspl/rpc/InfosClient') |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
279 r = self.session.get(url, params={'modeAppel': 0}) |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
280 v = self.validate(r) |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
281 return v['data'] |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
282 |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
283 def recent(self): |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
284 url = urljoin(URL, 'udc-wspl/rest/getlstcpt') |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
285 r = self.session.get(url) |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
286 v = self.validate(r) |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
287 account = v['data']['infoUdc']['familleCompte'][0]['compte'][0] |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
288 |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
289 url = urljoin(URL, 'rop-wspl/rest/releveOp') |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
290 data = json.dumps({'ibanCrypte': account['key'], |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
291 'pastOrPending': 1, 'triAV': 0, |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
292 'startDate': None, # ddmmyyyy |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
293 'endDate': None}) |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
294 headers = {'Content-Type': 'application/json'} |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
295 r = self.session.post(url, headers=headers, data=data) |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
296 v = self.validate(r) |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
297 return v['data'] |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
298 |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
299 def messages(self): |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
300 url = urljoin(URL, 'bmm-wspl/recupMsgRecu') |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
301 r = self.session.get(url, params={'nbMessagesParPage': 200, 'index': 0}) |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
302 v = self.validate(r) |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
303 return v['data'] |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
304 |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
305 def message(self, mid): |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
306 # required to set some cookies required by the next call |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
307 url = urljoin(URL, 'fr/connexion/mes-outils/messagerie') |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
308 r = self.session.get(url) |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
309 self.validate(r) |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
310 |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
311 url = urljoin(URL, 'bmm-wspl/recupMsg') |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
312 r = self.session.get(url, params={'identifiant': mid}) |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
313 v = self.validate(r) |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
314 return v['data'] |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
315 |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
316 |
9 | 317 def main(): |
318 conffile = sys.argv[1] | |
7
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
319 conf = loadconf(conffile) |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
320 |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
321 db = sqlite3.connect(conf['DATABASE']) |
0 | 322 db.execute('''CREATE TABLE IF NOT EXISTS messages (id TEXT PRIMARY KEY)''') |
323 db.execute('''CREATE TABLE IF NOT EXISTS transactions (id INTEGER PRIMARY KEY)''') | |
324 | |
7
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
325 sendmail = Mailer(host=conf['SMTPHOST'], |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
326 port=conf['SMTPPORT'], |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
327 starttls=conf['SMTPSTARTTLS'], |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
328 username=conf['SMTPUSER'], |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
329 password=conf['SMTPPASSWD']).send |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
330 |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
331 encrypt = GPG(conf['GNUPGHOME']).encrypt |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
332 |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
333 remote = BNPParibas() |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
334 remote.login(conf['USERNAME'], conf['PASSWORD']) |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
335 |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
336 ## transactions |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
337 recent = remote.recent() |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
338 data = recent['listerOperations']['compte'] |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
339 transactions = [ Transaction.fromjson(x) for x in data['operationPassee'] ] |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
340 balance = data['soldeDispo'] |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
341 |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
342 curs = db.cursor() |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
343 unseen = [] |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
344 for t in transactions: |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
345 curs.execute('''SELECT COUNT(*) FROM transactions WHERE id = ?''', (t.id, )) |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
346 if not curs.fetchone()[0]: |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
347 # not seen before |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
348 unseen.append(t) |
0 | 349 |
11 | 350 if unseen: |
351 lines = [] | |
352 lines.append(HEADER) | |
353 lines.append(SEP) | |
354 for t in unseen: | |
355 lines.append(str(t)) | |
356 lines.append(SEP) | |
357 lines.append(FOOTER.format(balance=balance)) | |
7
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
358 |
11 | 359 text = '\n'.join(lines) |
360 payload = encrypt(text, conf['MAILFROM'], conf['MAILTO']) | |
7
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
361 |
11 | 362 message = MIMEText(payload, _charset='utf8 7bit') |
363 message['Subject'] = 'BNP Paribas Account update' | |
364 message['From'] = conf['MAILFROM'] | |
365 message['To'] = conf['MAILTO'] | |
366 message['Date'] = format_datetime(localtime()) | |
367 | |
368 sendmail(message) | |
7
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
369 |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
370 curs.executemany('''INSERT INTO transactions (id) VALUES (?)''', ((x.id, ) for x in unseen)) |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
371 db.commit() |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
372 |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
373 ## messages |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
374 data = remote.info() |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
375 info = data['abonnement'] |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
376 nnew = info['nombreMessageBMMNonLus'] + info['nombreMessageBilatNonLus'] |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
377 |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
378 data = remote.messages() |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
379 for m in data['messages']: |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
380 |
0 | 381 curs = db.cursor() |
11 | 382 curs.execute('''SELECT COUNT(*) FROM messages WHERE id = ?''', (m['id'], )) |
0 | 383 if curs.fetchone()[0]: |
384 # already handled | |
385 continue | |
386 | |
11 | 387 text = Message.fromjson(remote.message(m['id'])) |
388 payload = encrypt(str(text), conf['MAILFROM'], conf['MAILTO']) | |
0 | 389 |
11 | 390 message = MIMEText(payload, _charset='utf8 7bit') |
7
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
391 message['Subject'] = 'BNP Paribas Message' |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
392 message['From'] = conf['MAILFROM'] |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
393 message['To'] = conf['MAILTO'] |
0 | 394 message['Date'] = format_datetime(localtime()) |
7
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
395 |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
396 sendmail(message) |
0 | 397 |
7
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
398 curs.execute('''INSERT INTO messages (id) VALUES (?)''', (m['id'], )) |
90f4e0bd0c2d
Almost complete rewrite to adapt to the new BNP Paribas website
Daniele Nicolodi <daniele@grinta.net>
parents:
6
diff
changeset
|
399 db.commit() |
0 | 400 |
401 | |
402 if __name__ == '__main__': | |
9 | 403 main() |