docker ready sin pruebas
This commit is contained in:
@ -1,14 +1,20 @@
|
||||
import requests
|
||||
from bs4 import BeautifulSoup
|
||||
import json
|
||||
import os
|
||||
import time
|
||||
import subprocess
|
||||
from googlenewsdecoder import gnewsdecoder
|
||||
from iacorrector import is_security_related, is_critico, is_favorable # Importa la función desde iacorrector.py
|
||||
from datetime import datetime
|
||||
import pytz
|
||||
import logging
|
||||
|
||||
# Configuración del logging
|
||||
LOG_FILE = "app.log"
|
||||
logging.basicConfig(
|
||||
filename=LOG_FILE, # Archivo de logs
|
||||
level=logging.INFO, # Nivel de logging (DEBUG, INFO, WARNING, ERROR, CRITICAL)
|
||||
format="%(asctime)s - %(levelname)s - %(message)s", # Formato de los logs
|
||||
)
|
||||
|
||||
HEADERS = {
|
||||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
|
||||
@ -23,7 +29,7 @@ def get_author_from_script(url):
|
||||
author = result.stdout.strip()
|
||||
return author if author else "Desconocido"
|
||||
except Exception as e:
|
||||
print(f"Error al obtener el autor para {url}: {e}")
|
||||
logging.info(f"Error al obtener el autor para {url}: {e}")
|
||||
return "Desconocido"
|
||||
|
||||
def get_url_from_google_news(url):
|
||||
@ -36,7 +42,7 @@ def get_url_from_google_news(url):
|
||||
else:
|
||||
return "N/C"
|
||||
except Exception as e:
|
||||
print(f"Error occurred: {e}")
|
||||
logging.info(f"Error occurred: {e}")
|
||||
|
||||
def get_article_content(url):
|
||||
"""
|
||||
@ -45,7 +51,7 @@ def get_article_content(url):
|
||||
try:
|
||||
response = requests.get(url, headers=HEADERS)
|
||||
if response.status_code != 200:
|
||||
print(f"Error al acceder a {url}: Código {response.status_code}")
|
||||
logging.info(f"Error al acceder a {url}: Código {response.status_code}")
|
||||
return "No se pudo obtener el contenido"
|
||||
|
||||
soup = BeautifulSoup(response.text, "html.parser")
|
||||
@ -68,7 +74,7 @@ def get_article_content(url):
|
||||
return "No se encontró contenido relevante"
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error al extraer contenido de {url}: {e}")
|
||||
logging.info(f"Error al extraer contenido de {url}: {e}")
|
||||
return "Error al extraer contenido"
|
||||
|
||||
def search_news(query):
|
||||
@ -79,7 +85,7 @@ def search_news(query):
|
||||
response = requests.get(base_url, headers=HEADERS)
|
||||
|
||||
if response.status_code != 200:
|
||||
print(f"Error al acceder a la página para la consulta '{query}': {response.status_code}")
|
||||
logging.info(f"Error al acceder a la página para la consulta '{query}': {response.status_code}")
|
||||
return []
|
||||
|
||||
soup = BeautifulSoup(response.content, 'xml')
|
||||
@ -123,19 +129,19 @@ def search_news(query):
|
||||
insertar_datos(news_item)
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error al procesar un artículo para '{query}': {e}")
|
||||
logging.info(f"Error al procesar un artículo para '{query}': {e}")
|
||||
|
||||
return news_list
|
||||
|
||||
def insertar_datos(news_item):
|
||||
API_URL = "http://127.0.0.1:8001/news/"
|
||||
API_URL = "http://localhost:8000/news/"
|
||||
|
||||
response = requests.post(API_URL, json=news_item)
|
||||
|
||||
if response.status_code == 200:
|
||||
print(f"Noticia '{news_item['titulo']}' creada con éxito.")
|
||||
logging.info(f"Noticia '{news_item['titulo']}' creada con éxito.")
|
||||
else:
|
||||
print(f"Error al insertar '{news_item['titulo']}':", response.status_code, response.json())
|
||||
logging.info(f"Error al insertar '{news_item['titulo']}':", response.status_code, response.json())
|
||||
|
||||
def search_from_keywords_file():
|
||||
"""
|
||||
@ -151,14 +157,14 @@ def search_from_keywords_file():
|
||||
keywords = [keyword.strip() for keyword in keywords]
|
||||
|
||||
for keyword in keywords:
|
||||
print(f"\nBuscando noticias sobre: {keyword}")
|
||||
logging.info(f"\nBuscando noticias sobre: {keyword}")
|
||||
search_news(keyword)
|
||||
time.sleep(2) # Pausa para evitar bloqueos por demasiadas solicitudes en poco tiempo
|
||||
|
||||
except FileNotFoundError:
|
||||
print("No se encontró el archivo 'keywords.txt'.")
|
||||
logging.info("No se encontró el archivo 'keywords.txt'.")
|
||||
except Exception as e:
|
||||
print(f"Error al leer el archivo 'keywords.txt': {e}")
|
||||
logging.info(f"Error al leer el archivo 'keywords.txt': {e}")
|
||||
|
||||
# Ejecutar la búsqueda desde el archivo
|
||||
search_from_keywords_file()
|
||||
|
Reference in New Issue
Block a user