-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython.py
More file actions
34 lines (27 loc) · 1.16 KB
/
python.py
File metadata and controls
34 lines (27 loc) · 1.16 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
import requests
from datetime import datetime, timedelta
def MakeApiRequest(start_date):
'''
This function works as follows:
1. Generate daily list of dates from start_date
2. For every day it makes api request to get USD to EUR exchange rate
3. After receiving a response it saves it to file
:param start_date: start date
:type start_date: int
:return None:
'''
# Generate daily date from start date
print('Start parsing ...')
start = datetime.strptime(start_date, '%Y-%m-%d')
end = datetime.now()
days = (end - start).days
for diff in range(days):
# Make API request for every date
parsing_date = start + timedelta(diff)
print("Parsing: https://api.exchangeratesapi.io/" + str(parsing_date.date()))
r1 = requests.get("https://api.exchangeratesapi.io/" + str(parsing_date.date()), {'base': "USD"})
# Extract data and save to file
file = open('rates.txt', "a")
print("Saving results for: https://api.exchangeratesapi.io/" + str(parsing_date.date()))
file.write(str(parsing_date.date()) + " " + str(r1.json()['rates']["EUR"]) + "\r")
MakeApiRequest("2000-01-01")