-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
303 lines (240 loc) · 10.7 KB
/
main.py
File metadata and controls
303 lines (240 loc) · 10.7 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import logging
import sys
import time
import traceback
from types import NoneType
from selenium import webdriver
from selenium.common import NoSuchElementException, StaleElementReferenceException, TimeoutException
from selenium.webdriver import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
import keyring
import pandas as pd
from datetime import datetime
import os
import json
import gspread
from google.oauth2.service_account import Credentials
now = datetime.now()
pd.reset_option('max_columns')
pd.set_option('display.max_columns', 10)
log_filename = 'session_log_' + now.strftime("%Y%m%d%H%M%S") + '.csv'
# Edge driver
from selenium.webdriver.edge.service import Service
service_obj = Service("/Users/YN557KV/AppData/Local/Programs/MSEdge Webdriver/msedgedriver")
driver = webdriver.Edge(service=service_obj)
# Chrome driver
# from selenium.webdriver.chrome.service import Service
# service_obj = Service("C:/_Dev/chrome-webdriver/chromedriver.exe")
# driver = webdriver.Chrome(service=service_obj)
# Credentials
servicename = "Login"
username = "31992.155136"
agencia = username.split(".")[0]
conta = username.split(".")[1]
if keyring.get_password(servicename, username):
password = keyring.get_password(servicename, username)
# Constants
TODAY = datetime.strftime(datetime.today(), "%d/%m/%Y")
SEARCH_STRING = "cartões - fatura - extrato"
# 3 seconds is max timeout
driver.implicitly_wait(3)
# explicit 10s wait
wait_10s = WebDriverWait(driver, 10)
driver.delete_all_cookies()
# Functions
def sendKeysTo(xpath, text):
driver.find_element(By.XPATH, xpath).send_keys(text)
def navigateToByClick(xpath):
driver.find_element(By.XPATH, xpath).click()
def readElement(xpath):
data = driver.find_element(By.XPATH, xpath).get_attribute("innerHTML")
return data
def readXpaths(filepath):
# Opening JSON file
f = open(filepath)
# returns JSON object as
# a dictionary
data = json.load(f)
# Closing file
f.close()
return data[0]
def loginBB():
print("Opening Banco do Brasil...")
driver.get("https://www.bb.com.br/site/")
driver.maximize_window()
print("Opened!")
navigateToByClick("//*[@id='header']/header/bb-navbar-header/nav/div[2]/div[2]/bb-navbar-dropdown/button")
# driver.find_element(By.XPATH, "//*[@id='header']/header/bb-navbar-header/nav/div[2]/div[2]/bb-navbar-dropdown/button").click()
navigateToByClick("//*[@id='cdk-overlay-0']/bb-dropdown-menu/bb-menu/ul/li[1]/a")
# driver.find_element(By.XPATH, "//*[@id='cdk-overlay-0']/bb-dropdown-menu/bb-menu/ul/li[1]/a").click()
sendKeysTo("//*[@id='dependenciaOrigem']", agencia)
# driver.find_element(By.XPATH, "//*[@id='dependenciaOrigem']").send_keys(agencia)
sendKeysTo("//*[@id='numeroContratoOrigem']", conta)
# driver.find_element(By.XPATH, "//*[@id='numeroContratoOrigem']").send_keys(conta)
navigateToByClick("//*[@id='botaoEnviar']")
# driver.find_element(By.XPATH, "//*[@id='botaoEnviar']").click()
sendKeysTo("//*[@id='senhaConta']", password)
# driver.find_element(By.XPATH, "//*[@id='senhaConta']").send_keys(password)
navigateToByClick("//*[@id='botaoEnviar']")
# driver.find_element(By.XPATH, "//*[@id='botaoEnviar']").click()
print("Username and password sent. Logging in...")
# driver.find_element(By.CLASS_NAME, "app-header__logo-container")
print("Logged!")
def accessMultiStatement(paths):
# wait_10s.until(expected_conditions.presence_of_element_located((By.XPATH,xpaths['Minhas financas'])))
time.sleep(5)
navigateToByClick(paths['Minhas financas'])
navigateToByClick(paths['Extrato Multibanco'])
navigateToByClick(paths['Checkbox Caixa'])
obj = driver.find_element(By.XPATH, paths['Titulo Extrato Multibanco'])
if obj:
return True
else:
return False
def readTransactions(paths):
df = pd.DataFrame(
columns=["ID", "DescricaoBanco", "ComplementoDescricao", "Valor", "DataCompra", "DataLancamento", "Banco",
"TipoLancamento", "Parcela", "Parcelas", "Categoria", "Subcategoria"])
day = 1
transaction = 1
while True:
try:
diaMes = readElement(paths['Div dia'].replace('{n}', str(day)))
except:
# Fim da Pagina
transaction = 1
day = 1
break
try:
descXpath = paths['Descricao lancamento']
descXpath = descXpath.replace('{d}', str(day))
descXpath = descXpath.replace('{t}', str(transaction))
descricao = readElement(descXpath)
except:
day += 1
transaction = 1
continue
valorXpath = paths['Valor lancamento']
valorXpath = valorXpath.replace('{d}', str(day))
valorXpath = valorXpath.replace('{t}', str(transaction))
valor = readElement(valorXpath)
naviXpath = paths['Link detalhes lancamento']
naviXpath = naviXpath.replace('{d}', str(day))
naviXpath = naviXpath.replace('{t}', str(transaction))
navigateToByClick(naviXpath)
complementoDesc = readElement(paths['Detalhes - comp.desc'])
complementoDesc = complementoDesc.strip()
dataCompra = readElement(paths['Detalhes - hora'])
if '·' in dataCompra:
dataCompra = dataCompra.replace(' · ', ' ')
dataCompra = datetime.strptime(dataCompra, "%d/%m/%Y %H:%M:%S")
else:
dataCompra = datetime.strptime(dataCompra + " 00:00:00", "%d/%m/%Y %H:%M:%S")
categoria = readElement(paths['Detalhes - categoria'])
subcategoria = readElement(paths['Detalhes - subcategoria'])
banco = readElement(paths['Detalhes - banco'])
tipo = readElement(paths['Detalhes - TipoLancamento'])
if tipo == 'CONTA':
tipo = 'Debito'
elif tipo == 'CARTÃO':
tipo = 'Credito'
else:
tipo = 'Debito'
navigateToByClick(paths['Detalhes - voltar'])
row_id = descricao.replace(' ', '') + "_" + valor.replace(',', '') + "_" + datetime.strftime(dataCompra,
"%Y%m%d%H%M%S")
new_row = pd.DataFrame({'ID': row_id,
'DescricaoBanco': descricao,
'ComplementoDescricao': complementoDesc,
'Valor': valor,
'DataCompra': dataCompra,
'DataLancamento': dataCompra,
'Banco': banco,
'TipoLancamento': tipo,
'Parcela': "0",
'Parcelas': "0",
'Categoria': categoria,
'Subcategoria': subcategoria
}, index=[0])
df = pd.concat([df, new_row])
print("Row added: "+ diaMes + " | " + new_row.loc[0, 'ID'] + " | " + banco + " | " + tipo)
# print("diaMes= "+diaMes+"\ndescricao= "+descricao+"\nvalor: "+valor+"\ndataCompra= "+dataCompra+"\ncategoria= "+categoria+"\nsubcategoria= "+subcategoria+"\nbanco= "+banco)
# print("\n\n")
transaction += 1
# return df.reset_index(drop=True)
return df.set_index('ID')
# Drive Functions
def readBase():
gc = gspread.service_account(filename="C:\_Dev\BB\service_account.json")
sh = gc.open("Carteira RPA")
worksheet = sh.worksheet("BaseHistorica")
base_dict = worksheet.get_all_records()
return pd.DataFrame.from_dict(base_dict)
def driveAuthorize():
scopes = [
'https://www.googleapis.com/auth/spreadsheets',
'https://www.googleapis.com/auth/drive'
]
credentials = Credentials.from_service_account_file(
"C:\_Dev\BB\service_account.json",
scopes=scopes
)
gspread.authorize(credentials)
def getDiffRows(df_base, df_trans):
dfNewRows = df_trans[ ~df_trans.index.isin(df_base.index) ]
return dfNewRows.reset_index()
def appendNewRows(df_toappend):
gc = gspread.service_account(filename="C:\_Dev\BB\service_account.json")
sh = gc.open("Carteira RPA")
worksheet = sh.worksheet("BaseHistorica")
df_toappend['DataCompra'] = df_toappend['DataCompra'].dt.strftime('%d/%m/%Y %H:%M:%S')
df_toappend['DataLancamento'] = df_toappend['DataLancamento'].dt.strftime('%d/%m/%Y')
# col_list = worksheet.col_values(1)
# empty_row = len(col_list)
# worksheet.update(range_name='A' + str(empty_row), values=df_toappend.values.tolist())
worksheet.append_rows(values=df_toappend.values.tolist(), value_input_option="USER_ENTERED", insert_data_option='OVERWRITE', table_range='A1', include_values_in_response=True)
def sortBase():
gc = gspread.service_account(filename="C:\_Dev\BB\service_account.json")
sh = gc.open("Carteira RPA")
worksheet = sh.worksheet("BaseHistorica")
worksheet.sort((5,'asc'), range='A2:L1000')
def formatBase():
gc = gspread.service_account(filename="C:\_Dev\BB\service_account.json")
sh = gc.open("Carteira RPA")
worksheet = sh.worksheet("BaseHistorica")
worksheet.format("D:D", {"numberFormat": {"type": "CURRENCY"}})
worksheet.format("E:E", {"numberFormat": {"type": "DATE_TIME"}})
worksheet.format("F:F", {"numberFormat": {"type": "DATE", "pattern": "dd/MM/yyyy"}})
# Actions
if __name__ == '__main__':
xpathsFilepath = "C:\_Dev\BB\docs\BBPortalXpaths.json"
xpaths = readXpaths(xpathsFilepath)
driveAuthorize()
dfBase = readBase()
loginBB()
accessMultiStatement(xpaths)
dfTransactions = readTransactions(xpaths)
dfNewRows = getDiffRows(dfBase, dfTransactions)
appendNewRows(dfNewRows)
sortBase()
"""
driver.find_element(By.XPATH, "//*[@id='acheFacil']").send_keys(SEARCH_STRING)
time.sleep(1)
driver.find_element(By.XPATH, "//*[@id='listaAcheFacil']/ul/li/a").click()
time.sleep(1)
wait_10s.until(expected_conditions.presence_of_element_located((By.XPATH, "//*[@id='carousel-cartoes']/img[1]")))
driver.find_element(By.XPATH, "//*[@id='carousel-cartoes']/img[1]").click()
time.sleep(1)
wait_10s.until(expected_conditions.presence_of_element_located((By.XPATH, "//*[@id='faturasAtual']/li[12]/a")))
driver.find_element(By.XPATH, "//*[@id='faturasAtual']/li[12]/a").click()
time.sleep(1)
driver.find_element(By.XPATH, "//*[@id='toolbarCartao']/li[5]/a").click()
time.sleep(1)
driver.find_element(By.XPATH, "/html/body/div[4]/div[2]/ul/li[2]/a").click()
time.sleep(1)
driver.find_element(By.XPATH, "/html/body/div[2]/div/div[1]/ul/li/ul/li[3]/ul/li[4]/a").click()
time.sleep(1)
driver.close()
"""