-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathppi.py
More file actions
34 lines (26 loc) · 1.09 KB
/
Copy pathppi.py
File metadata and controls
34 lines (26 loc) · 1.09 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 json
import csv
from functools import reduce
from bls_client import BLSClient
import os
import sys
from common import DATA_DIR, DEFAULT_START_YEAR, DEFAULT_END_YEAR
def main(start_year=DEFAULT_START_YEAR, end_year=DEFAULT_END_YEAR):
with open("data_ids/ppi_commodities_codes.txt") as file:
ids = file.read().split("\n")
client = BLSClient(os.getenv("BLS_API_KEY"))
results = client.bulk_load(ids, start_year=start_year, end_year=end_year)
with open(DATA_DIR / "ppi_commodities_data.json", "w") as output:
output.write(json.dumps(results))
with open(DATA_DIR / "ppi_commodities_data.json") as file:
results = json.loads(file.read())
csvs = [ client.convert_to_csv(bulk, ["commerce_sector", "item"]) for bulk in results ]
data = reduce(lambda a, b: a + b[1:], csvs)
with open(DATA_DIR / "ppi_commodities.csv", "w", newline="") as f:
writer = csv.writer(f)
writer.writerows(data)
if __name__ == "__main__":
start_year = DEFAULT_START_YEAR
if len(sys.argv) > 1:
start_year = sys.argv[1]
main(start_year=start_year)