Merge branch 'refactor/optimization' into 'develop'
Optimize See merge request india/india_market_java!38
This commit is contained in:
@@ -1,24 +0,0 @@
|
||||
package cn.stock.market.constant;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum NseIndiaSymbol {
|
||||
IPL02("IPL02"),
|
||||
IPHL("IPHL");
|
||||
|
||||
private String symbol;
|
||||
|
||||
public String getSymbol() {
|
||||
return symbol;
|
||||
}
|
||||
|
||||
public static Boolean contains(String symbol) {
|
||||
return Arrays.stream(NseIndiaSymbol.values()).anyMatch(e -> e.getSymbol().equals(symbol));
|
||||
}
|
||||
}
|
||||
@@ -56,6 +56,10 @@ public class MoneyStockPO {
|
||||
* 展示表示 */
|
||||
String selfDispId;
|
||||
|
||||
/**
|
||||
* NSE India的id */
|
||||
String nseIndiaId;
|
||||
|
||||
/**
|
||||
* 自有self_url */
|
||||
String selfUrl;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package cn.stock.market.utils;
|
||||
|
||||
import cn.stock.market.constant.NseIndiaSymbol;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import okhttp3.*;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -56,10 +56,10 @@ public class NseIndiaRequest {
|
||||
}
|
||||
}
|
||||
|
||||
public static String fetchData() {
|
||||
public static JSONObject stockByJYSFromHttp(String stockType, String symbol, String nseIndiaId) {
|
||||
initCookie();
|
||||
|
||||
String url = NSE_INDIA_URL + "/api/quote-equity?symbol=" + NseIndiaSymbol.IPHL.getSymbol();
|
||||
String url = NSE_INDIA_URL + "/api/quote-equity?symbol=" + nseIndiaId;
|
||||
Request request = createRequest(url).newBuilder()
|
||||
.addHeader("referer", NSE_INDIA_URL)
|
||||
.addHeader("origin", NSE_INDIA_URL)
|
||||
@@ -68,7 +68,28 @@ public class NseIndiaRequest {
|
||||
if (!response.isSuccessful()) {
|
||||
throw new IOException("Request failed with code: " + response.code());
|
||||
}
|
||||
return response.body().string();
|
||||
|
||||
JSONObject jsonData = JSONObject.parseObject(response.body().string());
|
||||
JSONObject data =jsonData.getJSONObject("priceInfo");
|
||||
JSONObject json = new JSONObject();
|
||||
|
||||
json.put("company","Indian Phosphate Limited");
|
||||
json.put("pricepercentchange",data.getString("pChange"));
|
||||
json.put("stockType",stockType);
|
||||
json.put("pricechange",data.getString("change"));
|
||||
json.put("pricecurrent",data.getString("lastPrice"));
|
||||
json.put("priceprevclose",data.getString("previousClose"));
|
||||
json.put("PREVDATE","");
|
||||
json.put("VOL",jsonData.getJSONObject("preOpenMarket").getString("totalTradedVolume"));
|
||||
json.put("dataSourceType","3");
|
||||
json.put("symbol",symbol);
|
||||
json.put("BSEID",symbol);
|
||||
json.put("NSEID",symbol);
|
||||
json.put("LTH",data.getString("upperCP"));
|
||||
json.put("LTL",data.getString("lowerCP"));
|
||||
json.put("OPN",data.getString("open"));
|
||||
|
||||
return json;
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Failed to fetch data", e);
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package cn.stock.market.web;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.stock.market.MoneyStockSuggestDTO;
|
||||
import cn.stock.market.constant.NseIndiaSymbol;
|
||||
import cn.stock.market.domain.basic.entity.MoneyStock;
|
||||
import cn.stock.market.domain.basic.repository.MoneyStockRepository;
|
||||
import cn.stock.market.dto.StockHistoryRequest;
|
||||
@@ -22,7 +21,6 @@ import io.swagger.annotations.ApiOperation;
|
||||
import io.swagger.annotations.ApiResponse;
|
||||
import io.swagger.annotations.ApiResponses;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import okhttp3.*;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.jsoup.Jsoup;
|
||||
@@ -179,85 +177,62 @@ public class MoneyApiController {
|
||||
return ServerResponse.createByErrorMsg("没有找到该股票");
|
||||
}*/
|
||||
// 设置重试次数
|
||||
if(NseIndiaSymbol.contains(symbol)){
|
||||
String url = String.format("https://priceapi.moneycontrol.com/pricefeed/%s/equitycash/%s",stockType,symbol);
|
||||
int maxRetries = 3;
|
||||
for (int retry = 1; retry <= maxRetries; retry++) {
|
||||
try {
|
||||
// Process the response
|
||||
String responseBody = NseIndiaRequest.fetchData();
|
||||
JSONObject jsonData = JSONObject.parseObject(responseBody);
|
||||
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.GET, null, String.class);
|
||||
JSONObject json1 = new JSONObject();
|
||||
|
||||
JSONObject data =jsonData.getJSONObject("priceInfo");
|
||||
if(data!=null){
|
||||
json1.put("company","Indian Phosphate Limited");
|
||||
json1.put("pricepercentchange",data.getString("pChange"));
|
||||
json1.put("stockType",stockType);
|
||||
json1.put("pricechange",data.getString("change"));
|
||||
json1.put("pricecurrent",data.getString("lastPrice"));
|
||||
json1.put("priceprevclose",data.getString("previousClose"));
|
||||
json1.put("PREVDATE","");
|
||||
json1.put("VOL",jsonData.getJSONObject("preOpenMarket").getString("totalTradedVolume"));
|
||||
json1.put("dataSourceType","3");
|
||||
json1.put("symbol",symbol);
|
||||
json1.put("BSEID",symbol);
|
||||
json1.put("NSEID",symbol);
|
||||
json1.put("LTH",data.getString("upperCP"));
|
||||
json1.put("LTL",data.getString("lowerCP"));
|
||||
json1.put("OPN",data.getString("open"));
|
||||
if(null!=moneyStock){
|
||||
json1.put("id",moneyStock.getId());
|
||||
if (responseEntity.getStatusCode().value() == 200 && responseEntity.getBody() != null ) {
|
||||
JSONObject data = JSONObject.parseObject(responseEntity.getBody()).getJSONObject("data");
|
||||
if(data!=null){
|
||||
json1.put("company",data.getString("SC_FULLNM"));
|
||||
json1.put("pricepercentchange",data.getString("pricepercentchange"));
|
||||
json1.put("stockType",stockType);
|
||||
json1.put("pricechange",data.getString("pricechange"));
|
||||
json1.put("pricecurrent",data.getString("pricecurrent"));
|
||||
json1.put("priceprevclose",data.getString("priceprevclose"));
|
||||
json1.put("PREVDATE",data.getString("PREVDATE"));
|
||||
json1.put("VOL",data.getString("VOL"));
|
||||
json1.put("dataSourceType","3");
|
||||
json1.put("symbol",data.getString("symbol"));
|
||||
json1.put("BSEID",data.getString("BSEID"));
|
||||
json1.put("NSEID",data.getString("NSEID"));
|
||||
json1.put("LTH",data.getString("HP"));
|
||||
json1.put("LTL",data.getString("LP"));
|
||||
json1.put("OPN",data.getString("OPN"));
|
||||
if(null!=moneyStock){
|
||||
json1.put("id",moneyStock.getId());
|
||||
}
|
||||
if(StringUtils.equals(data.getString("pricecurrent"),"0.00")
|
||||
&& (!StringUtils.equals(data.getString("priceprevclose"),"0.00"))){
|
||||
json1.put("pricecurrent",data.getString("priceprevclose"));
|
||||
}
|
||||
}
|
||||
return ServerResponse.createBySuccess(json1);
|
||||
}
|
||||
return ServerResponse.createBySuccess(json1);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
// 如果不是最后一次重试,则等待一段时间再进行下一次重试
|
||||
if (retry < maxRetries) {
|
||||
try {
|
||||
// 1秒钟
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
}
|
||||
if (moneyStock != null && moneyStock.getNseIndiaId() != null && !moneyStock.getNseIndiaId().isEmpty()) {
|
||||
try {
|
||||
// Get data from nseindia
|
||||
JSONObject json = NseIndiaRequest.stockByJYSFromHttp(stockType, symbol, moneyStock.getNseIndiaId());
|
||||
json.put("id",moneyStock.getId());
|
||||
|
||||
return ServerResponse.createBySuccess(json);
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Failed to fetch data", e);
|
||||
}
|
||||
}else {
|
||||
String url = String.format("https://priceapi.moneycontrol.com/pricefeed/%s/equitycash/%s",stockType,symbol);
|
||||
int maxRetries = 3;
|
||||
for (int retry = 1; retry <= maxRetries; retry++) {
|
||||
try {
|
||||
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.GET, null, String.class);
|
||||
JSONObject json1 = new JSONObject();
|
||||
if (responseEntity.getStatusCode().value() == 200 && responseEntity.getBody() != null ) {
|
||||
JSONObject data = JSONObject.parseObject(responseEntity.getBody()).getJSONObject("data");
|
||||
if(data!=null){
|
||||
json1.put("company",data.getString("SC_FULLNM"));
|
||||
json1.put("pricepercentchange",data.getString("pricepercentchange"));
|
||||
json1.put("stockType",stockType);
|
||||
json1.put("pricechange",data.getString("pricechange"));
|
||||
json1.put("pricecurrent",data.getString("pricecurrent"));
|
||||
json1.put("priceprevclose",data.getString("priceprevclose"));
|
||||
json1.put("PREVDATE",data.getString("PREVDATE"));
|
||||
json1.put("VOL",data.getString("VOL"));
|
||||
json1.put("dataSourceType","3");
|
||||
json1.put("symbol",data.getString("symbol"));
|
||||
json1.put("BSEID",data.getString("BSEID"));
|
||||
json1.put("NSEID",data.getString("NSEID"));
|
||||
json1.put("LTH",data.getString("HP"));
|
||||
json1.put("LTL",data.getString("LP"));
|
||||
json1.put("OPN",data.getString("OPN"));
|
||||
if(null!=moneyStock){
|
||||
json1.put("id",moneyStock.getId());
|
||||
}
|
||||
if(StringUtils.equals(data.getString("pricecurrent"),"0.00")
|
||||
&& (!StringUtils.equals(data.getString("priceprevclose"),"0.00"))){
|
||||
json1.put("pricecurrent",data.getString("priceprevclose"));
|
||||
}
|
||||
}
|
||||
return ServerResponse.createBySuccess(json1);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
}
|
||||
// 如果不是最后一次重试,则等待一段时间再进行下一次重试
|
||||
if (retry < maxRetries) {
|
||||
try {
|
||||
// 1秒钟
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user