Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
k-zakhariy committed Jun 30, 2022
0 parents commit 89fa357
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
data.csv
.DS_Store
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
run:
python calc.py
install:
pip install -r requirements.txt
62 changes: 62 additions & 0 deletions calc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from csv import DictReader
from genericpath import exists
from tabulate import tabulate
from os.path import exists
import sys
import requests
import time

import html2text
text_maker = html2text.HTML2Text()

table = [['Дата', 'Значення', 'Валюта', 'Курс', 'Сумма']]
total_income = 0
total_five_percent = 0
total_ten_percent = 0
if not exists('data.csv'):
print('File data.csv does not exists')
sys.exit()

with open('data.csv') as read_obj:
csv_dict_reader = DictReader(read_obj)

for row in csv_dict_reader:
date = row["date"]
value = row['value']
currency = row['currency']

result = 0
currencyAmount = 0
if currency != 'UAH':
response = requests.get(
"https://bank.gov.ua/NBU_Exchange/exchange?date="+date+"&json")

if response.text[0] == '<':
print("Failed for date - "+date)
print(text_maker.handle(response.text))
time.sleep(3)
continue

time.sleep(2)
resJson = response.json()

for x in resJson:
if x["CurrencyCodeL"] == currency and currency != 'UAH':
currencyAmount = x['Amount']
result = round(float(x["Amount"]) * float(value))

else:
result = float(value)
value = ""

total_income += result

table.append([date, value, currency, currencyAmount,
"{:,.2f} UAH".format(result)])

total_five_percent = (total_income / 100) * 5
total_ten_percent = ((total_income - total_five_percent) / 100) * 10

print(tabulate(table, headers='firstrow', tablefmt='fancy_grid'))
print(tabulate([["Total", "5%", "10%"], [total_income, total_five_percent,
total_ten_percent]], headers='firstrow', tablefmt='fancy_grid'))
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
requests
tabulate

0 comments on commit 89fa357

Please sign in to comment.