{ "cells": [ { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "insert_query = \"INSERT IGNORE INTO default.TRANSACTION (ID, TDATE, ACCOUNT_ID, MEMO, COUNTRY, OUTFLOW, INFLOW, OWNER_ID, INSTALLMENT_NR, INSTALLMENT_TT, CREATED, UPDATED) VALUES ( %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s )\"" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def find_owner(queried_name: str):\n", " from mysql.connector import connect, Error\n", "\n", " query = \"SELECT * FROM OWNER\"\n", " result = []\n", "\n", " try:\n", " with connect(\n", " host=\"127.0.0.1\",\n", " user=\"root\",\n", " password=\"pleasehashapasswordomg\",\n", " database=\"default\",\n", " ) as connection:\n", " # print(\"CONNECTED!\", connection)\n", " with connection.cursor() as cursor:\n", " cursor.execute(query)\n", " query_result = cursor.fetchall()\n", "\n", " for item in query_result:\n", " if item[1].lower() == queried_name:\n", " result.append(item[0])\n", " result.append(item[1])\n", " # print(result)\n", " # print(\"DONE!\")\n", " except Error as e:\n", " print(e)\n", " finally:\n", " connection.close()\n", "\n", " return result if result else None\n", " \n", "find_owner(\"daniel\")[0]" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "def create_lists(file_name):\n", " import re\n", "\n", " # Open the text file\n", " with open(file_name, \"r\", encoding=\"latin\") as file:\n", " # Read the contents of the file\n", " contents = file.readlines()\n", "\n", " # Define the regex patterns\n", " owner_pattern = r\"\\s\\d\\s?-\\s?([A-Z]+)\"\n", " line_pattern = r\"\\d{2}\\.\\d{2}\\.\\d{4}.{23}.{14}.{2}\\s*-?\\d*\\.?\\d+,\\d{2}\\s*\\d+,\\d{2}\"\n", " payment_pattern = (r\"\\d{2}\\.\\d{2}\\.\\d{4}PGTO.*200211(\\s*-?\\d*\\.?\\d+,\\d{2})(\\s*\\d+,\\d{2})\")\n", " partial_invoice_line_pattern = r\"\\d{2}\\/\\d{2}.{27}.{16}.{2}\\s+\\s*-?\\d*\\.?\\d+,\\d{2}\\s*\\d+,\\d{2}\"\n", "\n", " # Lists\n", " current_list = None\n", " owner_list = []\n", " result = {}\n", "\n", " # silly_counter = 1\n", " isPartial = True\n", "\n", " # Find Owners\n", " try:\n", " for line in contents:\n", " line = line.strip()\n", "\n", " found_owners = re.findall(owner_pattern, line)\n", " if found_owners:\n", " for owner_name in found_owners:\n", " print(owner_name)\n", " list_name = f\"list_{owner_name.lower()}\"\n", " owner_list.append(list_name)\n", " result[list_name] = {}\n", " result[list_name][\"owner_name\"] = owner_name\n", " result[list_name][\"owner_id\"] = find_owner(owner_name.lower())[0]\n", " except:\n", " print(\"Error during owner search\")\n", "\n", "\n", " # Treat and create transaction lists\n", " try:\n", " for line in contents:\n", " line = line.strip()\n", "\n", " if re.match(owner_pattern, line):\n", " found_owner = re.match(owner_pattern, line)\n", " owner_list = f\"list_{found_owner.group(1).lower()}\"\n", " current_list = owner_list\n", " result[current_list][\"tlist\"] = []\n", " else:\n", " if re.match(payment_pattern, line):\n", " result[current_list][\"tlist\"].append(line)\n", " elif re.match(line_pattern, line) or re.match(partial_invoice_line_pattern, line):\n", " result[current_list][\"tlist\"].append(line)\n", " except:\n", " print(\"Error during Transaction Lists creation\")\n", "\n", " # Check file pattern\n", " sample = result[current_list][\"tlist\"][0]\n", " if re.match(line_pattern, sample):\n", " isPartial = False\n", "\n", " for listObj in result:\n", " result[listObj][\"isPartial\"] = isPartial\n", "\n", " return result" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['\\n', '\\n', '\\n', '\\n', '\\n', '\\n', '\\n', '\\n', '\\n', '\\n', '\\n', '\\n', '\\n', '\\n', '\\n', '\\n', '\\n', '\\n', '\\n', ' SISBB - Sistema de Informações Banco do Brasil \\n', '13/11/2024 Auto-Atendimento 08:44:59\\n', ' Fatura do Cartão de Crédito \\n', ' \\n', ' \\n', 'Cliente : DANIEL O CARVALHO \\n', 'Nr.Cartão : 4984.****.****.5727 \\n', 'Modalidade : OUROCARD VISA INFINITE \\n', ' \\n', 'Vencimento : 25.04.2024 \\n', 'Total da fatura : R$ 18.362,96 \\n', 'Pagamento mínimo: R$ 2.754,44 \\n', ' \\n', 'DEMONSTRATIVO\\n', '--------------------------------------------------------------------------------\\n', 'Data Transações País Valor R$ Valor US$\\n', '--------------------------------------------------------------------------------\\n', ' \\n', ' \\n', ' 1 - DANIEL O CARVALHO \\n', ' \\n', ' SALDO FATURA ANTERIOR BR 19.249,92 0,00\\n', ' \\n', ' \\n', ' Pagamentos/Créditos \\n', '25.03.2024PGTO DEBITO CONTA 8611 000006025 200211 -19.249,92 0,00\\n', ' \\n', ' \\n', ' Lazer \\n', '14.03.2024EBN*XSOLLA CURITIBA BR 27,09 0,00\\n', '06.04.2024PAG*MmCultura BRASILIA BR 26,00 0,00\\n', ' \\n', ' \\n', ' Restaurantes \\n', '16.03.2024IFD*MARIANA PERDOMO CONBRASILIA BR 80,79 0,00\\n', '16.03.2024IFD*iFood OSASCO BR 5,00 0,00\\n', '17.03.2024IFD*TT BRASILIA COMERCIBRASILIA BR 134,00 0,00\\n', '18.03.2024IFD*IFOOD.COM AGENCIA DOsasco BR 12,90 0,00\\n', '19.03.2024CASA ALMERIA BRASILIA BR 157,92 0,00\\n', '19.03.2024CASA ALMERIA BRASILIA BR 105,00 0,00\\n', '19.03.2024IFD*TAIKAN FAST SUSHI LBRASILIA BR 149,90 0,00\\n', '19.03.2024IFD*iFood OSASCO BR 10,00 0,00\\n', '28.03.2024FOGO E BRASA STEAK HOUSPIRENOPOLIS BR 238,00 0,00\\n', '03.04.2024ifood *IFD*RC MELO C Vila Yara Osa BR 226,51 0,00\\n', '04.04.2024IFD*TAIKAN FAST SUSHI LBRASILIA BR 149,90 0,00\\n', '06.04.2024BAR E RESTAURANTE TIA BRASILIA BR 220,00 0,00\\n', '06.04.2024BAR E RESTAURANTE TIA BRASILIA BR 4,00 0,00\\n', '06.04.2024BAR E RESTAURANTE TIA BRASILIA BR 16,00 0,00\\n', '06.04.2024BAR E RESTAURANTE TIA BRASILIA BR 12,00 0,00\\n', '06.04.2024IFD*ME COMERCIO DE ALIMBRASILIA BR 66,90 0,00\\n', '07.04.2024IFD*UPTOWN BURGERS E SHBRASILIA BR 76,70 0,00\\n', ' \\n', ' \\n', ' Saúde \\n', '20.03.2024DROGASIL 2067 BRASILIA BR 516,80 0,00\\n', '20.03.2024REDE BRASIL DRUGSTORE BRASILIA BR 512,73 0,00\\n', '20.03.2024PAGUE MENOS 1225 BRASILIA BR 568,23 0,00\\n', '05.04.2024DROGARIA SAO PAULO BRASILIA BR 248,37 0,00\\n', ' \\n', ' \\n', ' Serviços \\n', '15.03.2024VELOE BARUERI BR 22,26 0,00\\n', '18.03.2024NETFLIX.COM SAO PAULO BR 39,90 0,00\\n', '19.03.2024PAG*FolhaDeSPaulo SAO PAULO BR 29,90 0,00\\n', '23.03.2024PAG*Zig SAO PAULO BR 376,01 0,00\\n', '27.03.2024Gympass GympassBr Sao Paulo BR 399,90 0,00\\n', '29.03.2024MP *MELIMAIS OSASCO BR 17,99 0,00\\n', '01.04.2024MERCADOLIVRE*AGUSHOPCOMOSASCO BR 39,90 0,00\\n', '01.04.2024MERCADOLIVRE*GIGATUDOBYOSASCO BR 418,41 0,00\\n', '02.04.2024MERCADOLIVRE*BRASIL OSASCO BR 58,57 0,00\\n', '03.04.2024MERCADOLIVRE*AGUSHOPCOMOSASCO BR 31,90 0,00\\n', '04.04.2024IFD*Gleidson Renato LeiOsasco BR 10,00 0,00\\n', '04.04.2024APPLE.COM/BILL SAO PAULO BR 54,90 0,00\\n', '05.04.2024MERCADOLIVRE*WIXACESSOROSASCO BR 58,57 0,00\\n', '04.04.2024IFD*LOG CITY ESPRESS Osasco BR 5,00 0,00\\n', '05.04.2024Gympass GympassBr Sao Paulo BR 399,90 0,00\\n', '05.04.2024MR JOHN BARBEARIA LTDA BRASILIA BR 60,00 0,00\\n', '09.04.2024MERCADOLIVRE*SABORESDAMOSASCO BR 109,90 0,00\\n', ' \\n', ' \\n', ' Supermercados \\n', '24.03.2024PAG*GaleteriaSerrana BRASILIA BR 164,78 0,00\\n', ' \\n', ' \\n', ' Transporte \\n', '18.03.2024UBER* TRIP OSASCO BR 13,98 0,00\\n', '18.03.2024UBER *TRIP HELP.UBER.COSAO PAULO BR 1,60 0,00\\n', '18.03.2024UBER * PENDING SAO PAULO BR 13,92 0,00\\n', '18.03.2024UBER *TRIP HELP.UBER.COSAO PAULO BR 5,00 0,00\\n', '19.03.2024UBER* TRIP OSASCO BR 11,83 0,00\\n', '21.03.2024UBER * PENDING SAO PAULO BR 10,98 0,00\\n', '23.03.2024UBER * PENDING SAO PAULO BR 10,96 0,00\\n', '25.03.2024UBER* TRIP OSASCO BR 14,55 0,00\\n', '25.03.2024UBER* TRIP OSASCO BR 5,00 0,00\\n', '25.03.2024UBER *TRIP HELP.UBER.COSAO PAULO BR 14,80 0,00\\n', '25.03.2024UBER * PENDING SAO PAULO BR 13,92 0,00\\n', '25.03.2024UBER *TRIP HELP.UBER.COSAO PAULO BR 3,41 0,00\\n', '28.03.2024UBER* TRIP OSASCO BR 14,31 0,00\\n', '28.03.2024UBER* TRIP OSASCO BR 1,44 0,00\\n', '28.03.2024UBER* TRIP OSASCO BR 14,08 0,00\\n', '28.03.2024UBER* TRIP OSASCO BR 3,00 0,00\\n', '02.04.2024UBER * PENDING SAO PAULO BR 19,94 0,00\\n', '02.04.2024UBER * PENDING SAO PAULO BR 39,93 0,00\\n', '04.04.2024UBER * PENDING SAO PAULO BR 10,93 0,00\\n', '06.04.2024UBER * PENDING SAO PAULO BR 39,94 0,00\\n', '09.04.2024UBER * PENDING SAO PAULO BR 10,97 0,00\\n', ' \\n', ' \\n', ' Vestuário \\n', '27.03.2024CASA DO CHOCOLATE BRASILIA BR 283,89 0,00\\n', ' \\n', ' \\n', ' Outros lançamentos \\n', '13.03.2024DL*GOOGLE YouTub SAO PAULO BR 41,90 0,00\\n', '18.03.2024Amazon Music SAO PAULO BR 21,90 0,00\\n', '14.03.2024STEAM PURCHASE SEATTLE DE 24,00 0,00\\n', '19.03.2024IOF - COMPRA NO EXTERIOR 1,05 0,00\\n', '15.03.2024STEAM PURCHASE SEATTLE DE 47,88 0,00\\n', '19.03.2024IOF - COMPRA NO EXTERIOR 2,09 0,00\\n', '04.04.2024STEAM PURCHASE SEATTLE DE 117,98 0,00\\n', '08.04.2024IOF - COMPRA NO EXTERIOR 1,29 0,00\\n', ' \\n', ' \\n', ' Compras parceladas \\n', '15.01.2024MP*MUNDODOSCO PARC 03/10 SAO PAULO BR 159,90 0,00\\n', '17.10.2023BIANCHINI AUT PARC 06/10 BRASILIA BR 535,00 0,00\\n', '22.03.2024SNOW PARC 01/02 BELO HORIZONBR 2.466,69 0,00\\n', '26.03.2024QUEST 01/02-SNOW BELO BR -2.466,69 0,00\\n', '22.03.2024SNOW PARC 01/10 BELO HORIZONBR 493,41 0,00\\n', '26.03.2024QUEST 01/10-SNOW BELO BR -493,41 0,00\\n', ' \\n', ' \\n', ' SubTotal 7.373,90 0,00\\n', ' \\n', ' \\n', ' 4 - IZABELY C NORMANDO \\n', ' \\n', ' \\n', ' Restaurantes \\n', '18.03.2024FEDERAL GOURMET BRASILIA BR 22,56 0,00\\n', '18.03.2024FEDERAL GOURMET BRASILIA BR 18,85 0,00\\n', '23.03.2024COCO BAMBU IGUATEMI BR BRASILIA BR 250,23 0,00\\n', '30.03.2024PAG*ColoreGelateria PIRENOPOLIS BR 44,46 0,00\\n', '01.04.2024FEDERAL GOUR BRASILIA BR 18,93 0,00\\n', '01.04.2024FEDERAL GOUR BRASILIA BR 120,00 0,00\\n', '06.04.2024BAR E RESTAURANTE TIA BRASILIA BR 27,00 0,00\\n', '06.04.2024BAR E RESTAURANTE TIA BRASILIA BR 16,00 0,00\\n', '07.04.2024ifood *IFD*SN COMERC Vila Yara Osa BR 152,00 0,00\\n', ' \\n', ' \\n', ' Saúde \\n', '18.03.2024LISTO*CLINICAESTETICARNBRASILIA BR 4.300,00 0,00\\n', '28.03.2024DROGASIL 2067 BRASILIA BR 114,34 0,00\\n', '08.04.2024DROGASIL 1383 BRASILIA BR 92,18 0,00\\n', ' \\n', ' \\n', ' Serviços \\n', '13.03.2024CODA OIAPOQUE BR 110,83 0,00\\n', '14.03.2024VINHOS E CO MACAPA BR 177,92 0,00\\n', '15.03.2024RUSTIC ALIMENTOS LTDA MACAPA BR 71,50 0,00\\n', '19.03.2024PICPAY UNICOMPRASINT Brasilia BR 353,00 0,00\\n', '24.03.2024DM *SHEINCOM MIDVIEW CITY BR 162,93 0,00\\n', '26.03.2024MP*DONAFUTRICA OSASCO BR 824,50 0,00\\n', '27.03.2024CAFES PLURAIS TORRADOS BRASILIA BR 279,90 0,00\\n', '30.03.2024PAG*WeniaFerreiraDo PIRENOPOLIS BR 94,80 0,00\\n', '31.03.2024RR PIRENOPOLIS BR 240,08 0,00\\n', '10.04.2024IFD*F C BOLOS DO FLAVIOBRASILIA BR 90,00 0,00\\n', ' \\n', ' \\n', ' Supermercados \\n', '07.04.2024PAG*GaleteriaSerrana BRASILIA BR 174,10 0,00\\n', ' \\n', ' \\n', ' Vestuário \\n', '24.03.2024DL*SHEINCOM SAO PAULO BR 270,94 0,00\\n', '30.03.2024MARIA PIRENOPOLIS BR 120,00 0,00\\n', '08.04.2024TRACKEFIELD BRASILIA BR 120,10 0,00\\n', ' \\n', ' \\n', ' Viagens \\n', '15.03.2024BDH HOTELARIA MACAPA BR 72,00 0,00\\n', ' \\n', ' \\n', ' Outros lançamentos \\n', '14.03.2024LATAM SITE SAO PAULO BR 93,00 0,00\\n', ' \\n', ' \\n', ' Compras parceladas \\n', '15.01.2024LDM PARC 03/04 BRASILIA BR 1.100,00 0,00\\n', '17.01.2024BRASILIA EMPR PARC 03/12 BRASILIA BR 599,00 0,00\\n', '26.01.2024VISAO INSTITU PARC 03/05 BRASILIA BR 200,00 0,00\\n', '26.03.2024BIOEXATA FARM PARC 01/03 BRASILIA BR 583,01 0,00\\n', '07.01.2024PG *B4A GLAMB PARC 04/12 SAO PAULO BR 74,90 0,00\\n', ' \\n', ' \\n', ' SubTotal 10.989,06 0,00\\n', ' \\n', ' \\n', ' Total 18.362,96 0,00\\n', ' \\n', ' \\n', 'RESUMO EM REAL\\n', '--------------------------------------------------------------------------------\\n', ' Saldo Pagamento/ Compras/ Total Lim. extra Saldo \\n', ' Anterior Créditos Débitos R$ utilizado Atual - R$ \\n', '--------------------------------------------------------------------------------\\n', ' 19.249,92 - -22.210,02 + 21.323,06 = 18.362,96 - 0,00 = 18.362,96\\n', '--------------------------------------------------------------------------------\\n', ' \\n', 'RESUMO EM DÓLAR\\n', '--------------------------------------------------------------------------------\\n', ' Compras/ Outros Saldo Taxa de Saldo \\n', ' Saques débitos Créditos Atual U$ conversão convertido \\n', '--------------------------------------------------------------------------------\\n', ' 0,00 - 0,00 + 0,00 = 0,00 X 0,0000 = 0,00\\n', '--------------------------------------------------------------------------------\\n', ' \\n', ' \\n', 'LIMITES - R$\\n', '--------------------------------------------------------------------------------\\n', 'Total para transações à vista : 70.255,00\\n', 'Saques : 70.255,00\\n', '(Incluido no total transação à vista)\\n', 'Total para transações parceladas : 0,00\\n', 'Crediário : 249.337,00\\n', ' \\n', ' \\n', 'ENCARGOS FINANCEIROS - R$ 1 2\\n', '--------------------------------------------------------------------------------\\n', 'Crédito Rotativo : 9,94 14,16\\n', 'Crédito Parcelado: 3,00 10,21\\n', 'Permanência : 1,00 1,00\\n', 'Multa : 2,00 2,00\\n', '1. Para o periodo %am \\n', '2. Máximo próximo periodo %am \\n', ' \\n', ' \\n', 'PONTO PRA VOCÊ/LIVELO\\n', '--------------------------------------------------------------------------------\\n', ' Pontuação acumulada Até 10.04.2024 : 1.331\\n', '--------------------------------------------------------------------------------\\n', '\\n', '\\n', '\\n', '\\n', '\\n', '\\n', '\\n', '\\n', '\\n', '\\n']\n" ] }, { "ename": "TypeError", "evalue": "expected str, bytes or os.PathLike object, not TextIOWrapper", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[6], line 9\u001b[0m\n\u001b[0;32m 7\u001b[0m contents \u001b[38;5;241m=\u001b[39m file\u001b[38;5;241m.\u001b[39mreadlines()\n\u001b[0;32m 8\u001b[0m \u001b[38;5;28mprint\u001b[39m(contents)\n\u001b[1;32m----> 9\u001b[0m \u001b[43mcreate_lists\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfile\u001b[49m\u001b[43m)\u001b[49m\n", "Cell \u001b[1;32mIn[5], line 5\u001b[0m, in \u001b[0;36mcreate_lists\u001b[1;34m(file_name)\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mre\u001b[39;00m\n\u001b[0;32m 4\u001b[0m \u001b[38;5;66;03m# Open the text file\u001b[39;00m\n\u001b[1;32m----> 5\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28;43mopen\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mfile_name\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mr\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mencoding\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mlatin\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m \u001b[38;5;28;01mas\u001b[39;00m file:\n\u001b[0;32m 6\u001b[0m \u001b[38;5;66;03m# Read the contents of the file\u001b[39;00m\n\u001b[0;32m 7\u001b[0m contents \u001b[38;5;241m=\u001b[39m file\u001b[38;5;241m.\u001b[39mreadlines()\n\u001b[0;32m 9\u001b[0m \u001b[38;5;66;03m# Define the regex patterns\u001b[39;00m\n", "File \u001b[1;32m~\\AppData\\Roaming\\Python\\Python312\\site-packages\\IPython\\core\\interactiveshell.py:324\u001b[0m, in \u001b[0;36m_modified_open\u001b[1;34m(file, *args, **kwargs)\u001b[0m\n\u001b[0;32m 317\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m file \u001b[38;5;129;01min\u001b[39;00m {\u001b[38;5;241m0\u001b[39m, \u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m2\u001b[39m}:\n\u001b[0;32m 318\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[0;32m 319\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mIPython won\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mt let you open fd=\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mfile\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m by default \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 320\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mas it is likely to crash IPython. If you know what you are doing, \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 321\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124myou can use builtins\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m open.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 322\u001b[0m )\n\u001b[1;32m--> 324\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mio_open\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfile\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", "\u001b[1;31mTypeError\u001b[0m: expected str, bytes or os.PathLike object, not TextIOWrapper" ] } ], "source": [ "# import os\n", "# file = os.path.join(\"E:\\\\forge\\\\python\\\\robopato\\\\documents\", \"OUROCARD_VISA_INFINITE-Mar_24.txt\")\n", "file = \"E:\\\\forge\\\\python\\\\robopato\\\\documents\\\\OUROCARD_VISA_INFINITE-Abr_24.txt\"\n", "# Open the text file\n", "with open(file, \"r\", encoding=\"latin\") as file:\n", " # Read the contents of the file\n", " contents = file.readlines()\n", " print(contents)\n", "create_lists(file)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def build_insert(input_dict: dict, account: int):\n", " from datetime import date, datetime\n", " from decimal import Decimal\n", " import re\n", " import hashlib\n", "\n", " insert_bulk = []\n", "\n", " # RegEx Patterns\n", " line_group_pattern = r\"(?P\\d{2})\\.(?P\\d{2})\\.(?P\\d{4})(?:(?P.+PARC (?P\\d+.)\\/(?P\\d+)\\s.{12})|(?P.{37}))(?P.{2})(?P\\s*-?\\d*\\.?\\d+,\\d{2})(?P\\s*\\d*\\.?\\d+,\\d{2})\"\n", " partial_invoice_group_pattern = r\"(?P\\d{2})\\/(?P\\d{2})(?:(?P.+PARC (?P\\d{2})\\/(?P\\d{2}).{15})|(?P.{43}))(?P.{2})(?P\\s+\\s*-?\\d*\\.?\\d+,\\d{2})(?P\\s*\\d+,\\d{2})\"\n", " payment_pattern = r\"(?P\\d{2})\\.(?P\\d{2})\\.(?P\\d{4})(?PPGTO DEBITO CONTA).*200211(?P\\s*-?\\d*\\.?\\d+,\\d{2})(?P\\s*\\d+,\\d{2})\"\n", "\n", " for key in input_dict:\n", " if input_dict[key][\"isPartial\"]:\n", " pattern_to_use = partial_invoice_group_pattern\n", " else:\n", " pattern_to_use = line_group_pattern\n", "\n", " for item in input_dict[key][\"tlist\"]:\n", " # check if it is an invoice payment\n", " matches = re.match(payment_pattern, item)\n", " if matches:\n", " tTdate = str(\n", " date(\n", " int(matches.group(\"year\")),\n", " int(matches.group(\"month\")),\n", " int(matches.group(\"day\")),\n", " )\n", " )\n", " tAccount = account\n", " tMemo = matches.group(\"memo\")\n", " tCountry = None\n", " tOutflow = \"0.00\"\n", " tInflow = matches.group(\"inflow\").strip().replace(\".\", \"\").replace(\",\", \".\").replace(\"-\", \"\")\n", " tOwner = input_dict[key][\"owner_id\"]\n", " tInstallmentNr = None\n", " tInstallmentTt = None\n", " tCreated = str(datetime.now(tz=None))\n", " tUpdated = None\n", " else:\n", " matches = re.match(pattern_to_use, item)\n", " tTdate = str(\n", " date(\n", " # partial files will not have the year data on transactions\n", " int(matches.group(\"year\")) if pattern_to_use == line_group_pattern else datetime.now().year,\n", " int(matches.group(\"month\")),\n", " int(matches.group(\"day\")),\n", " )\n", " )\n", " \n", " tAccount = account\n", "\n", " tMemo = matches.group(\"p_memo\") if matches.group(\"p_memo\") else matches.group(\"memo\")\n", " tInstallmentNr = int(matches.group(\"p_nr\")) if matches.group(\"p_nr\") else None\n", " tInstallmentTt = int(matches.group(\"p_tt\")) if matches.group(\"p_tt\") else None\n", "\n", " tCountry = matches.group(\"country\")\n", " tOutflow = matches.group(\"outflow\").strip().replace(\".\", \"\").replace(\",\", \".\")\n", " tInflow = matches.group(\"inflow\").strip().replace(\".\", \"\").replace(\",\", \".\")\n", " tOwner = input_dict[key][\"owner_id\"]\n", "\n", " tCreated = str(datetime.now(tz=None))\n", " tUpdated = None\n", "\n", " preHash = tTdate + tMemo + tOutflow + tInflow\n", " tId = hashlib.sha256(preHash.encode()).hexdigest()\n", "\n", " if matches.group(\"memo\") != \"Saldo Anterior\":\n", " insert_bulk.append(\n", " (\n", " tId,\n", " tTdate,\n", " tAccount,\n", " tMemo.strip(),\n", " tCountry,\n", " tOutflow,\n", " tInflow,\n", " tOwner,\n", " tInstallmentNr,\n", " tInstallmentTt,\n", " tCreated,\n", " tUpdated,\n", " )\n", " )\n", "\n", " return insert_bulk" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def db_insert(insert_bulk: list[tuple]):\n", " from mysql.connector import connect, Error\n", "\n", " try:\n", " with connect(\n", " host=\"127.0.0.1\",\n", " user=\"root\",\n", " password=\"pleasehashapasswordomg\",\n", " database=\"default\",\n", " ) as connection:\n", " print(\"CONNECTED!\", connection)\n", " with connection.cursor() as cursor:\n", " cursor.executemany(insert_query, insert_bulk)\n", " connection.commit()\n", " print(\"DONE!\")\n", " except Error as e:\n", " print(e)\n", " finally:\n", " connection.close()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def executor():\n", "\n", " import glob\n", "\n", " matched_files = glob.glob(\"./documents/OUROCARD*.txt\")\n", " # matched_files = glob.glob(\"./documents/OUROCARD_VISA_INFINITE-Mar_24.txt\")\n", "\n", " try:\n", " for file_name in matched_files:\n", " db_insert(build_insert(create_lists(file_name), 2))\n", " except:\n", " print(\"executor(): Error\")\n", " \n", " print(\"EXECUTOR COMPLETED.\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Error during Transaction Lists creation\n", "executor(): Error\n", "EXECUTOR COMPLETED.\n" ] } ], "source": [ "executor()" ] } ], "metadata": { "kernelspec": { "display_name": "base", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.7" } }, "nbformat": 4, "nbformat_minor": 2 }