analisis trends
This commit is contained in:
@ -2,6 +2,8 @@ package es.imunnic.inversionitasBot;
|
||||
|
||||
import es.imunnic.inversionitasBot.services.ApiService;
|
||||
import es.imunnic.inversionitasBot.services.GoogleNewsScraperService;
|
||||
import es.imunnic.inversionitasBot.services.ResultadosService;
|
||||
import es.imunnic.inversionitasBot.services.Trends24Scrapper;
|
||||
import org.json.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -13,6 +15,7 @@ import org.telegram.telegrambots.meta.api.objects.Update;
|
||||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||||
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
@ -25,11 +28,13 @@ public class TelegramBot extends TelegramLongPollingBot {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(GoogleNewsScraperService.class);
|
||||
private GoogleNewsScraperService scrapper = new GoogleNewsScraperService();
|
||||
private Trends24Scrapper trendScrapper = new Trends24Scrapper();
|
||||
private ResultadosService resultadosService = new ResultadosService();
|
||||
//@Value("${telegram.bot.username}")
|
||||
private String BOT_USERNAME = "Inversionitas_Bot";
|
||||
//@Value("${telegram.bot.token}")
|
||||
private String BOT_TOKEN = "7626026035:AAHEMp_iIN3y8AwywL0R6OTQvNi7EcJZ0iY";
|
||||
|
||||
//private String BOT_TOKEN = "7626026035:AAHEMp_iIN3y8AwywL0R6OTQvNi7EcJZ0iY"; inversionitas bot
|
||||
private String BOT_TOKEN = "8017576757:AAHNGvY8RK_PbnlV1frM8KZMQ45tdMSvO3c";
|
||||
protected String CHAT_ID = "-1002289752202";
|
||||
protected Integer THREAD_INDICES = 138;
|
||||
protected Integer THREAD_NOTICIAS = 137;
|
||||
@ -56,6 +61,12 @@ public class TelegramBot extends TelegramLongPollingBot {
|
||||
} else if (messageText.toLowerCase().startsWith("/busca ")) {
|
||||
String query = messageText.substring(7).trim();
|
||||
buscarNoticiasYEnviar(chatId, query);
|
||||
} else if (messageText.toLowerCase().startsWith("/resultados ")) {
|
||||
String query = messageText.substring(12).trim();
|
||||
buscarResultadosYEnviar(chatId, query);
|
||||
} else if (messageText.toLowerCase().startsWith("/trends ")) {
|
||||
String country = messageText.substring(8).trim().toLowerCase().replace(" ", "-");
|
||||
enviarTrends(chatId, country); // NUEVO
|
||||
} else {
|
||||
sendMessage(chatId, "Comando no reconocido. Usa /resumen para obtener el resumen diario.");
|
||||
}
|
||||
@ -73,6 +84,7 @@ public class TelegramBot extends TelegramLongPollingBot {
|
||||
SendMessage sendMessage = new SendMessage();
|
||||
sendMessage.setChatId(chatId);
|
||||
sendMessage.setText(parte);
|
||||
System.out.println(parte);
|
||||
sendMessage.setParseMode("MarkdownV2");
|
||||
|
||||
// Agregar el ID del tema si es distinto de null
|
||||
@ -209,6 +221,24 @@ public class TelegramBot extends TelegramLongPollingBot {
|
||||
}
|
||||
}
|
||||
|
||||
private void buscarResultadosYEnviar(String chatId, String query){
|
||||
Map<String, String> resultados = resultadosService.getBeneficioNetoAnual(query);
|
||||
|
||||
if (resultados.isEmpty()) {
|
||||
sendMessageHtml(chatId, "<b>No se encontraron resultados financieros para:</b> " + query);
|
||||
return;
|
||||
}
|
||||
|
||||
StringBuilder resultMessage = new StringBuilder();
|
||||
resultMessage.append("<b>Beneficio neto anual para: ").append(query).append("</b>\n\n");
|
||||
|
||||
resultados.forEach((año, beneficio) -> {
|
||||
resultMessage.append("📅 <b>").append(año).append(":</b> ").append(beneficio).append("\n");
|
||||
});
|
||||
|
||||
sendMessageHtml(chatId, resultMessage.toString());
|
||||
}
|
||||
|
||||
// Método para escapar caracteres especiales en Markdown
|
||||
private String escapeMarkdown(String text) {
|
||||
if (text == null) {
|
||||
@ -220,4 +250,29 @@ public class TelegramBot extends TelegramLongPollingBot {
|
||||
private String escapeHtml(String text) {
|
||||
return text.replace("&", "&");
|
||||
}
|
||||
|
||||
private void enviarTrends(String chatId, String country) {
|
||||
try {
|
||||
List<String> trends = trendScrapper.getTopTrends(country);
|
||||
|
||||
if (trends.isEmpty()) {
|
||||
sendMessage(chatId, "No se encontraron tendencias para: " + country);
|
||||
return;
|
||||
}
|
||||
|
||||
StringBuilder mensaje = new StringBuilder();
|
||||
mensaje.append("📈 <b>Top 20 tendencias en ").append(country.replace("-", " ")).append(":</b>\n\n");
|
||||
|
||||
int index = 1;
|
||||
for (String trend : trends) {
|
||||
mensaje.append(index++).append(". ").append(escapeHtml(trend)).append("\n");
|
||||
}
|
||||
|
||||
sendMessageHtml(chatId, mensaje.toString());
|
||||
} catch (IOException e) {
|
||||
sendMessage(chatId, "❌ Error al obtener las tendencias de Trends24, compruebe el nombre del pais");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,69 @@
|
||||
package es.imunnic.inversionitasBot.services;
|
||||
|
||||
import org.jsoup.Jsoup;
|
||||
import org.jsoup.nodes.Document;
|
||||
import org.jsoup.nodes.Element;
|
||||
import org.jsoup.select.Elements;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
public class ResultadosService {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ResultadosService.class);
|
||||
private static final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36";
|
||||
|
||||
/**
|
||||
* Realiza el scrap de los beneficios netos anuales de la empresa dada.
|
||||
*
|
||||
* @param companyName nombre de la empresa en la URL
|
||||
* @return Mapa con años como clave y beneficio neto como valor
|
||||
*/
|
||||
public Map<String, String> getBeneficioNetoAnual(String companyName) {
|
||||
Map<String, String> resultados = new LinkedHashMap<>();
|
||||
String url = "https://es.investing.com/equities/" + companyName + "-financial-summary"; // Cambia esta URL a la real
|
||||
|
||||
logger.info("Scrapeando resultados financieros de: {}", companyName);
|
||||
logger.info("URL objetivo: {}", url);
|
||||
|
||||
try {
|
||||
Document doc = Jsoup.connect(url)
|
||||
.userAgent(USER_AGENT)
|
||||
.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
|
||||
.header("Accept-Language", "es-ES,es;q=0.9")
|
||||
.header("Referer", "https://www.google.com/")
|
||||
.proxy("51.44.176.151", 20202)
|
||||
.timeout(10000)
|
||||
.get();
|
||||
|
||||
// Buscar la fila con el texto "Beneficio neto"
|
||||
Elements filas = doc.select("tr");
|
||||
for (Element fila : filas) {
|
||||
Element primeraColumna = fila.selectFirst("td");
|
||||
if (primeraColumna != null && primeraColumna.text().equalsIgnoreCase("Beneficio neto")) {
|
||||
Elements celdas = fila.select("td");
|
||||
for (int i = 2; i < celdas.size(); i++) { // Saltar los primeros <td> con el título
|
||||
String texto = celdas.get(i).text().trim();
|
||||
String año = String.valueOf(2020 + (i - 2)); // Ajusta esto si hay más o menos columnas
|
||||
if (!texto.equals("-") && !texto.isEmpty()) {
|
||||
resultados.put(año, texto);
|
||||
logger.info("Año {} → Beneficio neto: {}", año, texto);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
logger.error("Error durante el scraping de resultados de {}", companyName, e);
|
||||
}
|
||||
|
||||
logger.info("Beneficios netos encontrados: {}", resultados.size());
|
||||
return resultados;
|
||||
}
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package es.imunnic.inversionitasBot.services;
|
||||
|
||||
import org.jsoup.Jsoup;
|
||||
import org.jsoup.nodes.Document;
|
||||
import org.jsoup.nodes.Element;
|
||||
import org.jsoup.select.Elements;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Trends24Scrapper {
|
||||
|
||||
private static final String BASE_URL = "https://trends24.in/";
|
||||
|
||||
/**
|
||||
* Obtiene los 20 primeros trending topics de Trends24 para el país especificado.
|
||||
* @param country El país en inglés, tal como aparece en la URL (ejemplo: "united-states").
|
||||
* @return Lista de trending topics (Strings).
|
||||
* @throws IOException Si falla la conexión o parsing.
|
||||
*/
|
||||
public List<String> getTopTrends(String country) throws IOException {
|
||||
String url = BASE_URL + country + "/";
|
||||
Document doc = Jsoup.connect(url).get();
|
||||
|
||||
List<String> trends = new ArrayList<>();
|
||||
|
||||
Element trendList = doc.selectFirst("section#timeline ol.trend-card__list");
|
||||
if (trendList != null) {
|
||||
Elements trendElements = trendList.select("li .trend-name a");
|
||||
for (int i = 0; i < Math.min(20, trendElements.size()); i++) {
|
||||
Element trend = trendElements.get(i);
|
||||
trends.add(trend.text());
|
||||
}
|
||||
} else {
|
||||
System.out.println("❌ No se encontró la lista de tendencias.");
|
||||
}
|
||||
|
||||
return trends;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user