diff --git a/bot/src/main/java/es/imunnic/inversionitasBot/services/GoogleNewsScraperService.java b/bot/src/main/java/es/imunnic/inversionitasBot/services/GoogleNewsScraperService.java index d3112bd..a8ac1f7 100644 --- a/bot/src/main/java/es/imunnic/inversionitasBot/services/GoogleNewsScraperService.java +++ b/bot/src/main/java/es/imunnic/inversionitasBot/services/GoogleNewsScraperService.java @@ -14,7 +14,10 @@ import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import java.net.HttpURLConnection; import java.net.URL; +import java.time.LocalDate; +import java.time.ZoneId; import java.util.ArrayList; +import java.util.Date; import java.util.List; @Service @@ -39,19 +42,34 @@ public class GoogleNewsScraperService { SyndFeed feed = input.build(new XmlReader(conn.getInputStream())); List entries = feed.getEntries(); - int limit = Math.min(entries.size(), 10); // Máximo 10 noticias - for (int i = 0; i < limit; i++) { - SyndEntry entry = entries.get(i); + // Calcular la fecha límite (hace 8 meses) + LocalDate currentDate = LocalDate.now(); + LocalDate thresholdDate = currentDate.minusMonths(8); + + int count = 0; + for (SyndEntry entry : entries) { + if (count >= 10) break; + + Date pubDate = entry.getPublishedDate(); + if (pubDate == null) continue; + + LocalDate entryDate = pubDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); + if (entryDate.isBefore(thresholdDate)) { + logger.info("Noticia descartada por antigüedad: {}", entry.getTitle()); + continue; + } + String title = entry.getTitle(); String link = entry.getLink(); String description = (entry.getDescription() != null) ? entry.getDescription().getValue() : "Sin descripción"; String finalUrl = resolveFinalUrl(link); - logger.info("Noticia {}: {}", (i + 1), title); + logger.info("Noticia {}: {}", (count + 1), title); logger.info("Enlace: {}", finalUrl); newsList.add(new NewsArticle(title, description, finalUrl)); + count++; } logger.info("Total de noticias procesadas: {}", newsList.size());