From 269f02b79de3613525a261b85010e8d6e1bb92a7 Mon Sep 17 00:00:00 2001 From: gavin Date: Wed, 4 Sep 2024 11:56:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8C=87=E5=AE=9A=E8=82=A1=E7=A5=A8=E6=9B=B4?= =?UTF-8?q?=E6=8D=A2=E8=AF=A6=E6=83=85=E6=B5=8B=E8=AF=95=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../stock/market/web/MoneyApiController.java | 159 ++++++++++++++---- 1 file changed, 128 insertions(+), 31 deletions(-) diff --git a/src/main/java/cn/stock/market/web/MoneyApiController.java b/src/main/java/cn/stock/market/web/MoneyApiController.java index 4f1be40..0635d86 100644 --- a/src/main/java/cn/stock/market/web/MoneyApiController.java +++ b/src/main/java/cn/stock/market/web/MoneyApiController.java @@ -21,6 +21,7 @@ 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; @@ -43,6 +44,7 @@ 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; /** @@ -63,7 +65,26 @@ 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> cookieStore = new HashMap<>(); + + @Override + public void saveFromResponse(HttpUrl url, List cookies) { + cookieStore.put(url.host(), cookies); + } + + @Override + public List loadForRequest(HttpUrl url) { + List cookies = cookieStore.get(url.host()); + return cookies != null ? cookies : new ArrayList(); + } + }) + .build(); + } @ApiOperation(value = "股票详情信息",httpMethod = "GET") @ApiImplicitParams({ @@ -173,7 +194,12 @@ public class MoneyApiController { @EncryptFilter(decryptRequest = false) public ServerResponse getStockDetail(@RequestParam String stockType, @RequestParam String symbol ) { - String url = String.format("https://priceapi.moneycontrol.com/pricefeed/%s/equitycash/%s",stockType,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)) @@ -183,48 +209,119 @@ public class MoneyApiController { return ServerResponse.createByErrorMsg("没有找到该股票"); }*/ // 设置重试次数 - int maxRetries = 3; - for (int retry = 1; retry <= maxRetries; retry++) { + if(symbol.equals("IPL02") || symbol.equals("IPHL")){ try { - ResponseEntity 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"); + 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"); if(data!=null){ - json1.put("company",data.getString("SC_FULLNM")); - json1.put("pricepercentchange",data.getString("pricepercentchange")); + json1.put("company","Indian Phosphate Limited"); + json1.put("pricepercentchange",data.getString("pChange")); 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("pricechange",data.getString("change")); + json1.put("pricecurrent",data.getString("lastPrice")); + json1.put("priceprevclose",data.getString("previousClose")); + json1.put("PREVDATE",""); + json1.put("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")); + 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")); 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 (Exception e) { + + } catch (IOException e) { + throw new RuntimeException("Failed to initialize cookies", e); } - // 如果不是最后一次重试,则等待一段时间再进行下一次重试 - if (retry < maxRetries) { + }else { + int maxRetries = 3; + for (int retry = 1; retry <= maxRetries; retry++) { try { - // 1秒钟 - Thread.sleep(100); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); + ResponseEntity 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(); + } } } }