"""Fetch the two daily US birth series published by FiveThirtyEight.

    python fetch.py

Writes to ~/data/births/. Sources (FiveThirtyEight's data repository, which
took them from the CDC's National Center for Health Statistics and from the
Social Security Administration):

    https://raw.githubusercontent.com/fivethirtyeight/data/master/births/US_births_2000-2014_SSA.csv
    https://raw.githubusercontent.com/fivethirtyeight/data/master/births/US_births_1994-2003_CDC_NCHS.csv

The SSA series 2000-2014 is the one the rule is sealed on. The NCHS series is
a second witness, and only its years not in the SSA file (1994-1999) are used,
so the two are not the same days counted twice.
"""

import pathlib
import urllib.request

BASE = "https://raw.githubusercontent.com/fivethirtyeight/data/master/births/"
FILES = ["US_births_2000-2014_SSA.csv", "US_births_1994-2003_CDC_NCHS.csv"]
OUT = pathlib.Path.home() / "data" / "births"


def main():
    OUT.mkdir(parents=True, exist_ok=True)
    for name in FILES:
        with urllib.request.urlopen(BASE + name, timeout=60) as r:
            body = r.read()
        (OUT / name).write_bytes(body)
        print(f"{name}: {len(body)} bytes")


if __name__ == "__main__":
    main()
