-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathing_api.py
157 lines (127 loc) · 4.91 KB
/
ing_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
"""
ing_api
~~~~~~~~~~~~
This module implements provides a simple interface for ING Bank.
:copyright: (c) 2020 by Jan Pecek.
:license: MIT, see LICENSE for more details.
"""
from datetime import date
import requests
class IngClient:
"""
ING Client provides simple interface to connect to ING Bank API (currently deployed in CZ, ES)
Instantiate with: IngClient(country, cookie)
country - name of the country (defined by `IngClient.Country`)
cookie - Cookie header copied from web browser
Usage::
>>> import datetime
>>> from ing_api import IngClient
>>> cookie = 'copied value of Cookie header from browser'
>>> api = IngClient(IngClient.Country.CZ, cookie)
>>> # get information about the client
>>> client = api.client()
>>> # get all products of the client
>>> products = api.products()
>>> # get list of transactions (movements), see more arguments
>>> product_id = 'uuid'
>>> from_date = datetime.date(1970, 1, 1)
>>> movements = api.movements(product_id, from_date=from_date)
>>> # get transaction (movement) detail
>>> movement = api.movement('uuid')
"""
class Country:
CZ = 'CZ'
ES = 'ES'
_BASE_URLS = {
Country.CZ: 'https://ib.ing.cz/genoma_api/rest',
Country.ES: 'https://ing.ingdirect.es/genoma_api/rest'
}
_REFERERS = {
Country.CZ: 'https://ib.ing.cz/transactional-cz/',
Country.ES: 'https://ing.ingdirect.es/app-login/'
}
_USER_AGENT = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:66.0) Gecko/20100101 Firefox/66.0'
_HEADERS = {
'User-Agent': _USER_AGENT,
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate, br',
'Content-Type': 'application/json; charset=utf-8',
'X-Requested-With': 'XMLHttpRequest'
}
_GENOMA_SESSION_NAME = 'genoma-session-id'
_CLIENT_PATH = '/client'
_PRODUCTS_PATH = '/products'
_PRODUCT_PATH = _PRODUCTS_PATH + '/%s'
_MOVEMENTS_PATH = _PRODUCT_PATH + '/movements'
_MOVEMENT_PATH = '/movements/%s'
def __init__(self, country, cookie):
""" Initialize class
:param country: Name of the country
:type country: IngClient.Country
:param cookie: Value of Cookie header
:type cookie: str
:return: object `IngClient`
:rtype: IngClient
"""
self._country = country
self._cookie = cookie
self._genoma_session_id = self._extract_genoma_session_id(cookie)
def client(self):
r"""
Get logged client information
:return: JSON object with result
:rtype: dict
"""
response = requests.get(self._url(self._CLIENT_PATH), headers=self._headers)
return response.json()
def products(self):
r"""Get list of products
:return: JSON object with result
:rtype: list
"""
response = requests.get(self._url(self._PRODUCTS_PATH), headers=self._headers)
return response.json()
def movements(self, product_uuid, from_date, to_date=date.today(), limit=25, offset=0):
r"""Get list of movements for specific product
:param product_uuid: UUID of product
:type product_uuid: str
:param from_date: starting date of the selection
:type from_date: date
:param to_date: ending date of the selection (default today)
:type to_date: date
:param limit: number of items which should be loaded
:type limit: int
:param offset: offset for pagination
:type offset: int
:return: JSON object with result
:rtype: dict
"""
params = {
'fromDate': from_date.strftime('%d/%m/%Y'),
'toDate': to_date.strftime('%d/%m/%Y'),
'limit': limit,
'offset': offset
}
response = requests.get(self._url(self._MOVEMENTS_PATH % product_uuid), params=params, headers=self._headers)
return response.json()
def movement(self, movement_uuid):
r"""Get one movement (transaction) details
:param movement_uuid: UUID of movement
:type movement_uuid: str
:return: JSON object with result
:rtype: dict
"""
response = requests.get(self._url(self._MOVEMENT_PATH % movement_uuid), headers=self._headers)
return response.json()
def _url(self, path):
return '%s%s' % (self._BASE_URLS[self._country], path)
def _extract_genoma_session_id(self, cookie):
return [el for el in cookie.split(';') if self._GENOMA_SESSION_NAME in el][0].split('=')[1]
@property
def _headers(self):
headers = self._HEADERS.copy()
headers['Referer'] = self._REFERERS[self._country]
headers['Cookie'] = self._cookie
headers[self._GENOMA_SESSION_NAME] = self._genoma_session_id
return headers