From b762b366a5015ab6e21782481b9972af4e98edd8 Mon Sep 17 00:00:00 2001 From: Achilles Date: Sat, 6 Jan 2024 12:35:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E9=83=A8=E5=88=86=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../stock/market/web/MoneyApiController.java | 85 ++++++++++++++----- 1 file changed, 65 insertions(+), 20 deletions(-) diff --git a/src/main/java/cn/stock/market/web/MoneyApiController.java b/src/main/java/cn/stock/market/web/MoneyApiController.java index b253b16..31abf28 100644 --- a/src/main/java/cn/stock/market/web/MoneyApiController.java +++ b/src/main/java/cn/stock/market/web/MoneyApiController.java @@ -28,6 +28,7 @@ import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import java.io.IOException; +import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.stream.Collectors; @@ -183,83 +184,93 @@ public class MoneyApiController { - private static void nseActives() { + private static List nseActives() { + List list = new ArrayList<>(); String url = "https://www.moneycontrol.com/stocks/marketstats/nse-mostactive-stocks/nifty-50-9/"; try { Document doc = Jsoup.connect(url).get(); int size = doc.select("#mc_content > section > section > div.clearfix.stat_container > div.columnst.FR.wbg.brdwht > div > div.bsr_table.hist_tbl_hm > table > tbody > tr").size(); - System.err.println(size); for (int i =1;i<=size;i++){ + MoneyStockSuggestDTO dto = new MoneyStockSuggestDTO(); Element company_a = doc.select("#mc_content > section > section > div.clearfix.stat_container > div.columnst.FR.wbg.brdwht > div > div.bsr_table.hist_tbl_hm > table > tbody > tr:nth-child("+i+") > td.PR > span > a").first(); if (company_a != null) { String stockUrl = company_a.attr("href"); String stockName = company_a.text(); - log.info(stockName); - log.info(stockUrl); + dto.setStockName(stockName); + dto.setStockUrl(stockUrl); } String highPrice = getTextOrEmpty(doc.select("#mc_content > section > section > div.clearfix.stat_container > div.columnst.FR.wbg.brdwht > div > div.bsr_table.hist_tbl_hm > table > tbody > tr:nth-child("+i+") > td:nth-child(2)").first()); - log.info(highPrice); + dto.setHighPrice(highPrice); String lowPrice = getTextOrEmpty(doc.select("#mc_content > section > section > div.clearfix.stat_container > div.columnst.FR.wbg.brdwht > div > div.bsr_table.hist_tbl_hm > table > tbody > tr:nth-child(" + i + ") > td:nth-child(3)").first()); - log.info(lowPrice); + dto.setLowPrice(lowPrice); String lastPrice = getTextOrEmpty(doc.select("#mc_content > section > section > div.clearfix.stat_container > div.columnst.FR.wbg.brdwht > div > div.bsr_table.hist_tbl_hm > table > tbody > tr:nth-child(" + i + ") > td:nth-child(4)").first()); - log.info(lastPrice); + dto.setLastPrice(lastPrice); String change = getTextOrEmpty(doc.select("#mc_content > section > section > div.clearfix.stat_container > div.columnst.FR.wbg.brdwht > div > div.bsr_table.hist_tbl_hm > table > tbody > tr:nth-child(" + i + ") > td:nth-child(5)").first()); - log.info(change); + dto.setChange(change); String changePercent = getTextOrEmpty(doc.select("#mc_content > section > section > div.clearfix.stat_container > div.columnst.FR.wbg.brdwht > div > div.bsr_table.hist_tbl_hm > table > tbody > tr:nth-child(" + i + ") > td:nth-child(6)").first()); - log.info(changePercent); + dto.setChangePercent(changePercent); + dto.setStockType("nse"); + + list.add(dto); log.info("---------------------------------------------" + i); } } catch (IOException e) { e.printStackTrace(); } + return list; } - private static void bseActives() { + private static List bseActives() { + List list = new ArrayList<>(); + String url = "https://www.moneycontrol.com/stocks/marketstats/bsemact1/index.php"; try { Document doc = Jsoup.connect(url).get(); int size = doc.select("#mc_content > section > section > div.clearfix.stat_container > div.columnst.FR.wbg.brdwht > div > div > div.bsr_table.hist_tbl_hm > table > tbody > tr").size(); System.err.println(size); for (int i =1;i<=size;i++){ + MoneyStockSuggestDTO dto = new MoneyStockSuggestDTO(); Element company_a = doc.select("#mc_content > section > section > div.clearfix.stat_container > div.columnst.FR.wbg.brdwht > div > div > div.bsr_table.hist_tbl_hm > table > tbody > tr:nth-child("+i+") > td.PR > span > a").first(); if (company_a != null) { String stockUrl = company_a.attr("href"); String stockName = company_a.text(); - log.info(stockName); - log.info(stockUrl); + dto.setStockName(stockName); + dto.setStockUrl(stockUrl); } String highPrice = getTextOrEmpty(doc.select("#mc_content > section > section > div.clearfix.stat_container > div.columnst.FR.wbg.brdwht > div > div > div.bsr_table.hist_tbl_hm > table > tbody > tr:nth-child(" + i + ") > td:nth-child(2)").first()); - log.info(highPrice); + dto.setHighPrice(highPrice); String lowPrice = getTextOrEmpty(doc.select("#mc_content > section > section > div.clearfix.stat_container > div.columnst.FR.wbg.brdwht > div > div > div.bsr_table.hist_tbl_hm > table > tbody > tr:nth-child(" + i + ") > td:nth-child(3)").first()); - log.info(lowPrice); + dto.setLowPrice(lowPrice); String lastPrice = getTextOrEmpty(doc.select("#mc_content > section > section > div.clearfix.stat_container > div.columnst.FR.wbg.brdwht > div > div > div.bsr_table.hist_tbl_hm > table > tbody > tr:nth-child(" + i + ") > td:nth-child(4)").first()); - log.info(lastPrice); + dto.setLastPrice(lastPrice); String prevClosePrice = getTextOrEmpty(doc.select("#mc_content > section > section > div.clearfix.stat_container > div.columnst.FR.wbg.brdwht > div > div > div.bsr_table.hist_tbl_hm > table > tbody > tr:nth-child(" + i + ") > td:nth-child(5)").first()); - log.info(prevClosePrice); + dto.setPrevClosePrice(prevClosePrice); String change = getTextOrEmpty(doc.select("#mc_content > section > section > div.clearfix.stat_container > div.columnst.FR.wbg.brdwht > div > div > div.bsr_table.hist_tbl_hm > table > tbody > tr:nth-child(" + i + ") > td:nth-child(6)").first()); - log.info(change); + dto.setChange(change); String changePercent = getTextOrEmpty(doc.select("#mc_content > section > section > div.clearfix.stat_container > div.columnst.FR.wbg.brdwht > div > div > div.bsr_table.hist_tbl_hm > table > tbody > tr:nth-child(" + i + ") > td:nth-child(7)").first()); - log.info(changePercent); + dto.setChangePercent(changePercent); + list.add(dto); log.info("---------------------------------------------" + i); } } catch (IOException e) { - e.printStackTrace(); + log.error("occur Exception",e); } + return list; } private static List bseGainer() { @@ -488,7 +499,7 @@ public class MoneyApiController { }) @ApiResponses(value = { @ApiResponse(code = 200, message = "" + - "股票推荐相关: top gainer", response = JSONObject.class), + "股票推荐相关: TopLoser", response = JSONObject.class), }) @GetMapping("/api/market/money/getTopLoser") @ResponseBody @@ -518,6 +529,40 @@ public class MoneyApiController { + @ApiOperation(value = "股票推荐TopActive",httpMethod = "GET") + @ApiImplicitParams({ + @ApiImplicitParam(name="stockType",value = "BSE或者NSE"), + }) + @ApiResponses(value = { + @ApiResponse(code = 200, message = "" + + "股票推荐相关: top active", response = JSONObject.class), + }) + @GetMapping("/api/market/money/getTopActive") + @ResponseBody + public List getTopActive(@RequestParam String stockType) { + List moneyStockSuggestDTOS = null; + if(StringUtils.equals(stockType,"nse")){ + moneyStockSuggestDTOS = nseActives(); + }else if(StringUtils.equals(stockType,"bse")){ + moneyStockSuggestDTOS = bseActives(); + } + if (CollectionUtils.isNotEmpty(moneyStockSuggestDTOS)){ + moneyStockSuggestDTOS.stream().forEach(f-> + f.getStockUrl().replaceAll("(? selfUlrList = moneyStockSuggestDTOS.stream().map(MoneyStockSuggestDTO::getStockUrl).collect(Collectors.toList()); + if(CollectionUtils.isNotEmpty(selfUlrList)){ + List all = moneyStockRepository.findAll(QMoneyStockPO.moneyStockPO.selfUrl.in(selfUlrList)); + if(CollectionUtils.isNotEmpty(all)){ + moneyStockSuggestDTOS.stream().filter(f->all.stream().anyMatch(s->s.getSelfUrl().equals(f.getStockUrl()))) + .forEach(f->f.setScId(all.stream().filter(s->s.getSelfUrl().equals(f.getStockUrl())).findFirst().orElse(null).getMoneyScId())); + } + } + } + + return moneyStockSuggestDTOS; + } + + public static void main(String[] args) { nseGainer();