-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCurrencyExchangeRate.py
More file actions
executable file
·92 lines (69 loc) · 2.9 KB
/
CurrencyExchangeRate.py
File metadata and controls
executable file
·92 lines (69 loc) · 2.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
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
#!/bin/python3
import requests
import sys
import json
import argparse
# this funcion call a help of that script
def Help():
parser = argparse.ArgumentParser(
prog = 'CurrencyExxhangeRate.py',
description = '''Script process a NBP's API response \n
and return a currency exange rate''',
)
parser.add_argument('currencyName')
args=parser.parse_args()
# assumption: length of columnOne and columnTwo is equal
# function is printing a table with headlines and data
def TablePrint(headlineOne:str, headlineTwo:str, columnOne:list, columnTwo:list):
if len(columnOne) != len(columnTwo):
# if that statement is true, its a critital error
sys.exit(-1)
iterator = 0
print(headlineOne+"\t"+"\t"+headlineTwo)
for iterator in range(len(columnOne)):
print(str(columnOne[iterator])+"\t"+str(columnTwo[iterator]))
print("")
# funcion send call to API about currnet currency exange rate
def CurrentExangeRate(CurrencyName:str):
url = f"http://api.nbp.pl/api/exchangerates/rates/A/{CurrencyName}/"
try:
# it's standard return of this function, if everything gone well
currentExangeRate = float((json.loads(requests.get(url).text))["rates"][0]['mid'])
print(f"Current {CurrencyName} exange rate {currentExangeRate}", end = "\n")
except:
# if API return an error, that excpet will return and exit from program
print(f"currency {CurrencyName} don't exist")
sys.exit(-1)
# funcion counts currency price changes
def Diffrence(dates:list, currencyRates:list):
datesDiffrences = []
ratesDiffrence = []
currencyLen = len(currencyRates)
for iterator in range(1, len(currencyRates)):
datesDiffrences.append("from "+dates[iterator-1]+" to "+dates[iterator])
ratesDiffrence.append((currencyRates[iterator]-currencyRates[iterator-1]))
iterator += 1
# all data is transferred to TablePrint function, to be displayed as a table
TablePrint("days", "rate change", datesDiffrences, ratesDiffrence)
def Last5DaysCurrentExangeRate(CurrencyName:str):
lastDays = 5
try:
# it's standard algorithm of funcion, if everything gone well
url = f"http://api.nbp.pl/api/exchangerates/rates/A/{CurrencyName}/last/{lastDays}/"
fiveDaysCurrency = json.loads(requests.get(url).text)["rates"]
dates = []
currencyRates = []
for iterator in fiveDaysCurrency:
currencyRates.append(iterator["mid"])
dates.append(iterator["effectiveDate"])
TablePrint("Dates", "CurrencyRates", dates, currencyRates)
Diffrence(dates, currencyRates)
except:
print(f"currency {CurrencyName} don't exist")
sys.exit(-1)
# it's standard main function of script
if __name__ == "__main__":
Help()
currency = sys.argv[1]
CurrentExangeRate(currency)
Last5DaysCurrentExangeRate(currency)