diff --git a/bcb/sgs/__init__.py b/bcb/sgs/__init__.py index b56c9b3..245cd68 100644 --- a/bcb/sgs/__init__.py +++ b/bcb/sgs/__init__.py @@ -1,7 +1,8 @@ +import json from io import StringIO -import requests import pandas as pd +import requests from bcb.utils import Date @@ -119,6 +120,12 @@ def get(codes, start=None, end=None, last=0, multi=True, freq=None): urd = _get_url_and_payload(code.value, start, end, last) res = requests.get(urd["url"], params=urd["payload"]) if res.status_code != 200: + try: + res_json = json.loads(res.text) + except Exception: + res_json = {} + if "error" in res_json: + raise Exception("Download error: {}".format(res_json["error"])) raise Exception("Download error: code = {}".format(code.value)) df = pd.read_json(StringIO(res.text)) df = _format_df(df, code, freq) diff --git a/notebooks/sgs get series.ipynb b/notebooks/sgs get series.ipynb index 157eb8b..03ed167 100644 --- a/notebooks/sgs get series.ipynb +++ b/notebooks/sgs get series.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 1, + "execution_count": 5, "id": "addressed-representation", "metadata": {}, "outputs": [], @@ -16,7 +16,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 6, "id": "fatty-regulation", "metadata": {}, "outputs": [], @@ -26,12 +26,74 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 7, "id": "interstate-offer", "metadata": {}, "outputs": [ { "data": { + "application/vnd.microsoft.datawrangler.viewer.v0+json": { + "columns": [ + { + "name": "Date", + "rawType": "datetime64[ns]", + "type": "datetime" + }, + { + "name": "USDBRL", + "rawType": "float64", + "type": "float" + } + ], + "conversionMethod": "pd.DataFrame", + "ref": "29c5f3d1-9117-4e6e-871b-c37c6536b282", + "rows": [ + [ + "2025-02-17 00:00:00", + "5.7105" + ], + [ + "2025-02-18 00:00:00", + "5.6979" + ], + [ + "2025-02-19 00:00:00", + "5.7087" + ], + [ + "2025-02-20 00:00:00", + "5.7019" + ], + [ + "2025-02-21 00:00:00", + "5.7027" + ], + [ + "2025-02-24 00:00:00", + "5.7258" + ], + [ + "2025-02-25 00:00:00", + "5.7779" + ], + [ + "2025-02-26 00:00:00", + "5.7757" + ], + [ + "2025-02-27 00:00:00", + "5.8228" + ], + [ + "2025-02-28 00:00:00", + "5.8488" + ] + ], + "shape": { + "columns": 1, + "rows": 10 + } + }, "text/html": [ "