update get detail from bseindia

This commit is contained in:
vpckiet
2024-12-10 16:37:54 +07:00
parent c89345329d
commit 615aae7e1d
2 changed files with 79 additions and 4 deletions

View File

@@ -83,4 +83,6 @@ public class MoneyStockPO {
/** /**
* 是否展示 0是 1否 */ * 是否展示 0是 1否 */
Integer isShow; Integer isShow;
Boolean useFromBseindia = false;
} }

View File

@@ -40,9 +40,7 @@ import org.jsoup.Jsoup;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element; import org.jsoup.nodes.Element;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod; import org.springframework.http.*;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
@@ -53,6 +51,7 @@ import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.web.util.UriUtils; import org.springframework.web.util.UriUtils;
import java.io.IOException; import java.io.IOException;
import java.math.BigDecimal;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.time.Instant; import java.time.Instant;
@@ -194,7 +193,6 @@ public class MoneyApiController {
@GetMapping({"/market/api/market/money/getStockDetail", "/api/market/money/getStockDetail"}) @GetMapping({"/market/api/market/money/getStockDetail", "/api/market/money/getStockDetail"})
@ResponseBody @ResponseBody
@EncryptFilter(decryptRequest = false) @EncryptFilter(decryptRequest = false)
public ServerResponse getStockDetail(@RequestParam String stockType, @RequestParam String symbol) { public ServerResponse getStockDetail(@RequestParam String stockType, @RequestParam String symbol) {
MoneyStock moneyStock = moneyStockRepository.findOne(QMoneyStockPO.moneyStockPO.stockType.eq(stockType) MoneyStock moneyStock = moneyStockRepository.findOne(QMoneyStockPO.moneyStockPO.stockType.eq(stockType)
.and(QMoneyStockPO.moneyStockPO.moneyScId.eq(symbol)) .and(QMoneyStockPO.moneyStockPO.moneyScId.eq(symbol))
@@ -220,6 +218,54 @@ public class MoneyApiController {
String url = String.format("https://priceapi.moneycontrol.com/pricefeed/%s/equitycash/%s", stockType.toLowerCase(), symbol); String url = String.format("https://priceapi.moneycontrol.com/pricefeed/%s/equitycash/%s", stockType.toLowerCase(), symbol);
int maxRetries = 3; int maxRetries = 3;
if (moneyStock.getUseFromBseindia()) {
String bseUrl = "https://api.bseindia.com/BseIndiaAPI/api/getScripHeaderData/w?Debtflag=&scripcode=" + moneyStock.getNseIndiaId() + "&seriesid=";
HttpHeaders headers = new HttpHeaders();
headers.add("accept", "application/json, text/plain, */*");
headers.add("accept-language", "en-US,en;q=0.9,vi;q=0.8");
headers.add("origin", "https://www.bseindia.com");
headers.add("referer", "https://www.bseindia.com/");
headers.add("sec-ch-ua", "\"Google Chrome\";v=\"131\", \"Chromium\";v=\"131\", \"Not_A Brand\";v=\"24\"");
headers.add("sec-ch-ua-mobile", "?0");
headers.add("sec-ch-ua-platform", "\"Windows\"");
headers.add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36");
HttpEntity<String> entity = new HttpEntity<>(headers);
try {
ResponseEntity<String> responseEntity = restTemplate.exchange(bseUrl, HttpMethod.GET, entity, String.class);
if (responseEntity.getStatusCode().value() == 200 && responseEntity.getBody() != null) {
JSONObject bseData = JSONObject.parseObject(responseEntity.getBody());
JSONObject json1 = new JSONObject();
json1.put("company", bseData.getJSONObject("Cmpname").getString("FullN"));
json1.put("pricepercentchange", bseData.getJSONObject("CurrRate").getString("PcChg"));
json1.put("stockType", stockType);
json1.put("pricechange", bseData.getJSONObject("CurrRate").getString("Chg"));
json1.put("pricecurrent", bseData.getJSONObject("CurrRate").getString("LTP"));
json1.put("priceprevclose", bseData.getJSONObject("Header").getString("PrevClose"));
json1.put("PREVDATE", "");
json1.put("dataSourceType", "3");
json1.put("symbol", symbol);
json1.put("BSEID", moneyStock.getNseIndiaId());
json1.put("NSEID", moneyStock.getNseIndiaId());
json1.put("LTH", bseData.getJSONObject("Header").getString("High"));
json1.put("LTL", bseData.getJSONObject("Header").getString("Low"));
json1.put("OPN", bseData.getJSONObject("Header").getString("Open"));
if (moneyStock != null) {
json1.put("id", moneyStock.getId());
}
json1.put("VOL", this.getVolume(moneyStock.getNseIndiaId()));
return ServerResponse.createBySuccess(json1);
}
} catch (Exception e) {
System.err.println("Error fetching data from BSE India: " + e.getMessage());
}
}
for (int retry = 1; retry <= maxRetries; retry++) { for (int retry = 1; retry <= maxRetries; retry++) {
try { try {
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.GET, null, String.class); ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.GET, null, String.class);
@@ -280,6 +326,33 @@ public class MoneyApiController {
return null; return null;
} }
private String getVolume(String scripcode) {
String url = "https://api.bseindia.com/BseIndiaAPI/api/StockTrading/w?flag=&quotetype=EQ&scripcode=" + scripcode;
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.add("accept", "application/json, text/plain, */*");
headers.add("accept-language", "en-US,en;q=0.9,vi;q=0.8");
headers.add("origin", "https://www.bseindia.com");
headers.add("referer", "https://www.bseindia.com/");
headers.add("sec-ch-ua", "\"Google Chrome\";v=\"131\", \"Chromium\";v=\"131\", \"Not_A Brand\";v=\"24\"");
headers.add("sec-ch-ua-mobile", "?0");
headers.add("sec-ch-ua-platform", "\"Windows\"");
headers.add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36");
HttpEntity<String> entity = new HttpEntity<>(headers);
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
BigDecimal volume = BigDecimal.ZERO;
if (response.getStatusCode().value() == 200 && response.getBody() != null) {
JSONObject volumeData = JSONObject.parseObject(response.getBody());
if (volumeData.getString("TTQ") != null) {
volume = new BigDecimal(volumeData.getString("TTQ"));
}
}
return volume.toString();
}
private static List<MoneyStockSuggestDTO> nseActives() { private static List<MoneyStockSuggestDTO> nseActives() {
List<MoneyStockSuggestDTO> list = new ArrayList<>(); List<MoneyStockSuggestDTO> list = new ArrayList<>();