印度--获取指定指数信息接口getIndiaIndexByToday.do: BToday添加兜底数据源Growwin
This commit is contained in:
@@ -1,51 +1,33 @@
|
|||||||
package cn.stock.market.domain.basic.service;
|
package cn.stock.market.domain.basic.service;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.net.URL;
|
|
||||||
import java.net.URLConnection;
|
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import javax.net.ssl.X509TrustManager;
|
|
||||||
import javax.persistence.EntityExistsException;
|
import javax.persistence.EntityExistsException;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.sound.midi.Soundbank;
|
|
||||||
|
|
||||||
import cn.hutool.core.bean.BeanUtil;
|
|
||||||
import cn.hutool.core.text.StrFormatter;
|
import cn.hutool.core.text.StrFormatter;
|
||||||
import cn.stock.market.dto.model.*;
|
import cn.stock.market.dto.model.*;
|
||||||
|
import cn.stock.market.infrastructure.api.GrowwInApis;
|
||||||
import cn.stock.market.infrastructure.api.TodayApis;
|
import cn.stock.market.infrastructure.api.TodayApis;
|
||||||
import cn.stock.market.infrastructure.api.investing.IndiaIndexVo;
|
import cn.stock.market.infrastructure.api.investing.IndiaIndexVo;
|
||||||
import cn.stock.market.infrastructure.api.investing.IndiaStockVO;
|
import cn.stock.market.infrastructure.api.investing.IndiaStockVO;
|
||||||
import cn.stock.market.infrastructure.api.investing.InvestingApis;
|
import cn.stock.market.infrastructure.api.investing.InvestingApis;
|
||||||
import cn.stock.market.infrastructure.api.investing.InvestingInvokerApis;
|
import cn.stock.market.infrastructure.api.investing.InvestingInvokerApis;
|
||||||
import cn.stock.market.infrastructure.api.sina.vo.HotSearchVO;
|
import cn.stock.market.infrastructure.api.sina.vo.HotSearchVO;
|
||||||
import cn.stock.market.utils.*;
|
|
||||||
import com.ag.utils.CollectionUtils;
|
import com.ag.utils.CollectionUtils;
|
||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONArray;
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.google.common.base.Stopwatch;
|
import com.google.common.base.Stopwatch;
|
||||||
import okhttp3.OkHttpClient;
|
|
||||||
import okhttp3.Protocol;
|
|
||||||
import okhttp3.Request;
|
|
||||||
import okhttp3.Request.Builder;
|
import okhttp3.Request.Builder;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import cn.stock.market.infrastructure.db.po.QStockPO;
|
import cn.stock.market.infrastructure.db.po.QStockPO;
|
||||||
import org.apache.http.HttpResponse;
|
|
||||||
import org.apache.http.client.HttpClient;
|
|
||||||
import org.apache.http.client.methods.HttpGet;
|
|
||||||
import org.apache.http.client.utils.HttpClientUtils;
|
|
||||||
import org.apache.http.impl.client.HttpClients;
|
|
||||||
import org.apache.http.util.EntityUtils;
|
|
||||||
import org.jsoup.Jsoup;
|
import org.jsoup.Jsoup;
|
||||||
import org.jsoup.nodes.Document;
|
import org.jsoup.nodes.Document;
|
||||||
import org.jsoup.nodes.Element;
|
|
||||||
import org.jsoup.select.Elements;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.http.HttpEntity;
|
import org.springframework.http.HttpEntity;
|
||||||
@@ -1008,7 +990,12 @@ public class StockService {
|
|||||||
vo1.setKLine(kine);
|
vo1.setKLine(kine);
|
||||||
indexVoList.add(vo1);
|
indexVoList.add(vo1);
|
||||||
}catch (Exception e){
|
}catch (Exception e){
|
||||||
log.info("获取NIFTY50指数数据异常,异常信息{}。。。。",e.getMessage());
|
log.info("BToday获取BSESENSEX指数数据异常,异常信息。。。。", e);
|
||||||
|
try {
|
||||||
|
GrowwInApis.requestSenSexData(indexVoList);
|
||||||
|
} catch (Exception e1) {
|
||||||
|
log.info("GrowwIn获取BSESENSEX指数数据异常,异常信息。。。。", e1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -1028,7 +1015,12 @@ public class StockService {
|
|||||||
vo1.setKLine(kine);
|
vo1.setKLine(kine);
|
||||||
indexVoList.add(vo1);
|
indexVoList.add(vo1);
|
||||||
}catch (Exception e){
|
}catch (Exception e){
|
||||||
log.info("获取NIFTY50指数数据异常,异常信息{}。。。。",e.getMessage());
|
log.info("BToday获取NIFTY50指数数据异常,异常信息。。。。", e);
|
||||||
|
try {
|
||||||
|
GrowwInApis.requestNifty50Data(indexVoList);
|
||||||
|
} catch (Exception e1) {
|
||||||
|
log.info("GrowwIn获取NIFTY50指数数据异常,异常信息。。。。", e1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return ServerResponse.createBySuccess(indexVoList);
|
return ServerResponse.createBySuccess(indexVoList);
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,233 @@
|
|||||||
|
package cn.stock.market.infrastructure.api;
|
||||||
|
|
||||||
|
import cn.stock.market.dto.TodayStockDTO;
|
||||||
|
import cn.stock.market.infrastructure.api.investing.IndiaIndexVo;
|
||||||
|
import cn.stock.market.infrastructure.api.investing.IndiaStockVO;
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import com.alibaba.fastjson.JSONArray;
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import org.apache.http.HttpEntity;
|
||||||
|
import org.apache.http.client.config.RequestConfig;
|
||||||
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
|
import org.apache.http.client.methods.HttpGet;
|
||||||
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
|
import org.apache.http.impl.client.HttpClients;
|
||||||
|
import org.apache.http.util.EntityUtils;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 数据来源网站:<a href="https://groww.in/indices/nifty">...</a>, <a href="https://groww.in/indices/sp-bse-sensex">...</a>
|
||||||
|
*
|
||||||
|
* @auther jnerh
|
||||||
|
*/
|
||||||
|
public class GrowwInApis {
|
||||||
|
|
||||||
|
public static final String INDEX_DETAIL_URL = "https://groww.in/v1/api/stocks_data/v1/accord_points/exchange/%s/segment/CASH/latest_indices_ohlc/%s";
|
||||||
|
public static final String INDEX_KLINE_URL = "https://groww.in/v1/api/charting_service/v2/chart/delayed/exchange/%s/segment/CASH/%s/daily?intervalInMinutes=%s&minimal=true";
|
||||||
|
|
||||||
|
|
||||||
|
public static void requestSenSexData(List<IndiaIndexVo> indexVoList) {
|
||||||
|
IndiaIndexVo vo1 = new IndiaIndexVo();
|
||||||
|
JSONObject object = GrowwInApis.getSenSexDetail();
|
||||||
|
IndiaStockVO market = GrowwInApis.objToVo(object);
|
||||||
|
market.setName("BSESENSEX指数");
|
||||||
|
vo1.setIndexVo(market);
|
||||||
|
|
||||||
|
List kine = GrowwInApis.getSenSexKline();
|
||||||
|
vo1.setKLine(kine);
|
||||||
|
indexVoList.add(vo1);
|
||||||
|
}
|
||||||
|
public static void requestNifty50Data(List<IndiaIndexVo> indexVoList) {
|
||||||
|
IndiaIndexVo vo1 = new IndiaIndexVo();
|
||||||
|
JSONObject object = GrowwInApis.getNifty50Detail();
|
||||||
|
IndiaStockVO market = GrowwInApis.objToVo(object);
|
||||||
|
market.setName("NIFTY50指数");
|
||||||
|
vo1.setIndexVo(market);
|
||||||
|
|
||||||
|
List kine = GrowwInApis.getNifty50Kline();
|
||||||
|
vo1.setKLine(kine);
|
||||||
|
indexVoList.add(vo1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取Nifty50指数详情
|
||||||
|
*/
|
||||||
|
public static JSONObject getIndexDetail(String exchange, String type) {
|
||||||
|
String url = String.format(INDEX_DETAIL_URL, exchange, type);
|
||||||
|
String str = get(url);
|
||||||
|
return JSON.parseObject(str);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取k线图
|
||||||
|
*/
|
||||||
|
public static List getIndexKline(String exchange, String type, String intervalInMinutes) {
|
||||||
|
String url = String.format(INDEX_KLINE_URL, exchange, type, intervalInMinutes);
|
||||||
|
String str = get(url);
|
||||||
|
JSONObject object = JSON.parseObject(str);
|
||||||
|
return getList(object.getString("candles"));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取Nifty50指数详情
|
||||||
|
*/
|
||||||
|
public static JSONObject getSenSexDetail() {
|
||||||
|
return getIndexDetail("BSE", "1");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取k线图
|
||||||
|
*/
|
||||||
|
public static List getSenSexKline() {
|
||||||
|
return getIndexKline("BSE", "1", "1");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取Nifty50指数详情
|
||||||
|
*/
|
||||||
|
public static JSONObject getNifty50Detail() {
|
||||||
|
return getIndexDetail("NSE", "NIFTY");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取k线图
|
||||||
|
*/
|
||||||
|
public static List getNifty50Kline() {
|
||||||
|
return getIndexKline("NSE", "NIFTY", "5");
|
||||||
|
}
|
||||||
|
|
||||||
|
static String get(String url) {
|
||||||
|
CloseableHttpClient httpClient = null;
|
||||||
|
CloseableHttpResponse response = null;
|
||||||
|
String result = "";
|
||||||
|
|
||||||
|
try {
|
||||||
|
httpClient = HttpClients.createDefault();
|
||||||
|
HttpGet httpGet = new HttpGet(url);
|
||||||
|
httpGet.setHeader("Referer", "https://groww.in/indices/nifty");
|
||||||
|
httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36 Edg/121.0.0.0");
|
||||||
|
httpGet.setHeader("X-App-Id", "growwWeb");
|
||||||
|
httpGet.setHeader("X-Device-Id", "fd4815a2-49e4-570a-931a-0316d4d8a52d");
|
||||||
|
httpGet.setHeader("X-Device-Type", "desktop");
|
||||||
|
httpGet.setHeader("X-Platform", "web");
|
||||||
|
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000).setConnectionRequestTimeout(35000).setSocketTimeout(60000).build();
|
||||||
|
httpGet.setConfig(requestConfig);
|
||||||
|
response = httpClient.execute(httpGet);
|
||||||
|
HttpEntity entity = response.getEntity();
|
||||||
|
result = EntityUtils.toString(entity);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
|
||||||
|
if (null != response) {
|
||||||
|
try {
|
||||||
|
response.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (null != httpClient) {
|
||||||
|
try {
|
||||||
|
httpClient.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IndiaStockVO objToVo(JSONObject object) {
|
||||||
|
IndiaStockVO market = new IndiaStockVO();
|
||||||
|
if (object.containsKey("close")) {
|
||||||
|
market.setClose(object.getString("close"));
|
||||||
|
}
|
||||||
|
if (object.containsKey("value")) {
|
||||||
|
market.setNowPrice(object.getString("value"));
|
||||||
|
}
|
||||||
|
if (object.containsKey("dayChangePerc")) {
|
||||||
|
market.setRate(object.getString("dayChangePerc"));
|
||||||
|
}
|
||||||
|
if (object.containsKey("high")) {
|
||||||
|
market.setHigh(object.getString("high"));
|
||||||
|
}
|
||||||
|
if (object.containsKey("low")) {
|
||||||
|
market.setLow(object.getString("low"));
|
||||||
|
}
|
||||||
|
if (object.containsKey("open")) {
|
||||||
|
market.setOpen(object.getString("open"));
|
||||||
|
}
|
||||||
|
|
||||||
|
return market;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List getList(String json) {
|
||||||
|
List<JSONObject> dataObject = new ArrayList<>();
|
||||||
|
JSONArray jsonArray = JSON.parseArray(json);
|
||||||
|
for (Object obj : jsonArray) {
|
||||||
|
JSONObject entity = new JSONObject();
|
||||||
|
JSONArray jsonArray1 = JSON.parseArray(obj.toString());
|
||||||
|
entity.put("upd_date", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(JSON.parseArray(obj.toString()).getLong(0) * 1000)).replace(" ", "T"));
|
||||||
|
entity.put("price", jsonArray1.getBigDecimal(1));
|
||||||
|
dataObject.add(entity);
|
||||||
|
}
|
||||||
|
return dataObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static TodayStockDTO todayStockDTO(JSONObject jsonObject) {
|
||||||
|
TodayStockDTO dto = new TodayStockDTO();
|
||||||
|
if (jsonObject.containsKey("exchange")) {
|
||||||
|
dto.setStockType(jsonObject.getString("exchange"));
|
||||||
|
} else {
|
||||||
|
dto.setStockType("");
|
||||||
|
}
|
||||||
|
if (jsonObject.containsKey("lname")) {
|
||||||
|
dto.setStockName(jsonObject.getString("lname"));
|
||||||
|
} else {
|
||||||
|
dto.setStockName("");
|
||||||
|
}
|
||||||
|
if (jsonObject.containsKey("high_price")) {
|
||||||
|
dto.setHighPrice(jsonObject.getString("highPrice"));
|
||||||
|
} else {
|
||||||
|
dto.setHighPrice("0.00");
|
||||||
|
}
|
||||||
|
if (jsonObject.containsKey("low_price")) {
|
||||||
|
dto.setLowPrice(jsonObject.getString("low_price"));
|
||||||
|
} else {
|
||||||
|
dto.setLowPrice("0.00");
|
||||||
|
}
|
||||||
|
if (jsonObject.containsKey("price")) {
|
||||||
|
dto.setLastPrice(jsonObject.getString("price"));
|
||||||
|
} else {
|
||||||
|
dto.setLastPrice("0.00");
|
||||||
|
}
|
||||||
|
if (jsonObject.containsKey("close_price")) {
|
||||||
|
dto.setPrevClosePrice(jsonObject.getString("close_price"));
|
||||||
|
} else {
|
||||||
|
dto.setPrevClosePrice("0.00");
|
||||||
|
}
|
||||||
|
if (jsonObject.containsKey("netchg")) {
|
||||||
|
dto.setChange(jsonObject.getString("netchg"));
|
||||||
|
} else {
|
||||||
|
dto.setChange("0.00");
|
||||||
|
}
|
||||||
|
if (jsonObject.containsKey("perchg")) {
|
||||||
|
dto.setChangePercent(jsonObject.getString("perchg"));
|
||||||
|
} else {
|
||||||
|
dto.setChangePercent("0.00");
|
||||||
|
}
|
||||||
|
return dto;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println(getNifty50Detail());
|
||||||
|
System.out.println(getNifty50Kline());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user