You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
3.8 KiB

  1. package com.deepchart.utils;
  2. import com.deepchart.entity.StockDailyData;
  3. import com.deepchart.entity.StockInfo;
  4. import com.fasterxml.jackson.databind.ObjectMapper;
  5. import org.springframework.stereotype.Component;
  6. import java.io.BufferedReader;
  7. import java.io.InputStreamReader;
  8. import java.io.OutputStream;
  9. import java.net.HttpURLConnection;
  10. import java.net.URL;
  11. import java.nio.charset.StandardCharsets;
  12. import java.time.LocalDate;
  13. import java.time.format.DateTimeFormatter;
  14. import java.util.ArrayList;
  15. import java.util.HashMap;
  16. import java.util.List;
  17. import java.util.Map;
  18. /**
  19. * 股票行情基本数据工具类
  20. */
  21. @Component
  22. public class StockDataUtil {
  23. private static final String STOCK_DATA_PREFIX = "https://api.homilychart.com/link";
  24. private static final String TOKEN = "8nkj4QBV1RPIb4CzoRTnbZi0+fEeMx8pywnIlrmTxdwROKkuwWqAWu9orpkpeXVqL98DPfeonNYpHv+mucA";
  25. private static final ObjectMapper objectMapper = new ObjectMapper();
  26. public List<StockDailyData> getStockData(StockInfo stock) {
  27. List<List<Object>> kLine20 = List.of();
  28. try {
  29. // 1. 构建请求URL
  30. String url = STOCK_DATA_PREFIX + "/api/superBrainData";
  31. // 2. 使用Map构造请求体参数
  32. Map<String, Object> requestBody = new HashMap<>();
  33. requestBody.put("brainPrivilegeState", 1);
  34. requestBody.put("marketList", "can,usa,hk,vi,sg,th,in,cn,gb,my");
  35. requestBody.put("market", stock.getMarket());
  36. requestBody.put("code", stock.getCode());
  37. // 3. 将Map转换为JSON字符串
  38. String jsonInputString = objectMapper.writeValueAsString(requestBody);
  39. // 4. 创建HTTP连接
  40. HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
  41. connection.setRequestMethod("POST");
  42. connection.setRequestProperty("Content-Type", "application/json");
  43. connection.setRequestProperty("token", TOKEN);
  44. connection.setDoOutput(true);
  45. // 5. 发送请求数据
  46. try (OutputStream os = connection.getOutputStream()) {
  47. byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8);
  48. os.write(input, 0, input.length);
  49. }
  50. // 6. 解析响应
  51. if (connection.getResponseCode() == 200) {
  52. try (BufferedReader br = new BufferedReader(
  53. new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
  54. StringBuilder response = new StringBuilder();
  55. String responseLine;
  56. while ((responseLine = br.readLine()) != null) {
  57. response.append(responseLine.trim());
  58. }
  59. // 解析JSON响应
  60. Map<String, Object> jsonResponse = objectMapper.readValue(response.toString(), Map.class);
  61. Map<String, Object> data = (Map<String, Object>) jsonResponse.get("data");
  62. Map<String, Object> brain = (Map<String, Object>) data.get("Brain");
  63. kLine20 = (List<List<Object>>) brain.get("KLine20");
  64. }
  65. }
  66. // 7. 关闭连接
  67. connection.disconnect();
  68. } catch (Exception e) {
  69. e.printStackTrace();
  70. }
  71. List<StockDailyData> list = new ArrayList<>();
  72. for (List<Object> v : kLine20) {
  73. StockDailyData s = new StockDailyData();
  74. s.setDate(LocalDate.parse((String) v.get(0), DateTimeFormatter.ofPattern("yyyy/MM/dd")));
  75. s.setOpenPrice((double)v.get(1));
  76. s.setClosePrice((double)v.get(2));
  77. s.setLowPrice((double)v.get(3));
  78. s.setHighPrice((double)v.get(4));
  79. list.add(s);
  80. }
  81. return list;
  82. }
  83. }