- Updated configuration to enforce Redis caching for notified articles, removing memory cache options. - Enhanced wallamonitor.py to load Redis cache settings and handle errors more effectively. - Implemented new API endpoints for clearing Redis cache and retrieving Telegram forum topics. - Improved frontend components to support fetching and displaying available Telegram threads. - Added functionality for clearing cache from the UI, ensuring better management of notified articles.
237 lines
10 KiB
Python
237 lines
10 KiB
Python
import logging
|
|
import redis
|
|
import json
|
|
|
|
NOTIFIED_ARTICLE_TTL = 7 * 24 * 60 * 60 # TTL de 7 días en segundos para artículos notificados
|
|
|
|
class RedisArticleCache:
|
|
"""Maneja el cache de artículos notificados usando Redis"""
|
|
|
|
def __init__(self, redis_host='localhost', redis_port=6379, redis_db=0, redis_password=None):
|
|
self.logger = logging.getLogger(__name__)
|
|
|
|
# Inicializar conexión Redis
|
|
try:
|
|
self._redis_client = redis.Redis(
|
|
host=redis_host,
|
|
port=redis_port,
|
|
db=redis_db,
|
|
password=redis_password,
|
|
decode_responses=True,
|
|
socket_connect_timeout=5,
|
|
socket_timeout=5
|
|
)
|
|
# Verificar conexión
|
|
self._redis_client.ping()
|
|
self.logger.info(f"Conectado a Redis en {redis_host}:{redis_port} (db={redis_db})")
|
|
except (redis.ConnectionError, redis.TimeoutError) as e:
|
|
self.logger.error(f"Error conectando a Redis: {e}")
|
|
self.logger.error("Redis no está disponible. El sistema no podrá evitar duplicados sin Redis.")
|
|
raise
|
|
except Exception as e:
|
|
self.logger.error(f"Error inesperado inicializando Redis: {e}")
|
|
raise
|
|
|
|
def _get_article_key(self, article):
|
|
"""Genera una clave única para un artículo en Redis"""
|
|
return f"notified:{article.get_platform()}:{article.get_id()}"
|
|
|
|
def is_article_notified(self, article):
|
|
"""Verifica si un artículo ya ha sido notificado"""
|
|
try:
|
|
key = self._get_article_key(article)
|
|
return self._redis_client.exists(key) > 0
|
|
except Exception as e:
|
|
self.logger.error(f"Error verificando artículo en Redis: {e}")
|
|
return False
|
|
|
|
def mark_article_as_notified(self, article):
|
|
"""Marca un artículo como notificado en Redis con TTL, guardando toda la información del artículo"""
|
|
try:
|
|
key = self._get_article_key(article)
|
|
# Guardar toda la información del artículo como JSON
|
|
# Verificar si el artículo ya existe para mantener el estado de favorito
|
|
existing_value = self._redis_client.get(key)
|
|
is_favorite = False
|
|
if existing_value:
|
|
try:
|
|
existing_data = json.loads(existing_value)
|
|
is_favorite = existing_data.get('is_favorite', False)
|
|
except json.JSONDecodeError:
|
|
pass
|
|
|
|
article_data = {
|
|
'id': article.get_id(),
|
|
'title': article.get_title(),
|
|
'description': article._description, # Acceder al campo privado para obtener la descripción completa
|
|
'price': article.get_price(),
|
|
'currency': article.get_currency(),
|
|
'location': article.get_location(),
|
|
'allows_shipping': article._allows_shipping, # Acceder al campo privado para obtener el valor booleano
|
|
'url': article.get_url(),
|
|
'images': article.get_images(),
|
|
'modified_at': article.get_modified_at(),
|
|
'platform': article.get_platform(),
|
|
'is_favorite': is_favorite, # Mantener el estado de favorito
|
|
}
|
|
self._redis_client.setex(key, NOTIFIED_ARTICLE_TTL, json.dumps(article_data))
|
|
except Exception as e:
|
|
self.logger.error(f"Error marcando artículo como notificado en Redis: {e}")
|
|
|
|
def mark_articles_as_notified(self, articles):
|
|
"""Añade múltiples artículos a la lista de artículos ya notificados en Redis"""
|
|
article_list = articles if isinstance(articles, list) else [articles]
|
|
|
|
try:
|
|
# Verificar qué artículos ya existen antes de añadirlos
|
|
# Usar pipeline para mejor rendimiento al verificar múltiples artículos
|
|
pipe = self._redis_client.pipeline()
|
|
keys_to_check = []
|
|
for article in article_list:
|
|
key = self._get_article_key(article)
|
|
keys_to_check.append((article, key))
|
|
pipe.exists(key)
|
|
|
|
# Ejecutar las verificaciones
|
|
exists_results = pipe.execute()
|
|
|
|
# Ahora añadir solo los artículos que no existen
|
|
pipe = self._redis_client.pipeline()
|
|
added_count = 0
|
|
skipped_count = 0
|
|
|
|
for (article, key), exists in zip(keys_to_check, exists_results):
|
|
if exists > 0:
|
|
# El artículo ya existe, no hacer nada
|
|
skipped_count += 1
|
|
continue
|
|
|
|
# El artículo no existe, añadirlo
|
|
# Guardar toda la información del artículo como JSON
|
|
article_data = {
|
|
'id': article.get_id(),
|
|
'title': article.get_title(),
|
|
'description': article._description, # Acceder al campo privado para obtener la descripción completa
|
|
'price': article.get_price(),
|
|
'currency': article.get_currency(),
|
|
'location': article.get_location(),
|
|
'allows_shipping': article._allows_shipping, # Acceder al campo privado para obtener el valor booleano
|
|
'url': article.get_url(),
|
|
'images': article.get_images(),
|
|
'modified_at': article.get_modified_at(),
|
|
'platform': article.get_platform(),
|
|
'is_favorite': False, # Por defecto no es favorito
|
|
}
|
|
pipe.setex(key, NOTIFIED_ARTICLE_TTL, json.dumps(article_data))
|
|
added_count += 1
|
|
|
|
# Ejecutar solo si hay artículos para añadir
|
|
if added_count > 0:
|
|
pipe.execute()
|
|
|
|
self.logger.debug(f"{added_count} artículos añadidos, {skipped_count} ya existían en Redis")
|
|
except Exception as e:
|
|
self.logger.error(f"Error añadiendo artículos a Redis: {e}")
|
|
|
|
def set_favorite(self, platform, article_id, is_favorite=True):
|
|
"""Marca o desmarca un artículo como favorito en Redis"""
|
|
try:
|
|
key = f"notified:{platform}:{article_id}"
|
|
value = self._redis_client.get(key)
|
|
if value:
|
|
article_data = json.loads(value)
|
|
article_data['is_favorite'] = is_favorite
|
|
# Mantener el TTL existente o usar el default
|
|
ttl = self._redis_client.ttl(key)
|
|
if ttl > 0:
|
|
self._redis_client.setex(key, ttl, json.dumps(article_data))
|
|
else:
|
|
self._redis_client.setex(key, NOTIFIED_ARTICLE_TTL, json.dumps(article_data))
|
|
return True
|
|
return False
|
|
except Exception as e:
|
|
self.logger.error(f"Error marcando favorito en Redis: {e}")
|
|
return False
|
|
|
|
def get_favorites(self):
|
|
"""Obtiene todos los artículos marcados como favoritos"""
|
|
try:
|
|
keys = self._redis_client.keys('notified:*')
|
|
favorites = []
|
|
for key in keys:
|
|
value = self._redis_client.get(key)
|
|
if value:
|
|
try:
|
|
article_data = json.loads(value)
|
|
if article_data.get('is_favorite', False):
|
|
favorites.append(article_data)
|
|
except json.JSONDecodeError:
|
|
continue
|
|
return favorites
|
|
except Exception as e:
|
|
self.logger.error(f"Error obteniendo favoritos de Redis: {e}")
|
|
return []
|
|
|
|
def is_favorite(self, platform, article_id):
|
|
"""Verifica si un artículo es favorito"""
|
|
try:
|
|
key = f"notified:{platform}:{article_id}"
|
|
value = self._redis_client.get(key)
|
|
if value:
|
|
article_data = json.loads(value)
|
|
return article_data.get('is_favorite', False)
|
|
return False
|
|
except Exception as e:
|
|
self.logger.error(f"Error verificando favorito en Redis: {e}")
|
|
return False
|
|
|
|
def clear_cache(self):
|
|
"""Elimina toda la caché de artículos notificados en Redis"""
|
|
try:
|
|
# Obtener todas las claves que empiezan con 'notified:'
|
|
keys = self._redis_client.keys('notified:*')
|
|
|
|
if not keys:
|
|
self.logger.info("Cache de Redis ya está vacío")
|
|
return 0
|
|
|
|
# Eliminar todas las claves usando pipeline para mejor rendimiento
|
|
count = len(keys)
|
|
pipe = self._redis_client.pipeline()
|
|
for key in keys:
|
|
pipe.delete(key)
|
|
pipe.execute()
|
|
|
|
self.logger.info(f"Cache de Redis limpiado: {count} artículos eliminados")
|
|
return count
|
|
except Exception as e:
|
|
self.logger.error(f"Error limpiando cache de Redis: {e}")
|
|
return 0
|
|
|
|
|
|
def create_article_cache(cache_type='redis', **kwargs):
|
|
"""
|
|
Factory function para crear el cache de artículos usando Redis.
|
|
|
|
Args:
|
|
cache_type: 'redis' (solo Redis está soportado)
|
|
**kwargs: Argumentos para Redis:
|
|
- redis_host: host de Redis (default: 'localhost')
|
|
- redis_port: puerto de Redis (default: 6379)
|
|
- redis_db: base de datos de Redis (default: 0)
|
|
- redis_password: contraseña de Redis (opcional)
|
|
|
|
Returns:
|
|
RedisArticleCache
|
|
"""
|
|
if cache_type != 'redis':
|
|
raise ValueError(f"Tipo de cache desconocido: {cache_type}. Solo se soporta 'redis'")
|
|
|
|
return RedisArticleCache(
|
|
redis_host=kwargs.get('redis_host', 'localhost'),
|
|
redis_port=kwargs.get('redis_port', 6379),
|
|
redis_db=kwargs.get('redis_db', 0),
|
|
redis_password=kwargs.get('redis_password')
|
|
)
|
|
|