Skip to content

Instantly share code, notes, and snippets.

@srn

srn/bankdata.py Secret

Created March 27, 2014 12:02
Show Gist options
  • Save srn/443db556a0a9b07130f4 to your computer and use it in GitHub Desktop.
Save srn/443db556a0a9b07130f4 to your computer and use it in GitHub Desktop.
"""BankData Python Wrapper
Usage:
bankdata.py <id> <pin>
bankdata.py --version
Options:
-h --help Show this screen.
--version Show version.
"""
import json
import requests
from docopt import docopt
ENDPOINT = 'https://mobil.bankdata.dk/mobil'
USER_AGENT = 'iPhone, iPhone OS, 6.0'
class BankDataApi(object):
def __init__(self, bank_id, user_id, pin_code):
self.bank_id = bank_id
self.personal_id = 0
self._r = None
r = self.login(user_id, pin_code)
a, _json = self.get_accounts()
for account in _json['accounts']:
print account['name'], account['balance']
def login(self, user_id, pin_code):
data = {'userId': user_id, 'pinCode': pin_code}
return self._request('login', data, method='post')
def get_accounts(self):
return self._request('accounts')
def get_transactions_for_account(self, account, offset=0, transactions=[]):
url = 'accounts/%s/%s/postings' % (account['regNo'], account['accountNo'])
if offset > 0:
url += '?pageOffset=%s' % offset
r, _json = self._request(url)
transactions.extend(_json['postings'])
if _json['hasMorePostings']:
return self.get_transactions_for_account(account, offset + 1, transactions)
for transaction in transactions:
print transaction['text'], transaction['amount']
return transactions
def _request(self, url, data=None, method='get', headers=None):
endpoint = '%s/%s' % (ENDPOINT, url)
if self._r is None:
self._r = requests.Session()
if headers is None:
headers = self.get_headers()
_data = json.dumps(data)
if data:
r = getattr(self._r, method)(endpoint, data=_data, headers=headers)
else:
r = getattr(self._r, method)(endpoint, headers=headers)
_json = json.loads(r.text)
if 'personalId' in _json:
self.personal_id = _json['personalId']
return r, _json
def get_headers(self):
headers = {'Content-Type': 'application/json;charset=utf-8',
'Accept': 'application/json',
'User-Agent': USER_AGENT,
'x-bankNo': self.bank_id,
'Accept-Charset': 'utf-8',
'x-version': '2.0.0',
'Accept-Language': '2.0.0',
'x-buildNo': '1204',
'x-deviceType': 'phone',
'x-platform': 'ios',
'Accept-Encoding': 'gzip'}
if self.personal_id:
headers['x-personalId'] = self.personal_id
return headers
if __name__ == '__main__':
arguments = docopt(__doc__, version='0.0.1')
api = BankDataApi(51, arguments['<id>'], arguments['<pin>'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment