-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponses.py
More file actions
116 lines (78 loc) · 3.2 KB
/
responses.py
File metadata and controls
116 lines (78 loc) · 3.2 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import requests
import pprint
import datetime
import os
from matplotlib import pyplot as plt
"""
To get the list of the latest foreign exchange rates
GET /latest
To
GET /latest?symbols=USD,GBP
"""
base_url = 'https://api.exchangeratesapi.io/'
end_path_latest = 'latest'
currency_base = ['CAD', 'HKD', 'ISK', 'PHP', 'DKK', 'HUF', 'CZK', 'AUD', 'RON', 'SEK', 'IDR', 'INR', 'BRL', 'RUB', 'HRK', 'JPY', 'THB', 'CHF', 'SGD', 'PLN', 'BGN', 'TRY', 'CNY', 'NOK', 'NZD', 'ZAR', 'USD', 'MXN', 'ILS', 'GBP', 'KRW', 'MYR', 'CAD', 'HKD', 'ISK', 'PHP', 'DKK', 'HUF', 'CZK', 'AUD', 'RON', 'SEK', 'IDR', 'INR', 'BRL', 'RUB', 'HRK', 'JPY', 'THB', 'CHF', 'SGD', 'PLN', 'BGN', 'TRY', 'CNY', 'NOK', 'NZD', 'ZAR', 'USD', 'MXN', 'ILS', 'GBP', 'KRW', 'MYR']
def default_response(user_input):
message = str(user_input).lower()
return "Choose options from the list of available commands:\n /list\n /exchange\n /history"
def list_response():
end_point = f'{base_url}{end_path_latest}'
result = requests.get(end_point)
result_dict = {}
data = result.json()
data['rates']
for key, value in data['rates'].items():
result_dict[key] = data['rates'][key]
return(result_dict)
# get previously saved data (the last request from user <= 10 mins, data is up to date)
def list_response_database()
pass
# a result of exchange
def exchange_response(currency, amount):
end_point = f'{base_url}{end_path_latest}'
given_currency = currency
end_path_exchange = f'latest?symbols={given_currency}&base=USD'
end_point = f'{base_url}{end_path_exchange}'
result = requests.get(end_point)
_currency = []
exchange_rate = []
data = result.json()
data['rates']
for key, value in data['rates'].items():
exchange_rate.append(data['rates'][key])
_currency.append(key)
rate = exchange_rate[0]
return int(amount) * rate
def history_response(currency):
_base = 'USD'
_currency = currency
date_until = datetime.datetime.now().strftime('%Y-%m-%d')
date_start = datetime.timedelta(days=7)
date_from = (datetime.datetime.now() - date_start).strftime('%Y-%m-%d')
history_period = f'{base_url}history?start_at={date_from}&end_at={date_until}&symbols={_currency}&base={_base}'
history_result = requests.get(history_period)
"""
keys - date
values - exchange rate
"""
date = []
_exchange_rate = []
_result = history_result.json()
for key, value in _result['rates'].items():
_exchange_rate.append(_result['rates'][key])
date.append(key)
_exchange_rate_keys = []
_exchange_rate_value = []
for data in _exchange_rate:
for key, value in data.items():
_exchange_rate_keys.append(key)
_exchange_rate_value.append(value)
exchange_rate_result = zip(date, _exchange_rate_value)
""" Building graph with Matplotlib """
fig = plt.figure()
plt.plot(date, _exchange_rate_value, '-ob')
plt.title(f"{_currency}/{_base} trend in time range {date_from} to {date_until}")
plt.legend([f'{_currency}'])
plot_name = f"temporary_images/{_currency} {_base} trend in time range {date_from} to {date_until}.png"
fig.savefig(plot_name)
return plot_name