Merge branch 'feature/stock-kline-nseindia' into 'develop'
Add alternal source See merge request india/india_market_java!42
This commit is contained in:
@@ -1,13 +1,15 @@
|
||||
package cn.stock.market.utils;
|
||||
|
||||
import cn.stock.market.dto.StockHistoryRequest;
|
||||
import cn.stock.market.dto.StockHistoryResponse;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import okhttp3.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.Instant;
|
||||
import java.util.*;
|
||||
|
||||
public class NseIndiaRequest {
|
||||
private static final String NSE_INDIA_URL = "https://www.nseindia.com";
|
||||
@@ -93,4 +95,76 @@ public class NseIndiaRequest {
|
||||
throw new RuntimeException("Failed to fetch data", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static StockHistoryResponse stockKLineFromHttp(StockHistoryRequest stockHistoryRequest) {
|
||||
initCookie();
|
||||
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
|
||||
String fromDate = sdf.format(new Date(stockHistoryRequest.getFrom() * 1000));
|
||||
String toDate = sdf.format(new Date(stockHistoryRequest.getTo() * 1000));
|
||||
|
||||
String url = String.format("%s/api/historical/cm/equity?symbol=%s&from=%s&to=%s", NSE_INDIA_URL, stockHistoryRequest.getSymbol(), fromDate, toDate);
|
||||
|
||||
Request request = createRequest(url).newBuilder()
|
||||
.addHeader("referer", NSE_INDIA_URL)
|
||||
.addHeader("origin", NSE_INDIA_URL)
|
||||
.build();
|
||||
|
||||
try (Response response = client.newCall(request).execute()) {
|
||||
if (!response.isSuccessful()) {
|
||||
throw new IOException("Request failed with code: " + response.code());
|
||||
}
|
||||
|
||||
JSONObject jsonData = JSONObject.parseObject(response.body().string());
|
||||
JSONArray data =jsonData.getJSONArray("data");
|
||||
|
||||
StockHistoryResponse result = new StockHistoryResponse();
|
||||
List<Long> tList = new ArrayList<>();
|
||||
List<Double> oList = new ArrayList<>();
|
||||
List<Double> hList = new ArrayList<>();
|
||||
List<Double> lList = new ArrayList<>();
|
||||
List<Double> cList = new ArrayList<>();
|
||||
List<Long> vList = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < data.size(); i++) {
|
||||
Long t, v;
|
||||
Double o, h, l, c;
|
||||
|
||||
try {
|
||||
JSONObject jsonObject = data.getJSONObject(i);
|
||||
|
||||
String timestampStr = jsonObject.getString("TIMESTAMP");
|
||||
Instant instant = Instant.parse(timestampStr);
|
||||
t = instant.toEpochMilli() / 1000;
|
||||
|
||||
o = jsonObject.getDouble("CH_OPENING_PRICE");
|
||||
c = jsonObject.getDouble("CH_CLOSING_PRICE");
|
||||
h = jsonObject.getDouble("CH_TRADE_HIGH_PRICE");
|
||||
l = jsonObject.getDouble("CH_TRADE_LOW_PRICE");
|
||||
v = jsonObject.getLong("CH_TOT_TRADED_VAL");
|
||||
|
||||
} catch (Exception e) {
|
||||
continue;
|
||||
}
|
||||
|
||||
tList.add(t);
|
||||
oList.add(o);
|
||||
hList.add(h);
|
||||
lList.add(l);
|
||||
cList.add(c);
|
||||
vList.add(v);
|
||||
}
|
||||
|
||||
result.setT(tList);
|
||||
result.setO(oList);
|
||||
result.setH(hList);
|
||||
result.setL(lList);
|
||||
result.setC(cList);
|
||||
result.setV(vList);
|
||||
|
||||
return result;
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Failed to fetch data", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -745,6 +745,24 @@ public class MoneyApiController {
|
||||
// API request successful, return the response
|
||||
return ResponseEntity.ok(response);
|
||||
} else {
|
||||
if (!StringUtils.equals("H", resolution)) {
|
||||
try {
|
||||
MoneyStock moneyStock = moneyStockRepository.findOne((QMoneyStockPO.moneyStockPO.moneyScId.eq(symbol))
|
||||
.and(QMoneyStockPO.moneyStockPO.isLock.eq(0))
|
||||
.and(QMoneyStockPO.moneyStockPO.isShow.eq(0)))
|
||||
.orElse(null);
|
||||
|
||||
if (moneyStock != null && moneyStock.getNseIndiaId() != null && !moneyStock.getNseIndiaId().isEmpty()) {
|
||||
request.setSymbol(moneyStock.getNseIndiaId());
|
||||
response = NseIndiaRequest.stockKLineFromHttp(request);
|
||||
return ResponseEntity.ok(response);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to get data from nseindia.", e.getMessage());
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
|
||||
}
|
||||
}
|
||||
|
||||
// All retries failed, return an error response
|
||||
log.error("Failed to get a successful response after {} retries.", maxRetries);
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
|
||||
|
||||
Reference in New Issue
Block a user