-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcurrency.py
More file actions
28 lines (24 loc) · 964 Bytes
/
currency.py
File metadata and controls
28 lines (24 loc) · 964 Bytes
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
import json
from datetime import datetime, timedelta
import requests
def get_currency_exchange_rates(currency):
base = "USD" # reference currency
start_date = str(datetime.now() - timedelta(1))[:11] # yesterday
end_date = str(datetime.today())[:11] # today
url = 'https://api.exchangerate.host/timeseries?base={0}&start_date={1}&end_date={2}&symbols={3}'.format(base, start_date, end_date, currency)
response = requests.get(url)
data = response.json()
yesterday, today = "", ""
if currency == "JPY":
yesterday = str(data["rates"])[23:33]
today = str(data["rates"])[58:68]
# print(yesterday)
# print(today)
else:
yesterday = str(data["rates"])[23:30]
today = str(data["rates"])[56:64]
# print(yesterday) # yesterday
# print(today) # today
data_set = {'currency': currency, 'yesterday': yesterday, 'today': today}
print(data_set)
return data_set