Last active
November 15, 2020 21:50
-
-
Save rgiaviti/0eeb1375d8d156b9185b731ecfcb7dee to your computer and use it in GitHub Desktop.
BACEN parser IPCA
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
import datetime | |
bacen_ipca_url = "https://api.bcb.gov.br/dados/serie/bcdata.sgs.189/dados?formato=json" | |
output_json = "/home/bla/Desktop/igpm.json" | |
metadata = { | |
"status": "ATIVO", | |
"data-ultima-atualizacao": datetime.datetime.now().strftime('%d/%m/%Y'), | |
"versao": "1.1.0", | |
"site": "https://github.com/rgiaviti/open-data/blob/master/economia/inflacao/ipca.json", | |
"fontes": ["FGV", "Banco Central do Brasil"] | |
} | |
def preppend_metadata(metadata, payload): | |
print(" :: preppending metadata") | |
full_payload = { "meta-info": metadata, "ipca": payload } | |
return full_payload | |
def save_to_file(file, payload): | |
print(" :: saving to json file") | |
with open(file, 'w') as outfile: | |
json.dump(payload, outfile, default = date_converter) | |
def reorder_data(payload): | |
print(":: reordering data most recent to oldest") | |
ordered_payload = sorted(payload, key=lambda k: k['data'], reverse=True) | |
return ordered_payload | |
def convert_values(payload): | |
print(":: converting payload from string to date and float types") | |
converted_payload = [] | |
for index in payload: | |
converted_payload.append( | |
{ | |
"data": datetime.datetime.strptime(index["data"], '%d/%m/%Y'), | |
"valor": float(index["valor"]) | |
} | |
) | |
return converted_payload | |
def date_converter(o): | |
if isinstance(o, datetime.datetime): | |
return "{}/{}".format(o.month, o.year) | |
""" | |
Início da execução do Script | |
""" | |
try: | |
response = requests.get(bacen_ipca_url) | |
if (response.status_code != 200): | |
print(":: request failed") | |
print(f":: http status code %s", response.status_code) | |
else: | |
print(":: request to bacen successful") | |
converted_data = convert_values(response.json()) | |
ordered_data = reorder_data(converted_data) | |
full_payload = preppend_metadata(metadata, ordered_data) | |
save_to_file(output_json, full_payload) | |
#full_json = preppend_metadata(metadata, reordered_data) | |
#full_json = add_missing_metadata_info(full_json) | |
# save_to_file(full_json) | |
except Exception as e: | |
print(":: something bad happened") | |
print(f":: error:", e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment