-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
122 lines (98 loc) · 3.62 KB
/
main.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
import requests
import json
import os
from prettytable import PrettyTable
from collections import Counter
from requests.exceptions import HTTPError
from helpers.utils import *
BASE_URL = "https://api.tink.com"
ACCESS_TOKEN = ""
def get_transactions(limit="10"):
"""
Get last N transactions for a particular provider.
Default limit set to 10.
"""
url = BASE_URL + "/api/v1/search"
params = {"limit": limit}
headers = {"Authorization": "Bearer " + ACCESS_TOKEN}
try:
response = requests.get(url, params=params, headers=headers)
response.raise_for_status()
except HTTPError as http_err:
print(f"** HTTP error occurred: \n{http_err}")
raise SystemExit()
table = PrettyTable()
table.field_names = ["Date", "Transaction Description", "Amount", "Currency"]
for result in response.json()["results"]:
if result["type"] == "TRANSACTION":
table.add_row(
[
timestampt_to_date(result["transaction"]["date"]),
result["transaction"]["description"],
result["transaction"]["amount"],
result["transaction"]["currencyDenominatedOriginalAmount"][
"currencyCode"
],
]
)
print("\nShowing last " + str(limit) + " transactions")
print(table)
def get_last_unique_transactions(months=6):
"""
Gets unique transaction descriptions in last N months incluiding number of ocurrences
and shows the most frequent ones first.
Default months set to 6
"""
start_date = get_current_date()
end_date = get_relative_delta_date_in_months(months)
transactions = []
url = BASE_URL + "/api/v1/search"
params = {"startDate": start_date, "endDate": end_date}
headers = {"Authorization": "Bearer " + ACCESS_TOKEN}
try:
response = requests.get(url, headers=headers, params=params)
response.raise_for_status()
except HTTPError as http_err:
print(f"** HTTP error occurred: \n{http_err}")
raise SystemExit()
for result in response.json()["results"]:
if result["type"] == "TRANSACTION":
transactions.append(result["transaction"]["description"])
total_transactions = Counter(transactions)
table = PrettyTable()
table.field_names = ["Description", "Count"]
for value, count in total_transactions.most_common():
table.add_row([value, count])
print("\nShowing unique transactions in the last " + str(months) + " months")
print(table)
def get_access_token():
"""
Generates an ACCESS_TOKEN which is needed for further API calls
Documentation: https://docs.tink.com/resources/getting-started/retrieve-access-token
"""
print("Getting access token")
try:
response = requests.post(
BASE_URL + "/api/v1/oauth/token",
data={
"code": os.environ.get("CODE"),
"client_id": os.environ.get("CLIENT_ID"),
"client_secret": os.environ.get("CLIENT_SECRET"),
"grant_type": "authorization_code",
},
)
response.raise_for_status()
except HTTPError as http_err:
print(f"** HTTP error occurred: \n{http_err}")
print(f"Please make sure your CODE, CLIENT_ID and CLIENT_SECRET are valid ")
raise SystemExit()
json_data = response.json()
global ACCESS_TOKEN
ACCESS_TOKEN = json_data["access_token"]
print("access token OK")
if __name__ == "__main__":
get_access_token()
# Task 1:
get_transactions(10)
# Task 2:
get_last_unique_transactions(3)