From ef50f8c81126d319a6632b77240bd7bd0b6071f5 Mon Sep 17 00:00:00 2001 From: vu-tran Date: Mon, 18 Aug 2025 12:54:13 +0700 Subject: [PATCH] update get ger stock --- .../infrastructure/job/InvestingTask.java | 85 ++++++++++++++++++- 1 file changed, 83 insertions(+), 2 deletions(-) diff --git a/src/main/java/cn/stock/market/infrastructure/job/InvestingTask.java b/src/main/java/cn/stock/market/infrastructure/job/InvestingTask.java index d658cbd..27cfdb6 100644 --- a/src/main/java/cn/stock/market/infrastructure/job/InvestingTask.java +++ b/src/main/java/cn/stock/market/infrastructure/job/InvestingTask.java @@ -310,8 +310,7 @@ public class InvestingTask { } } - @Scheduled(cron = "0 0 0/3 * * ?") -// @PostConstruct +// @Scheduled(cron = "0 0 0/3 * * ?") public void getCincoDiasNews() { String baseUrl = "https://cincodias.elpais.com"; @@ -405,6 +404,88 @@ public class InvestingTask { } } + @Scheduled(cron = "0 0 0/3 * * ?") +// @PostConstruct + public void getBoerseNews(){ + String url_request = "https://www.boerse-online.de"; + try { + List results = new ArrayList<>(); + + String listUrl = url_request + "/nachrichten/1"; + Document doc = Jsoup.connect(listUrl) + .userAgent("Mozilla/5.0") + .get(); + + Elements articles = doc.select("article.article-list-item"); + + for (Element article : articles) { + Element aTag = article.selectFirst("h2 a"); + String title = aTag != null ? aTag.text().trim() : null; + String link = aTag != null ? url_request + aTag.attr("href") : null; + + Element imgTag = article.selectFirst("figure img"); + String image = imgTag != null ? imgTag.attr("data-src") : null; + + Element timeTag = article.selectFirst("small.article-info time"); + Date publishedDate = null; + + if (timeTag != null) { + String datetimeAttr = timeTag.attr("datetime"); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); + LocalDateTime dateTime = LocalDateTime.parse(datetimeAttr, formatter); + ZoneId berlinZone = ZoneId.of("Europe/Berlin"); + publishedDate = Date.from(dateTime.atZone(berlinZone).toInstant()); + } + + Element authorTag = article.selectFirst("small.article-info strong"); + String author = authorTag != null ? authorTag.text().trim() : null; + + // Fetch article detail page + String htmlContent = ""; + if (link != null) { + try { + Document detailPage = Jsoup.connect(link) + .userAgent("Mozilla/5.0") + .get(); + + Element body = detailPage.selectFirst("div.article-body"); + if (body != null) { + htmlContent = body.html(); // ✅ inner HTML only + } + + } catch (Exception e) { + System.err.println("Error fetching article detail: " + link); + e.printStackTrace(); + } + } + SiteNews siteNews = new SiteNews(); + siteNews.setAddTime(new Date()); + siteNews.setSourceId(link); + siteNews.setTitle(title); + siteNews.setSourceName("BOERSE"); + siteNews.setDescription(title); + siteNews.setImgurl(image); + siteNews.setContent(htmlContent); + siteNews.setStatus(1); + siteNews.setType(1); // Set as financial news type + siteNews.setViews(0); + siteNews.setShowTime(publishedDate); + try { + newsRepository.save(siteNews); + log.info("Saved German news : {}", title); + } catch (Exception e) { + log.warn("Failed to save German news {}: {}", link, e.getMessage()); + } + } + }catch (Exception e){ + log.error("Error fetching article detail: {}", e.getMessage()); + e.printStackTrace(); + } + + + } + + /** * Test method to manually trigger German news sync * This can be called via REST API or scheduled task