├── .gitignore ├── .travis.yml ├── README.md ├── ipeaData └── ipeadata.py ├── requirements.txt ├── setup.py └── test └── test_api.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | 49 | # Translations 50 | *.mo 51 | *.pot 52 | 53 | # Django stuff: 54 | *.log 55 | local_settings.py 56 | 57 | # Flask stuff: 58 | instance/ 59 | .webassets-cache 60 | 61 | # Scrapy stuff: 62 | .scrapy 63 | 64 | # Sphinx documentation 65 | docs/_build/ 66 | 67 | # PyBuilder 68 | target/ 69 | 70 | # Jupyter Notebook 71 | .ipynb_checkpoints 72 | 73 | # pyenv 74 | .python-version 75 | 76 | # celery beat schedule file 77 | celerybeat-schedule 78 | 79 | # SageMath parsed files 80 | *.sage.py 81 | 82 | # dotenv 83 | .env 84 | 85 | # virtualenv 86 | .venv 87 | venv/ 88 | ENV/ 89 | 90 | # Spyder project settings 91 | .spyderproject 92 | .spyproject 93 | 94 | # Rope project settings 95 | .ropeproject 96 | 97 | # mkdocs documentation 98 | /site 99 | 100 | # mypy 101 | .mypy_cache/ 102 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | python: 3 | - "3.5" 4 | - "3.6" 5 | install: 6 | - pip install -r requirements.txt 7 | # command to run tests 8 | script: 9 | - pylint ipeaData/ipeadata.py 10 | - coverage run --source=. -m unittest discover -s test -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # pyIpeaData 2 | 3 | ## Install 4 | 5 | ``` 6 | pip install ipeaData 7 | ``` 8 | 9 | ## Use 10 | 11 | ``` 12 | from ipeaData.ipeadata import ipeadata 13 | 14 | ipeadata('ADMIS') 15 | ``` 16 | -------------------------------------------------------------------------------- /ipeaData/ipeadata.py: -------------------------------------------------------------------------------- 1 | """ Module to get data from ipeadata.com.br """ 2 | import requests as req 3 | import pandas as pd 4 | 5 | 6 | def basic_api_call(api): 7 | """ 8 | Function to make a call on ipedata api 9 | :param api: url on api 10 | :return: a dataFrame with data 11 | """ 12 | response = req.get(api) 13 | if response.status_code == req.codes.ok: # pylint: disable=no-member 14 | json_response = response.json() 15 | if 'value' in json_response: 16 | try: 17 | data_frame = pd.DataFrame(json_response['value']) 18 | return data_frame 19 | except Exception: # pylint: disable=broad-except 20 | return None 21 | return None 22 | 23 | def get_sources(): 24 | """ 25 | Get sources from ipea web site 26 | :return: a data frame with the information 27 | """ 28 | api = "http://ipeadata2-homologa.ipea.gov.br/api/v1/Fontes" 29 | return basic_api_call(api) 30 | 31 | def get_metadata(serie=None): 32 | """ 33 | Return metadata of a serie 34 | :param serie: serie to search for otherwise return metadata for all series 35 | :return: a data frame 36 | """ 37 | url_final = "('%s')" % serie if serie is not None else '' 38 | api = "http://ipeadata2-homologa.ipea.gov.br/api/v1/Metadados%s" % url_final 39 | return basic_api_call(api) 40 | 41 | 42 | def get_nivel_region(serie): 43 | """ 44 | Return region nivel of a serie 45 | :param serie: serie to search for 46 | :return: a data frame 47 | """ 48 | api = ("http://ipeadata2-homologa.ipea.gov.br/api/v1/Metadados('{}')" 49 | "/Valores?$apply=groupby((NIVNOME))&$orderby=NIVNOME").format(serie) 50 | return basic_api_call(api) 51 | 52 | # pylint: disable=invalid-name 53 | def ipeadata(serie, groupby=None): 54 | """ 55 | Return the values from a given serie 56 | :param serie: a serie to search for 57 | :return: a data frame with the values 58 | """ 59 | if groupby is not None: 60 | df = get_nivel_region(serie) 61 | if df['NIVNOME'].isin([groupby]).any(): 62 | api = ("http://ipeadata2-homologa.ipea.gov.br/api/v1/AnoValors" 63 | "(SERCODIGO='{}',NIVNOME='{}')?$top=100&$skip=0&$orderby" 64 | "=SERATUALIZACAO&$count=true").format(serie, groupby) 65 | return basic_api_call(api) 66 | return None 67 | api = "http://ipeadata2-homologa.ipea.gov.br/api/v1/ValoresSerie(SERCODIGO='%s')" % serie 68 | return basic_api_call(api) 69 | 70 | 71 | if __name__ == "__main__": 72 | print(ipeadata('ADMIS')) 73 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | astroid==2.0.4 2 | certifi==2018.8.13 3 | chardet==3.0.4 4 | colorama==0.3.9 5 | coverage==4.5.1 6 | idna==2.7 7 | isort==4.3.4 8 | lazy-object-proxy==1.3.1 9 | mccabe==0.6.1 10 | numpy==1.15.0 11 | pandas==0.23.4 12 | pylint==2.1.1 13 | python-dateutil==2.7.3 14 | pytz==2018.5 15 | requests==2.19.1 16 | six==1.11.0 17 | typed-ast==1.1.0 18 | urllib3==1.23 19 | wincertstore==0.2 20 | wrapt==1.10.11 21 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup, find_packages 2 | from os import path, listdir 3 | 4 | here = path.abspath(path.dirname(__file__)) 5 | with open(path.join(here, 'README.md')) as f: 6 | long_description = f.read() 7 | 8 | setup( 9 | name='ipeaData', 10 | version='0.0.4', 11 | packages=find_packages(exclude=["ipedata.tests"]), 12 | url='https://github.com/ipea/pyIpeaData', 13 | author='Gustavo Coelho', 14 | author_email='gutorc@hotmail.com', 15 | license='Creative Commons Attribution-Noncommercial-Share Alike license', 16 | long_description=long_description, 17 | long_description_content_type="text/markdown", 18 | keywords='ipea', 19 | install_requires=['requests', 'pandas'], 20 | include_package_data=True, 21 | classifiers=[ 22 | # How mature is this project? Common values are 23 | # 3 - Alpha 24 | # 4 - Beta 25 | # 5 - Production/Stable 26 | 'Development Status :: 4 - Beta', 27 | 28 | # Indicate who your project is intended for 29 | 'Intended Audience :: Developers', 30 | 'Topic :: Internet :: WWW/HTTP', 31 | 32 | # Specify the Python versions you support here. In particular, ensure 33 | # that you indicate whether you support Python 2, Python 3 or both. 34 | 'Programming Language :: Python :: 3', 35 | 'Programming Language :: Python :: 3.3', 36 | 'Programming Language :: Python :: 3.4', 37 | 'Programming Language :: Python :: 3.5', 38 | ] 39 | ) 40 | -------------------------------------------------------------------------------- /test/test_api.py: -------------------------------------------------------------------------------- 1 | from ipeaData.ipeadata import * 2 | import unittest 3 | 4 | class TestApi(unittest.TestCase): 5 | 6 | def test_ipeadata(self): 7 | self.assertIsNotNone(ipeadata('ADMIS')) 8 | 9 | def test_ipeadata_groupby(self): 10 | df = ipeadata('COMP', 'Estados') 11 | self.assertIsNotNone(df) 12 | self.assertEqual(27, df.shape[0]) 13 | df = ipeadata('COMP', 'nadanan') 14 | self.assertIsNone(df) 15 | 16 | def test_get_sources(self): 17 | self.assertIsNotNone(get_sources()) 18 | 19 | def test_get_metadata(self): 20 | self.assertIsNotNone(get_metadata('ADMIS')) 21 | self.assertIsNotNone(get_metadata()) 22 | 23 | def test_get_region(self): 24 | df = get_nivel_region('QUANTLEITE') 25 | self.assertIsNotNone(df) 26 | self.assertEqual(13, df.shape[0]) 27 | 28 | if __name__ == '__main__': 29 | unittest.main(verbosity=3) --------------------------------------------------------------------------------