Merge branch 'develop' into 'main'

Develop

See merge request india/india_market_java!39
This commit is contained in:
Gavin g
2024-09-09 07:57:48 +00:00
3 changed files with 158 additions and 134 deletions

View File

@@ -56,6 +56,10 @@ public class MoneyStockPO {
* 展示表示 */
String selfDispId;
/**
* NSE India的id */
String nseIndiaId;
/**
* 自有self_url */
String selfUrl;

View File

@@ -0,0 +1,97 @@
package cn.stock.market.utils;
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;
public class NseIndiaRequest {
private static final String NSE_INDIA_URL = "https://www.nseindia.com";
private static final OkHttpClient client;
static {
client = new OkHttpClient.Builder()
.cookieJar(new CookieJar() {
private final Map<String, List<Cookie>> cookieStore = new HashMap<>();
@Override
public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
cookieStore.put(url.host(), cookies);
}
@Override
public List<Cookie> loadForRequest(HttpUrl url) {
List<Cookie> cookies = cookieStore.get(url.host());
return cookies != null ? cookies : new ArrayList<Cookie>();
}
})
.build();
}
private static Request createRequest(String url) {
Request request = new Request.Builder()
.url(url)
.header("accept", "application/json, text/plain, */*")
.header("accept-language", "en-US,en;q=0.9")
.header("cache-control", "no-cache")
.header("pragma", "no-cache")
.header("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36")
.build();
return request;
}
private static void initCookie() {
Request request = createRequest(NSE_INDIA_URL);
try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) {
throw new IOException("Failed to fetch initial cookies");
}
} catch (IOException e) {
throw new RuntimeException("Failed to initialize cookies", e);
}
}
public static JSONObject stockByJYSFromHttp(String stockType, String symbol, String nseIndiaId) {
initCookie();
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)
.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());
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);
}
}
}

View File

@@ -7,7 +7,7 @@ import cn.stock.market.domain.basic.repository.MoneyStockRepository;
import cn.stock.market.dto.StockHistoryRequest;
import cn.stock.market.dto.StockHistoryResponse;
import cn.stock.market.infrastructure.db.po.QMoneyStockPO;
import cn.stock.market.utils.RequestCacheUtils;
import cn.stock.market.utils.NseIndiaRequest;
import cn.stock.market.utils.ServerResponse;
import cn.stock.market.web.annotations.EncryptFilter;
import com.alibaba.fastjson.JSONObject;
@@ -21,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;
@@ -40,11 +39,7 @@ import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.logging.Logger;
import java.util.stream.Collectors;
/**
@@ -65,27 +60,6 @@ public class MoneyApiController {
private static final String EXTERNAL_API_URL = "https://priceapi.moneycontrol.com/techCharts/indianMarket/stock/history";
private static final OkHttpClient client;
static {
client = new OkHttpClient.Builder()
.cookieJar(new CookieJar() {
private final Map<String, List<Cookie>> cookieStore = new HashMap<>();
@Override
public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
cookieStore.put(url.host(), cookies);
}
@Override
public List<Cookie> loadForRequest(HttpUrl url) {
List<Cookie> cookies = cookieStore.get(url.host());
return cookies != null ? cookies : new ArrayList<Cookie>();
}
})
.build();
}
@ApiOperation(value = "股票详情信息",httpMethod = "GET")
@ApiImplicitParams({
@ApiImplicitParam(name="stockType",value = "BSE或者NSE"),
@@ -194,12 +168,6 @@ public class MoneyApiController {
@EncryptFilter(decryptRequest = false)
public ServerResponse getStockDetail(@RequestParam String stockType, @RequestParam String symbol ) {
String url = "";
if(symbol.equals("IPL02") || symbol.equals("IPHL")){
url = "https://www.nseindia.com/api/quote-equity?symbol=IPHL";
}else {
url = String.format("https://priceapi.moneycontrol.com/pricefeed/%s/equitycash/%s",stockType,symbol);
}
MoneyStock moneyStock = moneyStockRepository.findOne(QMoneyStockPO.moneyStockPO.stockType.eq(stockType)
.and(QMoneyStockPO.moneyStockPO.moneyScId.eq(symbol))
.and(QMoneyStockPO.moneyStockPO.isLock.eq(0))
@@ -209,122 +177,77 @@ public class MoneyApiController {
return ServerResponse.createByErrorMsg("没有找到该股票");
}*/
// 设置重试次数
if(symbol.equals("IPL02") || symbol.equals("IPHL")){
if("ANI".equals(symbol)){
JSONObject json1 = new JSONObject();
json1.put("company","Archit Nuwood Industries Ltd");
json1.put("pricepercentchange","Archit Nuwood Industries Ltd");
json1.put("stockType",stockType);
json1.put("pricecurrent","386");
json1.put("dataSourceType","3");
json1.put("symbol","ANI");
json1.put("BSEID","ANI");
json1.put("NSEID","ANI");
return ServerResponse.createBySuccess(json1);
}
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 {
String initialUrl = "https://www.nseindia.com/";
Request initialRequest = new Request.Builder()
.url(initialUrl)
.header("accept", "application/json, text/plain, */*")
.header("accept-language", "en-US,en;q=0.9")
.header("cache-control", "no-cache")
.header("pragma", "no-cache")
.header("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36")
.build();
try (Response initialResponse = client.newCall(initialRequest).execute()) {
if (!initialResponse.isSuccessful()) {
throw new IOException("Failed to fetch initial cookies");
}
}
Request request = new Request.Builder()
.url(url)
.header("accept", "application/json, text/plain, */*")
.header("accept-language", "en-US,en;q=0.9")
.header("cache-control", "no-cache")
.header("pragma", "no-cache")
.header("user-agent", "Mozilla/5.0")
.header("referer", "https://www.nseindia.com/")
.header("origin", "https://www.nseindia.com")
.header("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36")
.build();
try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) {
throw new IOException("Request failed with code: " + response.code());
}
// Process the response
String responseBody = response.body().string();
JSONObject jsonData = JSONObject.parseObject(responseBody);
JSONObject json1 = new JSONObject();
JSONObject data =jsonData.getJSONObject("priceInfo");
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","Indian Phosphate Limited");
json1.put("pricepercentchange",data.getString("pChange"));
json1.put("company",data.getString("SC_FULLNM"));
json1.put("pricepercentchange",data.getString("pricepercentchange"));
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("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",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"));
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 (IOException e) {
throw new RuntimeException("Failed to fetch data", e);
}
} catch (IOException e) {
throw new RuntimeException("Failed to initialize cookies", e);
} catch (Exception e) {
}
}else {
int maxRetries = 3;
for (int retry = 1; retry <= maxRetries; retry++) {
// 如果不是最后一次重试,则等待一段时间再进行下一次重试
if (retry < maxRetries) {
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();
}
// 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) {
return null;
}
}
return null;
}