Initial commit of my_toolbox package with basic tools and setup files.
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
Metadata-Version: 2.1
|
||||
Name: my_toolbox
|
||||
Version: 0.1
|
||||
Summary: Paquete con mis herramientas básicas
|
||||
Author: Tr4shhh
|
||||
Author-email: tu.email@example.com
|
||||
@@ -0,0 +1,15 @@
|
||||
README.md
|
||||
setup.py
|
||||
my_toolbox/__init__.py
|
||||
my_toolbox/calcular_combinatoria.py
|
||||
my_toolbox/comparar_hash.py
|
||||
my_toolbox/driver_para_scraping.py
|
||||
my_toolbox/excel.py
|
||||
my_toolbox/generar_archivos_carpetas_desde_tree.py
|
||||
my_toolbox/manejo_archivos.py
|
||||
my_toolbox/print_to_txt.py
|
||||
my_toolbox.egg-info/PKG-INFO
|
||||
my_toolbox.egg-info/SOURCES.txt
|
||||
my_toolbox.egg-info/dependency_links.txt
|
||||
my_toolbox.egg-info/top_level.txt
|
||||
tests/test_herramientas.py
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
my_toolbox
|
||||
@@ -0,0 +1,5 @@
|
||||
|
||||
from .manejo_postgresql import *
|
||||
from .generar_archivo_env import *
|
||||
from .driver_para_scraping import *
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,59 @@
|
||||
import undetected_chromedriver as uc
|
||||
|
||||
|
||||
def driver_scrapping(carpeta_descargas, carpeta_perfil_chrome, ejecutable_chrome, headless=False):
|
||||
"""
|
||||
Description:
|
||||
Initializes a Chrome session with specific options and settings based on provided parameters.
|
||||
Configures download folder, user profile directory, and Chrome executable.
|
||||
Sets the browser window size and returns the configured driver object for external use.
|
||||
|
||||
Parameters:
|
||||
carpeta_descargas (str): Path to the download directory.
|
||||
carpeta_perfil_chrome (str): Path to the Chrome user profile directory.
|
||||
ejecutable_chrome (str): Path to the Chrome executable.
|
||||
|
||||
Returns:
|
||||
driver (WebDriver): Configured WebDriver object ready for automation tasks.
|
||||
|
||||
Usage:
|
||||
driver = driver_scrapping(download_folder, profile_folder, executable_path)
|
||||
|
||||
Created_at: 2024-05-04
|
||||
Updated_at: 2024-05-04
|
||||
"""
|
||||
try:
|
||||
# Configuration settings for the Chrome browser
|
||||
options = uc.ChromeOptions()
|
||||
options.headless = headless # Set to True for headless operation
|
||||
options.add_argument("--disable-blink-features=AutomationControlled")
|
||||
options.add_argument("--password-store=basic")
|
||||
|
||||
# Add experimental options and preferences for browser configuration
|
||||
options.add_experimental_option(
|
||||
"prefs",
|
||||
{
|
||||
"download.default_directory": carpeta_descargas,
|
||||
"download.prompt_for_download": False,
|
||||
"download.directory_upgrade": True,
|
||||
"safebrowsing.enabled": True,
|
||||
"credentials_enable_service": False,
|
||||
"profile.password_manager_enabled": False,
|
||||
"plugins.always_open_pdf_externally": True,
|
||||
}
|
||||
)
|
||||
|
||||
# Initialize the Chrome driver with specified options
|
||||
driver = uc.Chrome(options=options,
|
||||
user_data_dir=carpeta_perfil_chrome,
|
||||
browser_executable_path=ejecutable_chrome)
|
||||
|
||||
# Set browser window size
|
||||
driver.set_window_size(1200, 800)
|
||||
|
||||
print("Browser configured and ready for use.")
|
||||
|
||||
return driver
|
||||
|
||||
except Exception as e:
|
||||
print(f"Failed to load driver. {e}")
|
||||
@@ -0,0 +1,16 @@
|
||||
def crear_archivo_env(nombre_archivo='.env'):
|
||||
|
||||
variables = {
|
||||
'DB_PASSWORD': '',
|
||||
'DB_USER': 'postgres',
|
||||
'DB_HOST': 'localhost',
|
||||
'DB_PORT': '5432',
|
||||
'DB_NAME': 'data',
|
||||
|
||||
}
|
||||
|
||||
|
||||
with open(nombre_archivo, 'w') as archivo:
|
||||
for clave, valor in variables.items():
|
||||
archivo.write(f'{clave}={valor}\n')
|
||||
print(f'Archivo {nombre_archivo} creado correctamente.')
|
||||
@@ -0,0 +1,96 @@
|
||||
"""
|
||||
manejo_postgresql.py
|
||||
|
||||
Este módulo proporciona funciones para conectarse a una base de datos PostgreSQL y ejecutar consultas,
|
||||
devolviendo los resultados en un DataFrame de pandas.
|
||||
|
||||
Fecha de creación: 14/11/2024
|
||||
"""
|
||||
|
||||
import psycopg2
|
||||
import pandas as pd
|
||||
import os
|
||||
|
||||
|
||||
def connect_to_db(host, dbname, user, password, port=5432):
|
||||
"""
|
||||
Establece una conexión a la base de datos PostgreSQL usando credenciales del archivo .env.
|
||||
|
||||
Returns:
|
||||
conn (psycopg2.extensions.connection): Objeto de conexión a la base de datos.
|
||||
"""
|
||||
conn = psycopg2.connect(
|
||||
host=host,
|
||||
dbname=dbname,
|
||||
user=user,
|
||||
password=password,
|
||||
port=port
|
||||
)
|
||||
return conn
|
||||
|
||||
|
||||
|
||||
def query_to_dataframe(conn, query):
|
||||
"""
|
||||
Ejecuta una consulta SQL y devuelve los resultados en un DataFrame de pandas.
|
||||
|
||||
Args:
|
||||
conn (psycopg2.extensions.connection): Objeto de conexión a la base de datos.
|
||||
query (str): Consulta SQL a ejecutar.
|
||||
|
||||
Returns:
|
||||
df (pandas.DataFrame): DataFrame con los resultados de la consulta.
|
||||
"""
|
||||
df = pd.read_sql_query(query, conn)
|
||||
return df
|
||||
|
||||
|
||||
def dataframe_to_db(conn, df, table_name):
|
||||
"""
|
||||
Guarda un DataFrame en una tabla de la base de datos PostgreSQL.
|
||||
|
||||
Args:
|
||||
conn (psycopg2.extensions.connection): Objeto de conexión a la base de datos.
|
||||
df (pandas.DataFrame): DataFrame a guardar en la base de datos.
|
||||
table_name (str): Nombre de la tabla donde se guardará el DataFrame.
|
||||
"""
|
||||
cursor = conn.cursor()
|
||||
# Crear tabla si no existe
|
||||
create_table_query = f"""
|
||||
CREATE TABLE IF NOT EXISTS {table_name} (
|
||||
{', '.join([f'{col} TEXT' for col in df.columns])}
|
||||
);
|
||||
"""
|
||||
cursor.execute(create_table_query)
|
||||
conn.commit()
|
||||
|
||||
# Insertar datos
|
||||
for index, row in df.iterrows():
|
||||
insert_query = f"""
|
||||
INSERT INTO {table_name} ({', '.join(df.columns)})
|
||||
VALUES ({', '.join(['%s'] * len(row))});
|
||||
"""
|
||||
cursor.execute(insert_query, tuple(row))
|
||||
conn.commit()
|
||||
cursor.close()
|
||||
|
||||
|
||||
def append_dataframe_to_db(conn, df, table_name):
|
||||
"""
|
||||
Añade un DataFrame a una tabla existente en la base de datos PostgreSQL.
|
||||
|
||||
Args:
|
||||
conn (psycopg2.extensions.connection): Objeto de conexión a la base de datos.
|
||||
df (pandas.DataFrame): DataFrame a añadir a la base de datos.
|
||||
table_name (str): Nombre de la tabla donde se añadirá el DataFrame.
|
||||
"""
|
||||
cursor = conn.cursor()
|
||||
# Insertar datos
|
||||
for index, row in df.iterrows():
|
||||
insert_query = f"""
|
||||
INSERT INTO {table_name} ({', '.join(df.columns)})
|
||||
VALUES ({', '.join(['%s'] * len(row))});
|
||||
"""
|
||||
cursor.execute(insert_query, tuple(row))
|
||||
conn.commit()
|
||||
cursor.close()
|
||||
@@ -0,0 +1,15 @@
|
||||
from setuptools import setup, find_packages
|
||||
|
||||
setup(
|
||||
name='my_toolbox',
|
||||
version='0.1',
|
||||
packages=find_packages(),
|
||||
install_requires=[
|
||||
# Dependencias del paquete, por ejemplo:
|
||||
# 'numpy',
|
||||
# 'pandas'
|
||||
],
|
||||
description='Paquete con mis herramientas básicas',
|
||||
author='Tr4shhh',
|
||||
author_email='tu.email@example.com'
|
||||
)
|
||||
Reference in New Issue
Block a user