cambio a html
This commit is contained in:
@ -6,6 +6,10 @@ public class NewsArticle {
|
|||||||
private String url;
|
private String url;
|
||||||
private String content;
|
private String content;
|
||||||
|
|
||||||
|
public NewsArticle(String title, String description, String url) {
|
||||||
|
this(title,description,url,null);
|
||||||
|
}
|
||||||
|
|
||||||
public NewsArticle(String title, String description, String url, String content) {
|
public NewsArticle(String title, String description, String url, String content) {
|
||||||
this.title = title;
|
this.title = title;
|
||||||
this.description = description;
|
this.description = description;
|
||||||
|
@ -85,6 +85,24 @@ public class TelegramBot extends TelegramLongPollingBot {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void sendMessageHtml(String chatId, String text) {
|
||||||
|
List<String> partes = dividirMensaje(text);
|
||||||
|
|
||||||
|
for (String parte : partes) {
|
||||||
|
SendMessage sendMessage = new SendMessage();
|
||||||
|
sendMessage.setChatId(chatId);
|
||||||
|
sendMessage.setText(parte);
|
||||||
|
sendMessage.setParseMode("HTML");
|
||||||
|
|
||||||
|
try {
|
||||||
|
execute(sendMessage);
|
||||||
|
} catch (TelegramApiException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Scheduled(cron = "0 0 13,20 * * ?", zone = "Europe/Paris")
|
@Scheduled(cron = "0 0 13,20 * * ?", zone = "Europe/Paris")
|
||||||
private void mensajeProgramado(){
|
private void mensajeProgramado(){
|
||||||
this.construirResumenNoticias(this.CHAT_ID);
|
this.construirResumenNoticias(this.CHAT_ID);
|
||||||
@ -154,28 +172,31 @@ public class TelegramBot extends TelegramLongPollingBot {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void buscarNoticiasYEnviar(String chatId, String query) {
|
private void buscarNoticiasYEnviar(String chatId, String query) {
|
||||||
sendMessage(chatId, "Analizando noticias esto puede tardar un poco ten paciencia");
|
String mensajeInicio = "<i>Analizando noticias...esto puede tardar un poco, ten paciencia...</i>";
|
||||||
try{
|
String mensajeError = "<b>Lo siento...</b> se me ha licuado el cerebro... Mira los logs para saber más.";
|
||||||
List<NewsArticle> noticias = scrapper.searchNews(query);
|
sendMessageHtml(chatId, mensajeInicio);
|
||||||
|
|
||||||
if (noticias.isEmpty()) {
|
try {
|
||||||
sendMessage(chatId, "No encontré noticias sobre: *" + escapeMarkdown(query) + "*");
|
List<NewsArticle> noticias = scrapper.searchNews(query);
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
StringBuilder mensaje = new StringBuilder("📰 *Noticias sobre " + escapeMarkdown(query) + "*:\n\n");
|
if (noticias.isEmpty()) {
|
||||||
for (NewsArticle noticia : noticias) {
|
sendMessageHtml(chatId, "No encontré noticias sobre: <b>" + query + "</b>");
|
||||||
mensaje.append("🔹 [")
|
return;
|
||||||
.append(escapeMarkdown(noticia.getTitle()))
|
}
|
||||||
.append("](")
|
|
||||||
.append(noticia.getUrl())
|
|
||||||
.append(")\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
sendMessage(chatId, mensaje.toString());
|
StringBuilder mensaje = new StringBuilder("📰 <b>Noticias sobre " + query + ":</b>\n\n");
|
||||||
|
for (NewsArticle noticia : noticias) {
|
||||||
|
mensaje.append("🔹 <a href=\"")
|
||||||
|
.append(noticia.getUrl())
|
||||||
|
.append("\">")
|
||||||
|
.append(noticia.getTitle())
|
||||||
|
.append("</a>\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
sendMessageHtml(chatId, mensaje.toString());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
sendMessage(chatId, "Lo siento se me ha licuado el cerebro Mira los logs para saber más");
|
sendMessageHtml(chatId, mensajeError);
|
||||||
sendMessage(chatId, e.toString());
|
sendMessageHtml(chatId, "<pre>" + e.toString() + "</pre>");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,9 +36,9 @@ public class GoogleNewsScraperService {
|
|||||||
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);
|
||||||
String content = extractArticleContent(finalUrl);
|
//String content = extractArticleContent(finalUrl);
|
||||||
|
|
||||||
newsList.add(new NewsArticle(title, description, finalUrl, content));
|
newsList.add(new NewsArticle(title, description, finalUrl));
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@ -58,7 +58,7 @@ public class GoogleNewsScraperService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private String extractArticleContent(String url) {
|
/*private String extractArticleContent(String url) {
|
||||||
try {
|
try {
|
||||||
Document doc = Jsoup.connect(url).userAgent(USER_AGENT).get();
|
Document doc = Jsoup.connect(url).userAgent(USER_AGENT).get();
|
||||||
Elements paragraphs = doc.select("article p, .post-content p, .entry-content p, .content p, #article-body p");
|
Elements paragraphs = doc.select("article p, .post-content p, .entry-content p, .content p, #article-body p");
|
||||||
@ -70,6 +70,6 @@ public class GoogleNewsScraperService {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return "Error al extraer contenido";
|
return "Error al extraer contenido";
|
||||||
}
|
}
|
||||||
}
|
}*/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user