bToday代码提交

This commit is contained in:
Achilles
2023-12-26 18:00:33 +08:00
parent d28e97d9ba
commit 7b693410ae
5 changed files with 361 additions and 0 deletions

View File

@@ -0,0 +1,106 @@
package cn.stock.market.web;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
@RestController
@Api(tags="bToday获取详情接口")
public class BTodayStockController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/api/bToday/kLine")
@ApiOperation(value = "股票详情K线图",httpMethod = "GET")
@ApiImplicitParams({
@ApiImplicitParam(name="exchange",value = "BSE或者NSE"),
@ApiImplicitParam(name="co_code",value = "BToday的coCode值"),
@ApiImplicitParam(name="format",value = "S(1D的时候传S),H(5D的时候传H),H(3M的时候传H),H(1Y的时候传H),H(5Y的时候传H),H(10Y的时候传H)"),
@ApiImplicitParam(name="durationtype",value = "D(1D的时候传D),D(5D的时候传D),M(3M的时候传M),Y(1Y的时候传Y),Y(5Y的时候传Y),Y(10Y的时候传Y)"),
@ApiImplicitParam(name="duration",value = "1(1D的时候传1),5(5D的时候传5),3(3M的时候传3),1(1Y的时候传1),5(5Y的时候传5),10(10Y的时候传10)"),
})
public JSONArray getPriceChartCompanyPullView(
@RequestParam(value = "exchange") String exchange,
@RequestParam(value = "co_code") String coCode,
@RequestParam(value = "format") String format,
@RequestParam(value = "durationtype") String durationType,
@RequestParam(value = "duration") String duration) {
if (StringUtils.isBlank(exchange) || StringUtils.isBlank(coCode) || StringUtils.isBlank(format) || StringUtils.isBlank(durationType) || StringUtils.isBlank(duration)) {
return new JSONArray();
}
// 构建请求URL
String apiUrl = buildApiUrl(exchange, coCode, format, durationType, duration);
// 发起REST请求并获取响应数据
Map<String, Object> response = restTemplate.getForObject(apiUrl, Map.class);
List<Map<String, Object>> data = (List<Map<String, Object>>) response.get("data");
// 转换为StockData列表
JSONArray jsonArray = new JSONArray();
data.forEach(item -> {
String updateDate = (String) item.get("upd_date");
BigDecimal price = new BigDecimal(String.valueOf(item.get("price")));
// 创建JSONObject并放入数据
JSONObject jsonObject = new JSONObject();
jsonObject.put("updateDate", updateDate);
jsonObject.put("price", price);
jsonArray.add(jsonObject);
});
return jsonArray;
}
@ApiOperation(value = "股票详情信息",httpMethod = "GET")
@ApiImplicitParams({
@ApiImplicitParam(name="exchange",value = "BSE或者NSE"),
@ApiImplicitParam(name="co_code",value = "coCode值"),
})
@GetMapping("/api/bToday/stockDetail")
public com.alibaba.fastjson.JSONObject getPriceChartCompanyPullView(
@RequestParam(value = "exchange") String exchange,
@RequestParam(value = "co_code") String coCode
) {
if (StringUtils.isBlank(exchange) || StringUtils.isBlank(coCode) ) {
return new com.alibaba.fastjson.JSONObject();
}
// 构建请求URL
String apiUrl = buildDetailApiUrl(exchange, coCode);
String forObject = restTemplate.getForObject(apiUrl, String.class);
return com.alibaba.fastjson.JSONObject.parseObject(forObject);
}
private String buildDetailApiUrl(String exchange, String coCode) {
String url = String.format("https://marketapi.intoday.in/widget/stockdetail/pullview?co_code=%s&exchange=%s",coCode,exchange);
return url;
}
private String buildApiUrl(String exchange, String coCode, String format, String durationType, String duration) {
// 构建请求URL的逻辑根据你的实际情况来
// 示例return "https://your-api-endpoint/bToday/kLine?exchange=" + exchange + "&co_code=" + coCode + "&format=" + format + "&durationtype=" + durationType + "&duration=" + 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);
return url;
}
}