Optimización de código

This commit is contained in:
Your Name
2024-09-10 09:48:07 -06:00
parent 06feae543a
commit cfd666f70b

65
app.py
View File

@@ -7,6 +7,7 @@ from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import NoSuchElementException from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import StaleElementReferenceException
import os import os
from flask import Flask, request, jsonify from flask import Flask, request, jsonify
@@ -27,12 +28,7 @@ service = Service('/usr/bin/chromedriver')
driver = None driver = None
def extract(username: str, password: str): def insert_alumno_extraccion(datos_html: str, materias_html: str, historial_html: str = 'error', materias_actuales_html: str = 'error'):
url_credentials = f'https://{username}:{password}@sgu.ulsa.edu.mx/psulsa/alumnos/consultainformacionalumnos/consultainformacion.aspx'
url = 'https://sgu.ulsa.edu.mx/psulsa/alumnos/consultainformacionalumnos/consultainformacion.aspx'
username_integer = int(username[2:])
def insert_alumno_extraccion(datos_html: str, materias_html: str, historial_html: str = 'error', materias_actuales_html: str = 'error'):
try: try:
conn = psycopg2.connect( conn = psycopg2.connect(
dbname=os.getenv("DBNAME"), dbname=os.getenv("DBNAME"),
@@ -60,8 +56,7 @@ def extract(username: str, password: str):
finally: finally:
cur.close() cur.close()
conn.close() conn.close()
def update_alumno_extraccion_error(error: str):
def update_alumno_extraccion_error(error: str):
try: try:
conn = psycopg2.connect( conn = psycopg2.connect(
@@ -89,6 +84,11 @@ def extract(username: str, password: str):
cur.close() cur.close()
conn.close() conn.close()
def extract(username: str, password: str):
url_credentials = f'https://{username}:{password}@sgu.ulsa.edu.mx/psulsa/alumnos/consultainformacionalumnos/consultainformacion.aspx'
url = 'https://sgu.ulsa.edu.mx/psulsa/alumnos/consultainformacionalumnos/consultainformacion.aspx'
username_integer = int(username[2:])
try: try:
driver.get(url_credentials) driver.get(url_credentials)
driver.get(url) driver.get(url)
@@ -99,49 +99,50 @@ def extract(username: str, password: str):
elemento = WebDriverWait(driver, 3.5).until( elemento = WebDriverWait(driver, 3.5).until(
EC.presence_of_element_located((By.ID, 'ctl00_contenedor_HistorialAlumno1_lblBtnSeccionHAcademico')) EC.presence_of_element_located((By.ID, 'ctl00_contenedor_HistorialAlumno1_lblBtnSeccionHAcademico'))
) )
elemento.click()
# Get the HTML content of the materias element # Intentar varias veces en caso de un `StaleElementReferenceException`
for _ in range(3):
try:
elemento.click()
break # Si se hace clic correctamente, salir del bucle
except StaleElementReferenceException:
print("Elemento 'stale', intentando de nuevo...")
elemento = driver.find_element(By.ID, 'ctl00_contenedor_HistorialAlumno1_lblBtnSeccionHAcademico')
# Obtener el HTML de las materias
materias_html = driver.find_element(By.ID, 'ctl00_contenedor_HistorialAlumno1_divHAcademico').get_attribute('innerHTML') materias_html = driver.find_element(By.ID, 'ctl00_contenedor_HistorialAlumno1_divHAcademico').get_attribute('innerHTML')
historial_html = driver.find_element(By.ID, 'ctl00_contenedor_HistorialAlumno1_gvMaterias').get_attribute('innerHTML') historial_html = driver.find_element(By.ID, 'ctl00_contenedor_HistorialAlumno1_gvMaterias').get_attribute('innerHTML')
# materias_actuales_html = driver.find_element(By.ID, 'ctl00_contenedor_HistorialAlumno1_div13').get_attribute('innerHTML')
# Manejar el historial como DataFrame
historial_html_io = StringIO(f"<table>{historial_html}</table>") historial_html_io = StringIO(f"<table>{historial_html}</table>")
# Read the HTML table into a DataFrame
df = pd.read_html(historial_html_io)[0] df = pd.read_html(historial_html_io)[0]
if 'GRUPO' not in df.columns: if 'GRUPO' not in df.columns:
raise KeyError("Column 'GRUPO' not found in the DataFrame") raise KeyError("Column 'GRUPO' not found in the DataFrame")
# Remove decimals from the 'PERIODO' column (if present)
df['PERIODO'] = df['PERIODO'].apply(lambda x: str(x).replace('.0', '') if isinstance(x, (float, int)) else x) df['PERIODO'] = df['PERIODO'].apply(lambda x: str(x).replace('.0', '') if isinstance(x, (float, int)) else x)
# Convert the DataFrame to JSON
json_result = df[df['GRUPO'] != 'Promedio:'].to_json(orient='records') json_result = df[df['GRUPO'] != 'Promedio:'].to_json(orient='records')
# Connect to PostgreSQL database
query = insert_alumno_extraccion(datos_html, materias_html, json_result) query = insert_alumno_extraccion(datos_html, materias_html, json_result)
print("Data extracted successfully") print("Data extracted successfully")
return json_result return json_result
except NoSuchElementException as e: except NoSuchElementException as e:
update_alumno_extraccion_error(str(e)) update_alumno_extraccion_error(str(e))
def se_puede_extraer():
def se_puede():
try: try:
conn = conn = psycopg2.connect( # Establece la conexión a la base de datos usando with para gestionar automáticamente el cierre
with psycopg2.connect(
dbname=os.getenv("DBNAME"), dbname=os.getenv("DBNAME"),
user=os.getenv("DBUSER"), user=os.getenv("DBUSER"),
password=os.getenv("DBPASSWORD"), password=os.getenv("DBPASSWORD"),
host=os.getenv("DBHOST"), host=os.getenv("DBHOST"),
port=os.getenv("DBPORT") port=os.getenv("DBPORT")
) ) as conn:
cursor = conn.cursor() with conn.cursor() as cursor:
# SELECCIONAR ÚLTIMA planeacion
# SELECCIONAR ULTIMA planeacion
query = """ query = """
SELECT 1 SELECT 1
FROM alumno_extraccion_fecha FROM alumno_extraccion_fecha
@@ -154,15 +155,13 @@ def se_puede_extraer():
result = cursor.fetchone() result = cursor.fetchone()
# Verifica si se obtuvo algún resultado # Verifica si se obtuvo algún resultado
exists = result is not None return result is not None
except psycopg2.Error as e:
# Cierra el cursor y la conexión # Maneja errores específicos de la base de datos
cursor.close() print(f"Error en la base de datos: {e}")
conn.close()
return exists
except Exception as e: except Exception as e:
print(f"Error: {e}") # Maneja otros tipos de errores
print(f"Error general: {e}")
return False return False
app = Flask(__name__) app = Flask(__name__)