mirror of
https://github.com/PR0M3TH3AN/python_charts.git
synced 2026-10-27 08:50:39 +00:00
65 lines
1.8 KiB
Python
65 lines
1.8 KiB
Python
#!/usr/bin/env python3
|
|
"""refresh_data.py
|
|
-----------------
|
|
Download one or more FRED series into ``data/fred.db``.
|
|
|
|
Run this script on a machine with internet access and commit the updated
|
|
database afterwards.
|
|
|
|
Example:
|
|
python scripts/refresh_data.py --series UNRATE CPIAUCSL --start 1960-01-01
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import date
|
|
import argparse
|
|
import pathlib
|
|
import sqlite3
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
from pandas_datareader.data import DataReader
|
|
import pandas as pd
|
|
|
|
from scripts.constants import DB_PATH_DEFAULT, UNRATE, DCOILWTICO
|
|
|
|
HERE = pathlib.Path(__file__).resolve().parent
|
|
DATA_PATH = DB_PATH_DEFAULT.parent
|
|
DB_FILE = DB_PATH_DEFAULT
|
|
|
|
DEFAULT_START = "1948-01-01" # earliest UNRATE observation
|
|
DEFAULT_END = date.today().isoformat()
|
|
DEFAULT_SERIES = (UNRATE, DCOILWTICO)
|
|
|
|
|
|
def main(argv: list[str] | None = None) -> None:
|
|
p = argparse.ArgumentParser(description="Download FRED series to SQLite")
|
|
p.add_argument(
|
|
"--series",
|
|
nargs="+",
|
|
default=list(DEFAULT_SERIES),
|
|
help="FRED series names",
|
|
)
|
|
p.add_argument("--start", type=str, default=DEFAULT_START, help="start date")
|
|
p.add_argument("--end", type=str, default=DEFAULT_END, help="end date")
|
|
args = p.parse_args(argv)
|
|
|
|
DATA_PATH.mkdir(exist_ok=True)
|
|
with sqlite3.connect(DB_FILE) as conn:
|
|
for s in args.series:
|
|
logger.info("\u21af downloading %s …", s)
|
|
df = DataReader(s, "fred", args.start, args.end)
|
|
df.to_sql(s, conn, if_exists="replace", index_label="date")
|
|
logger.info("\u2713 wrote %s: %s rows", s, len(df))
|
|
logger.info(
|
|
"\nDone. SQLite DB at %s (%s KB)",
|
|
DB_FILE,
|
|
DB_FILE.stat().st_size / 1024,
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|