diff --git a/src/main/java/cn/stock/market/infrastructure/api/investing/InvestingApis.java b/src/main/java/cn/stock/market/infrastructure/api/investing/InvestingApis.java index 2358b71..d53a99f 100644 --- a/src/main/java/cn/stock/market/infrastructure/api/investing/InvestingApis.java +++ b/src/main/java/cn/stock/market/infrastructure/api/investing/InvestingApis.java @@ -227,18 +227,8 @@ public class InvestingApis { if(code == null) { throw new RuntimeException("找不到股票信息"); } - String interval = null; - if("min".equalsIgnoreCase(string)) { - interval = "PT5M"; - } else if("day".equalsIgnoreCase(string)) { - interval = "P1D"; - } else if("week".equalsIgnoreCase(string)) { - interval = "P1W"; - } else if("month".equalsIgnoreCase(string)) { - interval = "P1M"; - } Date nowDate = new Date(); - JSONObject json = InvestingInvokerApis.of().__kline(code, interval); + JSONObject json = InvestingInvokerApis.of().__kline(code, string); return json .getJSONArray("data") .stream() diff --git a/src/main/java/cn/stock/market/infrastructure/api/investing/InvestingInvokerApis.java b/src/main/java/cn/stock/market/infrastructure/api/investing/InvestingInvokerApis.java index 92a44ee..f8fbf3d 100644 --- a/src/main/java/cn/stock/market/infrastructure/api/investing/InvestingInvokerApis.java +++ b/src/main/java/cn/stock/market/infrastructure/api/investing/InvestingInvokerApis.java @@ -138,6 +138,7 @@ public class InvestingInvokerApis { throw new RuntimeException("找不到股票信息"); } String interval = null; + String period = null; if("min".equalsIgnoreCase(string)) { interval = "PT5M"; } else if("day".equalsIgnoreCase(string)) { @@ -146,13 +147,30 @@ public class InvestingInvokerApis { interval = "P1W"; } else if("month".equalsIgnoreCase(string)) { interval = "P1M"; + } else if("3month".equalsIgnoreCase(string)) { + interval = "P1D"; + period = "P3M"; + } else if("6month".equalsIgnoreCase(string)) { + interval = "P1D"; + period = "P6M"; + } else if("year".equalsIgnoreCase(string)) { + interval = "P1W"; + period = "P1Y"; } if(StringUtils.isBlank(interval)) { interval = string; } - String tmpl = "https://api.investing.com/api/financialdata/{}/historical/chart/?interval={}&pointscount=160"; - String url = StrFormatter.format(tmpl, code.getCode(), interval); + + String url = null; + if(StringUtils.isNotBlank(period)) { + String tmpl = "https://api.investing.com/api/financialdata/{}/historical/chart/?period={}&interval={}&pointscount=160"; + url = StrFormatter.format(tmpl, code.getCode(), period, interval); + } else { + String tmpl = "https://api.investing.com/api/financialdata/{}/historical/chart/?interval={}&pointscount=160"; + url = StrFormatter.format(tmpl, code.getCode(), interval); + } + Builder builder = builderGet(url); String body = httpClient().newCall(builder.build()).execute().body().string(); diff --git a/src/main/java/cn/stock/market/infrastructure/db/po/StockPO.java b/src/main/java/cn/stock/market/infrastructure/db/po/StockPO.java index 1bd76cb..398dba2 100644 --- a/src/main/java/cn/stock/market/infrastructure/db/po/StockPO.java +++ b/src/main/java/cn/stock/market/infrastructure/db/po/StockPO.java @@ -66,7 +66,7 @@ public class StockPO { Integer stockState; - Integer stockExchangeId; + String stockExchangeId; - Integer stockSymbol; + String stockSymbol; } diff --git a/src/main/java/cn/stock/market/infrastructure/job/InvestingTask.java b/src/main/java/cn/stock/market/infrastructure/job/InvestingTask.java new file mode 100644 index 0000000..cb512db --- /dev/null +++ b/src/main/java/cn/stock/market/infrastructure/job/InvestingTask.java @@ -0,0 +1,93 @@ +package cn.stock.market.infrastructure.job; + +import cn.stock.market.domain.basic.entity.Stock; +import cn.stock.market.domain.basic.repository.StockRepository; +import cn.stock.market.infrastructure.api.investing.InvestingInvokerApis; +import cn.stock.market.utils.Utils; +import com.ag.utils.CollectionUtils; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.google.common.base.Stopwatch; +import com.google.common.collect.Lists; +import lombok.extern.slf4j.Slf4j; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import java.io.IOException; +import java.util.Date; +import java.util.List; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +@Slf4j +@Component +public class InvestingTask { + + @Scheduled(cron = "0 0 5 * * ?") + public void syncIndiaData(){ + log.info("同步股票数据开始。。。。"); + Map stockMap = StockRepository.of().cacheCodeMap(); + Stopwatch stopwatch = Stopwatch.createStarted(); + List list = Lists.newArrayList(); + int currentTotal = 0; + int pageNum =1; + int pageSize = 300; + try { + for(int i = 0;i < pageNum;i ++) { + JSONObject jsonObject = InvestingInvokerApis.of().__page(pageNum, pageSize); + int total = 0; + if(jsonObject.containsKey("total")){ + total = Integer.parseInt(jsonObject.get("total").toString()); + } + JSONArray dataObjArray = new JSONArray(); + if(jsonObject.containsKey("data")){ + dataObjArray = JSON.parseArray(jsonObject.get("data").toString()); + } + for (Object obj : dataObjArray) { + JSONObject jsonObject2 = JSON.parseObject(obj.toString()); + String code = jsonObject2.get("Id").toString(); + String name = jsonObject2.get("Name").toString(); + String stockSymbol = jsonObject2.get("Symbol").toString(); + String exchangeId = jsonObject2.get("ExchangeId").toString(); + if (stockMap.containsKey(code)) { + log.info("已经存在 {} 信息, 跳过", code); + continue; + } + + if(! Utils.isShOrSzOrBJ(code)) { + log.info("{} 非 sh 或者 sz 或者 bj , 跳过", code); + continue; + } + Stock stock = new Stock(); + stock.setStockSymbol(stockSymbol); + stock.setStockExchangeId(exchangeId); + stock.setStockName(name); + stock.setStockCode(code); + stock.setIsLock(0); + stock.setIsShow(0); + stock.setAddTime(new Date()); + stock.setStockState(0); + list.add(stock); + } + currentTotal += pageSize; + if((total - currentTotal) < pageSize ){ + pageSize = total - currentTotal; + } + if(total == currentTotal){ + break; + } + pageNum ++; + } + if(CollectionUtils.isNotEmpty(list)) { + StockRepository.of().saveAll(list); + } + int count = list.size(); + log.info("syncAFutureStockList执行, 受影响数{}, 耗时:{}毫秒", count, stopwatch.elapsed(TimeUnit.MILLISECONDS)); + log.info("同步股票数据结束。。。。"); + } catch (Exception e) { + log.info("同步股票数据异常,异常信息{}。。。。",e.getMessage()); + } + + } +} diff --git a/src/main/java/cn/stock/market/infrastructure/job/JobBoot.java b/src/main/java/cn/stock/market/infrastructure/job/JobBoot.java index d9de463..a3aa98f 100644 --- a/src/main/java/cn/stock/market/infrastructure/job/JobBoot.java +++ b/src/main/java/cn/stock/market/infrastructure/job/JobBoot.java @@ -13,7 +13,7 @@ import cn.stock.market.domain.basic.service.SiteNewsService; import lombok.extern.slf4j.Slf4j; @Slf4j -@Component +//@Component public class JobBoot { /** * cronExpression表达式定义:  diff --git a/src/main/java/cn/stock/market/infrastructure/job/StockTask.java b/src/main/java/cn/stock/market/infrastructure/job/StockTask.java index 2f7e947..4cbac87 100644 --- a/src/main/java/cn/stock/market/infrastructure/job/StockTask.java +++ b/src/main/java/cn/stock/market/infrastructure/job/StockTask.java @@ -37,7 +37,7 @@ import cn.stock.market.web.config.Config; import lombok.extern.slf4j.Slf4j; @Slf4j -@Component +//@Component public class StockTask { @Autowired RealtimeRepository realtimeRepository; @Autowired StockRepository stockRepository; @@ -202,7 +202,7 @@ public class StockTask { } /*同步股票列表*/ - @Scheduled(cron = "0 5 6 * * ?") +// @Scheduled(cron = "0 5 6 * * ?") public void syncBjsStockList() { MdcUtil.setTraceIdIfAbsent(); boolean flag = true; diff --git a/src/main/java/cn/stock/market/utils/Utils.java b/src/main/java/cn/stock/market/utils/Utils.java index 6a68794..1a0017d 100644 --- a/src/main/java/cn/stock/market/utils/Utils.java +++ b/src/main/java/cn/stock/market/utils/Utils.java @@ -1,16 +1,18 @@ package cn.stock.market.utils; +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.time.DayOfWeek; import java.time.LocalDate; import java.time.LocalTime; -import java.util.ArrayList; -import java.util.Date; -import java.util.Hashtable; -import java.util.List; -import java.util.Map; +import java.util.*; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; import org.apache.commons.lang3.StringUtils; import org.springframework.data.domain.Page; @@ -394,13 +396,55 @@ public final class Utils { return returnStr; } + //读取txt文本内容 + public static String readTxt() { + FileReader fr = null; + BufferedReader br = null; + StringBuilder buffer = new StringBuilder(); + try { + String file = Objects.requireNonNull(Utils.class.getResource("/india.txt")).getPath(); + fr = new FileReader(file); + br = new BufferedReader(fr); + + String line = ""; + while ((line = br.readLine()) != null) { + buffer.append(line); + } + } catch (IOException e) { + throw new RuntimeException(e); + } finally { + if (br != null) { + try { + br.close(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + if (fr != null) { + try { + fr.close(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } + return buffer.toString(); + } + + public static void main(String[] args) { - System.out.println(getFirstLetter("0L神鼎ds飞#@#丹砂")); - System.out.println(getFirstLetter("0L神鼎ds飞#@#丹砂")); - System.out.println(highVersion("1.0.8", "1.0.9")); - System.out.println(highVersion("1.0.9", "1.0.10")); - System.out.println(highVersion("1.0.10", "1.0.11")); - System.out.println(highVersion("1.0.10", "1.0.9")); +// System.out.println(getFirstLetter("0L神鼎ds飞#@#丹砂")); +// System.out.println(getFirstLetter("0L神鼎ds飞#@#丹砂")); +// System.out.println(highVersion("1.0.8", "1.0.9")); +// System.out.println(highVersion("1.0.9", "1.0.10")); +// System.out.println(highVersion("1.0.10", "1.0.11")); +// System.out.println(highVersion("1.0.10", "1.0.9")); + String str = readTxt(); + JSONArray jsonArray = JSON.parseArray(str); + System.out.println(Arrays.toString(jsonArray.toArray())); +// for (Object object : jsonArray){ +// object. +// } } } diff --git a/src/main/java/cn/stock/market/web/StockApiController.java b/src/main/java/cn/stock/market/web/StockApiController.java index e4420bd..b259023 100644 --- a/src/main/java/cn/stock/market/web/StockApiController.java +++ b/src/main/java/cn/stock/market/web/StockApiController.java @@ -25,6 +25,7 @@ import org.springframework.web.bind.annotation.ResponseBody; import com.ag.utils.DateUtils; import com.ag.utils.Jsons; +import com.ag.utils.param.ParamUtils; import com.alibaba.fastjson.JSONObject; import com.google.common.base.Joiner; import com.google.common.cache.Cache; @@ -41,6 +42,9 @@ import cn.stock.market.domain.basic.service.StockService; import cn.stock.market.dto.model.StockCode; import cn.stock.market.dto.model.StockVO; import cn.stock.market.infrastructure.api.EastmoneyApi; +import cn.stock.market.infrastructure.api.investing.IndiaStockVO; +import cn.stock.market.infrastructure.api.investing.InvestingApis; +import cn.stock.market.infrastructure.api.investing.InvestingInvokerApis; import cn.stock.market.infrastructure.api.qq.QqStockApi; import cn.stock.market.infrastructure.api.sina.SinaStockApi; import cn.stock.market.utils.RequestCacheUtils; @@ -121,7 +125,7 @@ public class StockApiController { @ResponseBody public ServerResponse getINDStockInfo(@RequestParam("stockCode") String stockCode) { ParamUtils.verify("股票代码", stockCode, ParamUtils.STRING_NOT_EMPTY_VERIFY_AND_CONVERT_VALUE); - + IndiaStockVO market = InvestingApis.of().market(StockCode.of(stockCode)); return ServerResponse.createBySuccess(market.toStockVo()); } @@ -139,11 +143,15 @@ public class StockApiController { @ApiOperation(value = "印度股票K线", httpMethod = "GET") @ResponseBody @ApiImplicitParams({ - @ApiImplicitParam(name = "stockCode",value = "股票对应代码",dataType ="String",required = true), - @ApiImplicitParam(name = "type",value = "type 0-时分线 1-日线 2-周线 3-月线",dataType ="Integer",required = true), + @ApiImplicitParam(name = "stockCode",value = "股票对应代码",dataType ="String", paramType = "query", required = true), + @ApiImplicitParam(name = "type",value = "min-时分线 day-日线 week-周线 month-月线, 3month-季度, 6month-半年, year-年",dataType ="String", paramType = "query", required = true), }) - public ServerResponse getINDTimeK(@RequestParam("stockCode") String stockCode, @RequestParam("type") Integer type) { - return this.stockService.getIndiaK(stockCode, type); + public ServerResponse getINDTimeK(@RequestParam("stockCode") String stockCode, @RequestParam("type") String type) throws IOException { + ParamUtils.verify("股票代码", stockCode, ParamUtils.STRING_NOT_EMPTY_VERIFY_AND_CONVERT_VALUE); + ParamUtils.verify("K线类型", stockCode, ParamUtils.STRING_NOT_EMPTY_VERIFY_AND_CONVERT_VALUE); + + List list = InvestingApis.of().kline(StockCode.of(stockCode), type); + return ServerResponse.createBySuccess(list); } //根据股票id查询 股票指数、大盘指数信息 diff --git a/src/main/java/cn/stock/market/web/config/Swagger2Config.java b/src/main/java/cn/stock/market/web/config/Swagger2Config.java index 5766e7b..3f09760 100644 --- a/src/main/java/cn/stock/market/web/config/Swagger2Config.java +++ b/src/main/java/cn/stock/market/web/config/Swagger2Config.java @@ -128,7 +128,7 @@ public class Swagger2Config { public ApiInfoBuilder commonApiInfoBuilder() { return new ApiInfoBuilder() - .description("鑫宝 接口文档") + .description("英文股票接口文档") .termsOfServiceUrl("http://www.xxxx.cn/") .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0").version("v1.0"); }