-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetting_trans_merchants.py
More file actions
59 lines (48 loc) · 1.9 KB
/
getting_trans_merchants.py
File metadata and controls
59 lines (48 loc) · 1.9 KB
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
import requests
import argparse
import logging
import os
ULR_MERCH = 'https://simpledebit.gocardless.io/merchants'
TRANS_API = 'https://simpledebit.gocardless.io/merchants/'
PAYMENTS_LOG = 'payments.log'
logging.basicConfig(level=logging.INFO,
handlers=[
logging.StreamHandler(),
logging.FileHandler(PAYMENTS_LOG)
])
def write_file(filename: str, iban: str, transactions: int) -> None:
with open(filename, "a") as f:
f.write(f'{iban},{str(transactions)}\n')
def transactions_calc(trans_list: list, discount: int) -> int:
calc_of_amount = 0
calc_of_fees = 0
for i in trans_list:
calc_of_amount += int(i["amount"])
calc_of_fees += int(i["fee"])
return round(calc_of_amount - calc_of_fees + calc_of_fees*discount/100)
def get_api_transactions(merchant: str, filename: str) -> None:
try:
full_api = TRANS_API + merchant
r = requests.get(full_api)
if r.status_code == 200:
write_file(filename, r.json()['iban'], transactions_calc(r.json()['transactions'], r.json()['discount']['fees_discount']))
else:
write_file("no response")
except PermissionError as e:
logging.error(f'Permission error: {e}')
def get_api_mechants(url: str, filename: str) -> None:
r = requests.get(url)
if os.path.exists(filename):
os.remove(filename)
for i in r.json():
try:
get_api_transactions(i, filename)
except Exception as e:
logging.error(f'Unknown error: {e}')
def main():
parser = argparse.ArgumentParser(description='Program captures merchants and calculates heir balance with otput to a file payments.csv')
parser.add_argument('filename', nargs='?', default='payments.csv')
args = parser.parse_args()
get_api_mechants(ULR_MERCH, args.filename)
if __name__=="__main__":
main()