-
Notifications
You must be signed in to change notification settings - Fork 0
/
cei_client.py
179 lines (163 loc) · 6.23 KB
/
cei_client.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import requests
import ssl
import pandas as pd
import string
from requests.packages.urllib3.exceptions import InsecureRequestWarning
from bs4 import BeautifulSoup
from dataclasses import dataclass
from werkzeug.utils import cached_property
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
ssl._create_default_https_context = ssl._create_unverified_context
@dataclass
class Broker():
value: str
date: str
class CEIHttpClient():
IS_LOGGED = False
SESSION = None
LOGIN_URL = 'https://ceiapp.b3.com.br/CEI_Responsivo/./login.aspx'
PAYMENTS_URL = (
'https://ceiapp.b3.com.br/CEI_Responsivo/ConsultarProventos.aspx'
)
session = requests.Session()
def __init__(self, username, password):
self.username = username
self.password = password
def login(self):
response = self.session.get(
self.LOGIN_URL,
verify=False
)
login_page_content = response.content
login_page_parsed = BeautifulSoup(
login_page_content,
"html.parser"
)
view_state = login_page_parsed.find(id='__VIEWSTATE')['value']
viewstate_generator = login_page_parsed.find(
id='__VIEWSTATEGENERATOR'
)['value']
event_validation = login_page_parsed.find(
id='__EVENTVALIDATION'
)['value']
payload = {
'ctl00$ContentPlaceHolder1$smLoad': (
'ctl00$ContentPlaceHolder1$UpdatePanel1|'
'ctl00$ContentPlaceHolder1$btnLogar'
),
'__EVENTTARGET': '',
'__EVENTARGUMENT': '',
'__VIEWSTATEGENERATOR': viewstate_generator,
'__EVENTVALIDATION': event_validation,
'__VIEWSTATE': view_state,
'ctl00$ContentPlaceHolder1$txtLogin': self.username,
'ctl00$ContentPlaceHolder1$txtSenha': self.password,
'__ASYNCPOST': True,
'g-recaptcha-response': None,
'ctl00$ContentPlaceHolder1$btnLogar': 'Entrar'
}
headers = {
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Referer': 'https://cei.b3.com.br/CEI_Responsivo/login.aspx',
'Origin': 'https://cei.b3.com.br',
'Host': 'cei.b3.com.br',
'User-Agent': (
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) '
'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 '
'Safari/537.36'
),
}
response = self.session.post(
self.LOGIN_URL,
data=payload,
headers=headers
)
self.session.headers.update(headers)
if response.status_code == 200:
self.IS_LOGGED = True
@cached_property
def payments_page(self):
if not self.IS_LOGGED:
self.login()
response = self.session.get(
self.PAYMENTS_URL,
verify=False
)
return response.content
def get_brokers(self):
payments_page = self.payments_page
id = 'ctl00_ContentPlaceHolder1_ddlAgentes'
soup = BeautifulSoup(payments_page, 'html.parser')
brokers_select = soup.find('select', id=id)
brokers_option = brokers_select.find_all('option')
date = soup.find(
id='ctl00_ContentPlaceHolder1_txtData'
)['value']
return [
Broker(
value=broker_option['value'],
date=date
)
for broker_option in brokers_option
if broker_option['value'] != -1
]
def get_owned_assets(self):
brokers = self.get_brokers()
payments_page = self.payments_page
owned_assets = []
for broker in brokers:
payments_page_parsed = BeautifulSoup(
payments_page,
"html.parser"
)
view_state = payments_page_parsed.find(id='__VIEWSTATE')['value']
viewstate_generator = payments_page_parsed.find(
id='__VIEWSTATEGENERATOR'
)['value']
event_validation = payments_page_parsed.find(
id='__EVENTVALIDATION'
)['value']
payload = {
'ctl00$ContentPlaceHolder1$ToolkitScriptManager1:': (
'ctl00$ContentPlaceHolder1$updFiltro|'
'ctl00$ContentPlaceHolder1$btnConsultar'
),
(
'ctl00_ContentPlaceHolder1_'
'ToolkitScriptManager1_HiddenField'
): '',
'__EVENTTARGET': '',
'__EVENTARGUMENT': '',
'__VIEWSTATE': view_state,
'__VIEWSTATEGENERATOR': viewstate_generator,
'__EVENTVALIDATION': event_validation,
'ctl00$ContentPlaceHolder1$ddlAgentes': broker.value,
'ctl00$ContentPlaceHolder1$ddlContas': 0,
'ctl00$ContentPlaceHolder1$txtData': broker.date,
'__ASYNCPOST': True,
'ctl00$ContentPlaceHolder1$btnConsultar': 'Consultar'
}
consult_response = self.session.post(
self.PAYMENTS_URL,
data=payload,
verify=False
)
consult_page = consult_response.content
consult_page_parsed = BeautifulSoup(
consult_page,
'lxml'
)
div = consult_page_parsed.find(
id='ctl00_ContentPlaceHolder1_updFiltro'
)
payments_table = div.find_all("table")[0]
payments_df = pd.read_html(str(payments_table))[0]
payments_df.drop(
payments_df.tail(1).index,
inplace=True
)
codes = payments_df['Cód. Negociação']
codes = codes.str.rstrip(string.digits)
codes = list(set(codes))
owned_assets += codes
return owned_assets