noticias con menos de 8 meses

This commit is contained in:
2025-04-07 19:23:04 +02:00
parent eea2d8ae2f
commit cc43fc0fcc

View File

@ -14,7 +14,10 @@ import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date;
import java.util.List; import java.util.List;
@Service @Service
@ -39,19 +42,34 @@ public class GoogleNewsScraperService {
SyndFeed feed = input.build(new XmlReader(conn.getInputStream())); SyndFeed feed = input.build(new XmlReader(conn.getInputStream()));
List<SyndEntry> entries = feed.getEntries(); List<SyndEntry> entries = feed.getEntries();
int limit = Math.min(entries.size(), 10); // Máximo 10 noticias
for (int i = 0; i < limit; i++) { // Calcular la fecha límite (hace 8 meses)
SyndEntry entry = entries.get(i); 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 title = entry.getTitle();
String link = entry.getLink(); String link = entry.getLink();
String description = (entry.getDescription() != null) ? entry.getDescription().getValue() : "Sin descripción"; String description = (entry.getDescription() != null) ? entry.getDescription().getValue() : "Sin descripción";
String finalUrl = resolveFinalUrl(link); String finalUrl = resolveFinalUrl(link);
logger.info("Noticia {}: {}", (i + 1), title); logger.info("Noticia {}: {}", (count + 1), title);
logger.info("Enlace: {}", finalUrl); logger.info("Enlace: {}", finalUrl);
newsList.add(new NewsArticle(title, description, finalUrl)); newsList.add(new NewsArticle(title, description, finalUrl));
count++;
} }
logger.info("Total de noticias procesadas: {}", newsList.size()); logger.info("Total de noticias procesadas: {}", newsList.size());