-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_fill.py
23 lines (18 loc) · 907 Bytes
/
db_fill.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from sqlalchemy import insert
from requests import get
from math import trunc
from datetime import datetime, timedelta
from dbschema import historical_rates_table, db_engine
current_date = datetime.today()
start_date = current_date - timedelta(days=180)
while start_date < current_date:
request_date = start_date.date().strftime("%Y-%m-%d")
request = get(f"https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@{request_date}/v1/currencies/usd.json")
if request.status_code == 200:
brl = trunc(float(request.json()["usd"]["brl"]) * 10**6)
eur = trunc(float(request.json()["usd"]["eur"]) * 10**6)
gbp = trunc(float(request.json()["usd"]["gbp"]) * 10**6)
with db_engine.begin() as conn:
query = insert(historical_rates_table).values(date=request_date, brl=brl, eur=eur, gbp=gbp)
conn.execute(query)
start_date += timedelta(days=1)