fix:增加today指数信息

This commit is contained in:
xiaoliuhu
2024-02-23 15:07:23 +08:00
parent 4df20a07a0
commit 77c436df1d
4 changed files with 254 additions and 32 deletions

View File

@@ -13,10 +13,12 @@ import java.util.concurrent.TimeUnit;
import javax.net.ssl.X509TrustManager;
import javax.persistence.EntityExistsException;
import javax.servlet.http.HttpServletRequest;
import javax.sound.midi.Soundbank;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.text.StrFormatter;
import cn.stock.market.dto.model.*;
import cn.stock.market.infrastructure.api.TodayApis;
import cn.stock.market.infrastructure.api.investing.IndiaIndexVo;
import cn.stock.market.infrastructure.api.investing.IndiaStockVO;
import cn.stock.market.infrastructure.api.investing.InvestingApis;
@@ -980,4 +982,85 @@ public class StockService {
}
return ServerResponse.createBySuccess(indexVoList);
}
public ServerResponse getIndexByBtoday(){
List<IndiaIndexVo> indexVoList = new ArrayList<>();
try {
String exchange = "nse";
IndiaIndexVo vo1 = new IndiaIndexVo();
String coCode = "20558";
JSONObject object = TodayApis.getStockDetail(exchange, coCode);
IndiaStockVO market = objToVo(object);
market.setName("BSESENSEX指数");
vo1.setIndexVo(market);
//获取k线图 1D 当天的数据
String format = "S";
String durationType = "D";
String duration = "1";
List kine = TodayApis.getStockKline(exchange,coCode,format,durationType,duration);
vo1.setKLine(kine);
indexVoList.add(vo1);
}catch (Exception e){
log.info("获取NIFTY50指数数据异常异常信息{}。。。。",e.getMessage());
}
try {
String exchange = "nse";
IndiaIndexVo vo1 = new IndiaIndexVo();
String coCode = "20559";
JSONObject object = TodayApis.getStockDetail(exchange, coCode);
IndiaStockVO market = objToVo(object);
market.setName("NIFTY50指数");
vo1.setIndexVo(market);
//获取k线图 1D 当天的数据
String format = "S";
String durationType = "D";
String duration = "1";
List kine = TodayApis.getStockKline(exchange,coCode,format,durationType,duration);
vo1.setKLine(kine);
indexVoList.add(vo1);
}catch (Exception e){
log.info("获取NIFTY50指数数据异常异常信息{}。。。。",e.getMessage());
}
return ServerResponse.createBySuccess(indexVoList);
}
private IndiaStockVO objToVo(JSONObject object){
IndiaStockVO market = new IndiaStockVO();
if(object.containsKey("prev_close")){
market.setClose(object.getString("prev_close"));
}
if(object.containsKey("price")){
market.setNowPrice(object.getString("price"));
}
if(object.containsKey("perchg")){
market.setRate(object.getString("perchg"));
}
if(object.containsKey("high_price")){
market.setHigh(object.getString("high_price"));
}
if(object.containsKey("low_price")){
market.setLow(object.getString("low_price"));
}
if(object.containsKey("open_price")){
market.setOpen(object.getString("open_price"));
}
return market;
// stockListVO.setName(jsonObject.getString("companyname"));
// stockListVO.setCode(jsonObject.getString("co_code"));
// stockListVO.setSpell(jsonObject.getString("symbol"));
// stockListVO.setGid(jsonObject.getString("co_code"));
// stockListVO.setNowPrice(jsonObject.getString("price"));
// stockListVO.setHcrate(jsonObject.getBigDecimal("perchg"));
// stockListVO.setToday_max(jsonObject.getString("high_price"));
// stockListVO.setToday_min(jsonObject.getString("low_price"));
// stockListVO.setBusiness_balance(jsonObject.getString("val_traded"));
// stockListVO.setBusiness_amount(jsonObject.getString("vol_traded"));
// stockListVO.setPreclose_px(jsonObject.getString("prev_close"));
// stockListVO.setOpen_px(jsonObject.getString("open_price"));
}
}

