Actualización del 2025-08-16 a las 17:45:53

This commit is contained in:
jlimolina 2025-08-16 17:45:53 +02:00
parent b26e9ad87f
commit db6fa3d2c3
5 changed files with 232 additions and 165 deletions

View file

@ -1,4 +1,5 @@
import hashlib
import re # <-- CORRECCIÓN: Se importa el módulo de expresiones regulares
from datetime import datetime
import logging
import feedparser
@ -14,8 +15,6 @@ def process_single_feed(feed_data):
"""
feed_id = feed_data['id']
feed_url = feed_data['url']
# --- LÍNEA CLAVE ---
# Obtenemos el nombre del feed para usarlo como fuente de la noticia.
feed_nombre = feed_data.get('nombre', 'Fuente Desconocida')
etag = feed_data.get('last_etag')
modified = feed_data.get('last_modified')
@ -55,9 +54,19 @@ def process_single_feed(feed_data):
noticia_id = hashlib.md5(link.encode()).hexdigest()
titulo = entry.get("title", "Sin título")
resumen_html = entry.get("summary", "")
imagen_url = ""
# --- MEJORA: Se prioriza el contenido completo sobre el resumen para obtener más texto ---
resumen_html = ""
if hasattr(entry, 'content') and entry.content:
resumen_html = entry.content[0].value
elif hasattr(entry, 'summary'):
resumen_html = entry.summary
# --- CORRECCIÓN: Limpia los códigos de media personalizados y otros artefactos ---
if resumen_html:
resumen_html = re.sub(r'\[\[\{.*?\}\]\]', '', resumen_html)
imagen_url = ""
if "media_content" in entry and entry.media_content:
imagen_url = entry.media_content[0].get("url", "")
elif resumen_html:
@ -67,14 +76,13 @@ def process_single_feed(feed_data):
imagen_url = img_tag['src']
resumen_texto_plano = BeautifulSoup(resumen_html, 'html.parser').get_text(separator=' ', strip=True)
fecha_publicacion = datetime.now()
if hasattr(entry, 'published_parsed') and entry.published_parsed:
fecha_publicacion = datetime(*entry.published_parsed[:6])
elif hasattr(entry, 'updated_parsed') and entry.updated_parsed:
fecha_publicacion = datetime(*entry.updated_parsed[:6])
# --- LÍNEA CLAVE ---
# Añadimos 'feed_nombre' a la tupla de datos que se guardará en la BD.
noticias_encontradas.append(
(noticia_id, titulo, resumen_texto_plano, link, fecha_publicacion,
imagen_url, feed_nombre, feed_data['categoria_id'], feed_data['pais_id'])
@ -92,4 +100,3 @@ def process_single_feed(feed_data):
logging.error(f"Excepción inesperada al procesar el feed {feed_url} (ID: {feed_id}): {e}", exc_info=True)
return feed_id, noticias_encontradas, new_etag, new_modified, success