forked from fintech-hub/bancocentralbrasil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bancocentral.py
139 lines (111 loc) · 5.86 KB
/
bancocentral.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/python
# -*- coding: utf-8 -*-
import requests
import re
class BancoCentralException(BaseException):
pass
class AcessarBancoCentral:
def __init__(self, url):
self.url = url
def getURL(self):
headers = {
'Host': 'conteudo.bcb.gov.br',
'Connection': 'keep-alive',
'Cache-Control': 'max-age=0',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
'DNT': '1',
'Content-Type': 'application/atom+xml',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7,mt;q=0.6'
}
# Server very unstable. Test 10x http 200 response
for index in range(10):
try:
request = requests.get(self.url, headers=headers, timeout=None)
if request.status_code == 200:
return request
elif request.status_code != 200:
continue
except requests.ConnectionError:
continue
def cleanContent(content):
fix = {'<': '<', '>': '>'}
for key, value in fix.items():
content = content.replace(key, value)
content = content.replace('\r\n', '')
content = content.replace('/> <content', '/> <content')
return content
class Inflacao:
def __init__(self):
self.query_url = "https://conteudo.bcb.gov.br/api/feed/pt-br/PAINEL_INDICADORES/inflacao"
acesso = AcessarBancoCentral(self.query_url)
self.req = acesso.getURL()
def get_meta_tax(self):
inflacao = cleanContent(self.req.content.decode('utf-8'))
tax = re.search('<div id=label>Meta</div><div id=rate><div id=value>(\d*\,?\d+)</div>', inflacao).group(1)
return float(tax.replace(',','.'))
def get_acumulada_tax(self):
inflacao = cleanContent(self.req.content.decode('utf-8'))
tax = re.search('<div id=label>Acumulada</div><div id=rate><div id=value>(\d*\,?\d+)</div>', inflacao).group(1)
return float(tax.replace(',','.'))
class Poupanca:
def __init__(self):
self.query_url = "https://conteudo.bcb.gov.br/api/feed/pt-br/PAINEL_INDICADORES/poupanca"
acesso = AcessarBancoCentral(self.query_url)
self.req = acesso.getURL()
def get_poupanca_tax(self):
poupanca = cleanContent(self.req.content.decode('utf-8'))
tax = re.search('<div id=value>(\d*\,?\d+)</div>', poupanca).group(1)
return float(tax.replace(',','.'))
class Cambio:
def __init__(self):
self.query_url = "https://conteudo.bcb.gov.br/api/feed/pt-br/PAINEL_INDICADORES/cambio"
acesso = AcessarBancoCentral(self.query_url)
self.req = acesso.getURL()
self.cambio = cleanContent(self.req.content.decode('utf-8'))
def get_dolar_compra_ptax(self):
data = re.search('INDICADOR_CAMBIO_DOLAR_PTAX" />(.*)</entry>', self.cambio).group(1)
compra = re.search('<div id=rate><div id=label>Compra</div><div id=value>(\d*\,?\d+)</div>', data).group(1)
return float(compra.replace(',','.'))
def get_dolar_venda_ptax(self):
data = re.search('INDICADOR_CAMBIO_DOLAR_PTAX" />(.*)</entry>', self.cambio).group(1)
venda = re.search('<div id=rate><div id=label>Venda</div><div id=value>(\d*\,?\d+)</div>', data).group(1)
return float(venda.replace(',','.'))
def get_dolar_compra(self):
data = re.search('INDICADOR_CAMBIO_DOLAR" />(.*)</entry>', self.cambio).group(1)
compra = re.search('<div id=rate><div id=label>Compra</div><div id=value>(\d*\,?\d+)</div>', data).group(1)
return float(compra.replace(',','.'))
def get_dolar_venda(self):
data = re.search('INDICADOR_CAMBIO_DOLAR" />(.*)</entry>', self.cambio).group(1)
venda = re.search('<div id=rate><div id=label>Venda</div><div id=value>(\d*\,?\d+)</div>', data).group(1)
return float(venda.replace(',','.'))
def get_euro_compra_ptax(self):
data = re.search('INDICADOR_CAMBIO_EURO_PTAX" />(.*)</entry>', self.cambio).group(1)
compra = re.search('<div id=rate><div id=label>Compra</div><div id=value>(\d*\,?\d+)</div>', data).group(1)
return float(compra.replace(',','.'))
def get_euro_venda_ptax(self):
data = re.search('INDICADOR_CAMBIO_EURO_PTAX" />(.*)</entry>', self.cambio).group(1)
venda = re.search('<div id=rate><div id=label>Venda</div><div id=value>(\d*\,?\d+)</div>', data).group(1)
return float(venda.replace(',','.'))
def get_euro_compra(self):
data = re.search('INDICADOR_CAMBIO_EURO" />(.*)</entry>', self.cambio).group(1)
compra = re.search('<div id=rate><div id=label>Compra</div><div id=value>(\d*\,?\d+)</div>', data).group(1)
return float(compra.replace(',','.'))
def get_euro_venda(self):
data = re.search('INDICADOR_CAMBIO_EURO" />(.*)</entry>', self.cambio).group(1)
venda = re.search('<div id=rate><div id=label>Venda</div><div id=value>(\d*\,?\d+)</div>', data).group(1)
return float(venda.replace(',','.'))
class Selic:
def __init__(self):
self.query_url = "https://conteudo.bcb.gov.br/api/feed/pt-br/PAINEL_INDICADORES/juros"
acesso = AcessarBancoCentral(self.query_url)
self.req = acesso.getURL()
def get_selic_meta(self):
selic = cleanContent(self.req.content.decode('utf-8'))
selic_meta = re.search('<div id=ratevalue>(\d*\,?\d+)</div>', selic).group(1)
return float(selic_meta.replace(',','.'))
def get_selic_real(self):
selic = cleanContent(self.req.content.decode('utf-8'))
selic_real = re.search('<div id=dailyratevalue>(\d*\,?\d+)</div>', selic).group(1)
return float(selic_real.replace(',','.'))