View File

@@ -0,0 +1,18 @@
package cn.stock.market.dto;
import lombok.Data;
@Data
public class TodayStockDTO {
private String stockType;
private String stockName;
private String stockUrl;
private String highPrice;
private String lowPrice;
private String lastPrice;
private String prevClosePrice;
private String change;
private String changePercent;
private String dispId;
private String scId;
}

View File

@@ -1,5 +1,6 @@
package cn.stock.market.infrastructure.api;
import cn.stock.market.dto.TodayStockDTO;
import cn.stock.market.utils.HttpClientRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
@@ -20,7 +21,7 @@ public class TodayApis {
}
//获取Top Gainers
public static List<JSONObject> getTopGainers(String exchange){
public static List getTopGainers(String exchange){
String url = "https://marketapi.intoday.in/widget/topgainer/view?exchange=" + exchange;
//返回字符串对象格式:{"status_code":1,"success":true,"data":[{}],"message":"Successful","fromredis":true}
String str = get(url);
@@ -32,17 +33,19 @@ public class TodayApis {
if(!bool){
return null;
}
return getList(object.getString("data"),1);
//进行数据转换
List<JSONObject> dataObject = new ArrayList<>();
JSONArray jsonArray = JSON.parseArray(object.getString("data"));
for (Object obj : jsonArray){
JSONObject entity = JSON.parseObject(obj.toString());
dataObject.add(entity);
}
return dataObject;
// List<TodayStockDTO> dataObject = new ArrayList<>();
// JSONArray jsonArray = JSON.parseArray(object.getString("data"));
// for (Object obj : jsonArray){
//// JSONObject entity = JSON.parseObject(obj.toString());
// TodayStockDTO entity = todayStockDTO(JSON.parseObject(obj.toString()));
// dataObject.add(entity);
// }
// return dataObject;
}
//获取Top Losers
public static List<JSONObject> getTopLosers(String exchange){
public static List getTopLosers(String exchange){
String url = "https://marketapi.intoday.in/widget/toploser/view?exchange=" + exchange;
//返回字符串对象格式:{"status_code":1,"success":true,"data":[{}],"message":"Successful","fromredis":true}
String str = get(url);
@@ -54,17 +57,20 @@ public class TodayApis {
if(!bool){
return null;
}
return getList(object.getString("data"),1);
//进行数据转换
List<JSONObject> dataObject = new ArrayList<>();
JSONArray jsonArray = JSON.parseArray(object.getString("data"));
for (Object obj : jsonArray){
JSONObject entity = JSON.parseObject(obj.toString());
dataObject.add(entity);
}
return dataObject;
//// List<TodayStockDTO> dataObject = new ArrayList<>();
// List<JSONObject> dataObject = new ArrayList<>();
// JSONArray jsonArray = JSON.parseArray(object.getString("data"));
// for (Object obj : jsonArray){
// JSONObject entity = JSON.parseObject(obj.toString());
//// TodayStockDTO entity = todayStockDTO(JSON.parseObject(obj.toString()));
// dataObject.add(entity);
// }
// return dataObject;
}
//获取Most Active Volume
public static List<JSONObject> getMostActiveVolume(String exchange){
public static List getMostActiveVolume(String exchange){
String url = "https://marketapi.intoday.in/widget/mostactivetopper/view?exchange=" + exchange;
//返回字符串对象格式:{"status_code":1,"success":true,"data":[{}],"message":"Successful","fromredis":true}
String str = get(url);
@@ -76,17 +82,19 @@ public class TodayApis {
if(!bool){
return null;
}
return getList(object.getString("data"),1);
//进行数据转换
List<JSONObject> dataObject = new ArrayList<>();
JSONArray jsonArray = JSON.parseArray(object.getString("data"));
for (Object obj : jsonArray){
JSONObject entity = JSON.parseObject(obj.toString());
dataObject.add(entity);
}
return dataObject;
// List<TodayStockDTO> dataObject = new ArrayList<>();
// JSONArray jsonArray = JSON.parseArray(object.getString("data"));
// for (Object obj : jsonArray){
//// JSONObject entity = JSON.parseObject(obj.toString());
// TodayStockDTO entity = todayStockDTO(JSON.parseObject(obj.toString()));
// dataObject.add(entity);
// }
// return dataObject;
}
//获取Most Active Value
public static List<JSONObject> getMostActiveValue(String exchange){
public static List getMostActiveValue(String exchange){
String url = "https://marketapi.intoday.in/widget/mostactivetopperbyvalue/view?exchange=" + exchange;
//返回字符串对象格式:{"status_code":1,"success":true,"data":[{}],"message":"Successful","fromredis":true}
String str = get(url);
@@ -98,14 +106,117 @@ public class TodayApis {
if(!bool){
return null;
}
//进行数据转换
return getList(object.getString("data"),1);
// //进行数据转换
// List<TodayStockDTO> dataObject = new ArrayList<>();
// JSONArray jsonArray = JSON.parseArray(object.getString("data"));
// for (Object obj : jsonArray){
//// JSONObject entity = JSON.parseObject(obj.toString());
// TodayStockDTO entity = todayStockDTO(JSON.parseObject(obj.toString()));
// dataObject.add(entity);
// }
// return dataObject;
}
private static List getList(String json, int type){
if(type == 1){
List<JSONObject> dataObject = new ArrayList<>();
JSONArray jsonArray = JSON.parseArray(object.getString("data"));
JSONArray jsonArray = JSON.parseArray(json);
for (Object obj : jsonArray){
JSONObject entity = JSON.parseObject(obj.toString());
dataObject.add(entity);
}
return dataObject;
}else{
//进行数据转换
List<TodayStockDTO> dataObject = new ArrayList<>();
JSONArray jsonArray = JSON.parseArray(json);
for (Object obj : jsonArray){
TodayStockDTO entity = todayStockDTO(JSON.parseObject(obj.toString()));
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 JSONObject getStockDetail(String exchange,String coCode){
String url = String.format("https://marketapi.intoday.in/widget/stockdetail/pullview?co_code=%s&exchange=%s",coCode,exchange);
String str = get(url);
JSONObject object = JSON.parseObject(str);
boolean bool = false;
if(object.containsKey("success")){
bool = object.getBoolean("success");
}
if(!bool){
return new JSONObject();
}
JSONArray jsonArray = JSON.parseArray(object.getString("data"));
JSONObject entity = JSON.parseObject(jsonArray.get(0).toString());
return entity;
}
//获取k线图
public static List getStockKline(String exchange, String coCode, String format, String durationType, String duration){
String url = String.format("https://marketapi.intoday.in/widget/pricechart_company/pullview?exchange=%s&co_code=%s&format=%s&durationtype=%s&duration=%s",
exchange, coCode, format, durationType, duration);
String str = get(url);
JSONObject object = JSON.parseObject(str);
boolean bool = false;
if(object.containsKey("success")){
bool = object.getBoolean("success");
}
if(!bool){
return new ArrayList();
}
return getList(object.getString("data"),1);
}
public static void main(String[] args) {

View File

@@ -233,6 +233,16 @@ public class StockApiController {
return this.stockService.getIndiaIndex();
}
@RequestMapping({"getIndiaIndexByToday.do"})
@ApiOperation(value = "印度--获取指定指数信息", httpMethod = "GET")
@ResponseBody
public ServerResponse getIndiaIndexByToday() {
String INDEX_CODE = "TODAY_INDEX";
return RequestCacheUtils.cache("getIndiaIndexByToday.do", INDEX_CODE, (string) -> {
return this.stockService.getIndexByBtoday();
});
}
//根据股票id查询 股票指数、大盘指数信息
@RequestMapping({"getMarketByStockGid.do"})
@ApiOperation(value = "根据股票id查询 股票指数、大盘指数信息", httpMethod = "GET")