changeset 17:80648a7a119c default tip

Update
author Daniele Nicolodi <daniele@grinta.net>
date Sun, 21 Jan 2018 01:29:18 +0000
parents af2e222f2dad
children
files bnpparibas.py setup.py
diffstat 2 files changed, 26 insertions(+), 55 deletions(-) [+]
line wrap: on
line diff
--- a/bnpparibas.py	Thu May 05 07:04:38 2016 +0000
+++ b/bnpparibas.py	Sun Jan 21 01:29:18 2018 +0000
@@ -11,8 +11,8 @@
 import textwrap
 import time
 
+from datetime import datetime
 from contextlib import contextmanager
-from datetime import datetime
 from email.mime.text import MIMEText
 from email.utils import format_datetime, localtime, parseaddr
 from io import BytesIO
@@ -24,7 +24,7 @@
 
 from html2text import HTML2Text
 from PIL import Image
-
+from dateutil.relativedelta import relativedelta
 
 URL = 'https://mabanque.bnpparibas/'
 
@@ -174,7 +174,7 @@
     @classmethod
     def fromjson(cls, x):
         data = {'id': x['msg']['id'],
-                'date': datetime.strptime(x['msg']['id'], '%Y-%m-%d-%H.%M.%S.%f'),
+                'date': datetime.datetime.strptime(x['msg']['id'], '%Y-%m-%d-%H.%M.%S.%f'),
                 'subject': x['msg']['objet'],
                 'sender': x['msg']['emetteur']['nom'],
                 'content': x['msg']['contenu'],
@@ -291,7 +291,7 @@
         v = self.validate(r)
         return v['data']
 
-    def recent(self):
+    def recent(self, startdate, enddate):
         url = urljoin(URL, 'udc-wspl/rest/getlstcpt')
         r = self.session.get(url)
         v = self.validate(r)
@@ -300,8 +300,8 @@
         url = urljoin(URL, 'rop-wspl/rest/releveOp')
         data = json.dumps({'ibanCrypte': account['key'],
                            'pastOrPending': 1, 'triAV': 0,
-                           'startDate': None, # ddmmyyyy
-                           'endDate': None})
+                           'startDate': '{:%d%m%Y}'.format(startdate), # ddmmyyyy
+                           'endDate': '{:%d%m%Y}'.format(enddate)})
         headers = {'Content-Type': 'application/json'}
         r = self.session.post(url, headers=headers, data=data)
         v = self.validate(r)
@@ -324,50 +324,6 @@
         v = self.validate(r)
         return v['data']
 
-    def records0(self):
-        # required to set some cookies required by the next call
-        url = urljoin(URL, 'fr/connexion/virements-services/releves-en-ligne')
-        r = self.session.get(url)
-        self.validate(r)
-
-        url = urljoin(URL, 'demat-wspl/rest/initialisationDemat')
-        r = self.session.get(url)
-        v = self.validate(r)
-
-        for branch in v['data']['initialisationDemat']['arbres']:
-            for leave in branch.get('arbre', ( )):
-                if leave['typeDoc'] == 'RELEV':
-                    data = leave
-                    break
-
-        self.iban = data['ibans'][0]['ibanCrypte']
-        query = {'famDoc': data['codeFamille'],
-                 'idTypeDocument': data['idBranche'],
-                 'listeIbanCrypte': [ self.iban, ],
-                 'typeCpt': data['typeCompte'],
-                 'typeDoc': data['typeDoc'],
-                 'typeFamille': 'R001'} # ???
-
-        url = urljoin(URL, 'demat-wspl/rest/consultationDemat')
-        data = json.dumps(query)
-        headers = {'Content-Type': 'application/json'}
-        r = self.session.post(url, headers=headers, data=data)
-        v = self.validate(r)
-
-        years = v['data']['consultationDemat']['listeCompte'][0]['listeAnnee']
-        query['codeProduit'] = ''
-        documents = []
-
-        url = urljoin(URL, 'demat-wspl/rest/rechercheDemat')
-        for year in years:
-            query['anneeSelectionnee'] = year
-            data = json.dumps(query)
-            r = self.session.post(url, headers=headers, data=data)
-            v = self.validate(r)
-            documents += v['data']['consultationDemat']['listeCompte'][0]['listeDocument']
-
-        return documents
-
     def records(self):
         url = urljoin(URL, 'fr/connexion/mes-outils/dematerialisation')
         r = self.session.get(url)
@@ -403,6 +359,7 @@
 def transactions(conf):
     db = sqlite3.connect(conf['DATABASE'])
     db.execute('''CREATE TABLE IF NOT EXISTS transactions (id INTEGER PRIMARY KEY)''')
+    db.execute('''CREATE TABLE IF NOT EXISTS last (date TEXT)''')
 
     sendmail = Mailer(host=conf['SMTPHOST'],
                       port=conf['SMTPPORT'],
@@ -415,10 +372,22 @@
     remote = BNPParibas()
     remote.login(conf['USERNAME'], conf['PASSWORD'])
 
-    recent = remote.recent()
-    data = recent['listerOperations']['compte']
-    transactions = [ Transaction.fromjson(x) for x in data['operationPassee'] ]
-    balance = data['soldeDispo']
+    # last date
+    curs = db.cursor()
+    curs.execute('''SELECT date FROM last''')
+    row = curs.fetchone()
+    startdate = datetime.strptime(row[0], '%d-%m-%Y') if row else datetime(2016, 10, 1)
+
+    transactions = []
+    while startdate <= datetime.now():
+        enddate = startdate + relativedelta(months=2)
+        recent = remote.recent(startdate, enddate)
+        data = recent['listerOperations']['compte']
+        transactions += [ Transaction.fromjson(x) for x in data['operationPassee'] ]
+        balance = data['soldeDispo']
+        startdate = enddate
+
+    transactions.sort(key=lambda x: datetime.strptime(x.date, '%d-%m-%Y'))
 
     curs = db.cursor()
     unseen = []
@@ -449,6 +418,7 @@
         sendmail(message)
 
     curs.executemany('''INSERT INTO transactions (id) VALUES (?)''', ((x.id, ) for x in unseen))
+    curs.execute('''INSERT OR REPLACE INTO 'last' (date) VALUES (?)''', (transactions[-1].date, ))
     db.commit()
 
 
--- a/setup.py	Thu May 05 07:04:38 2016 +0000
+++ b/setup.py	Sun Jan 21 01:29:18 2018 +0000
@@ -16,7 +16,8 @@
           'Pillow',
           'click',
           'html2text',
-          'requests', ],
+          'requests',
+          'python-dateutil'],
       entry_points={
           'console_scripts': [
               'bnpparibas = bnpparibas:main', ],