1.9非小号网站汇率抓取代码参考
/**
- 非小号(dncapi.fxhapp.com)网汇率抓取实现类
- @author
-
@date 2021-12-02 */ @Service @Slf4j public class FxhappExchangePrice implements CoinPriceFetchService {
private String BASE_URL="https://dncapi.fxhapp.com/api/coin/web-coininfo";
private static final String USDT_CNY="tether";
private static final String ETH_CNY="ethereum";
private static final String appName="非小号";
@Autowired private FetchExchangeRateService fetchExchangeRateService;
@Override public void fetchPriceJsonAndSave() { }
public void fetchPriceAndSave(String fetchId) { List<FetchExchangeRatePO> exchangeRates=Lists.newArrayList(); exchangeRates.addAll(this.sendRequest(USDT_CNY ,fetchId)); exchangeRates.addAll(this.sendRequest(ETH_CNY ,fetchId)); exchangeRates.addAll(this.sendRequest2(fetchId)); if(!exchangeRates.isEmpty()){ log.warn("【非小号】查询汇率成功,开始批量入库:{}",JSON.toJSONString(exchangeRates)); //批量保存汇率结果集 fetchExchangeRateService.saveBatch(exchangeRates); } }
private final List<FetchExchangeRatePO> sendRequest(String symbol ,String fetchId){ long start = System.currentTimeMillis(); List<FetchExchangeRatePO> list= Lists.newArrayList(); try { JSONObject js = new JSONObject(); js.put("addlink", "1"); js.put("code", symbol); js.put("webp", "1"); final String resp= HttpUtil.okHttpPost(BASE_URL, js.toString()); log.info("【非小号】{} 汇率抓取,请求地址:{} 返回结果:{}",symbol,BASE_URL,resp);
if(StringUtils.isNotBlank(resp)){ long delayTime = System.currentTimeMillis() - start; JSONObject json = new JSONObject(resp); JSONObject data = json.getJSONObject("data"); Double usdt_cnyRate =null; Double usdt_usdRate =null; if(data .has("price_cny")){ usdt_cnyRate = Double.valueOf(data.get("price_cny").toString()); AssetType assetType=symbol.equals(USDT_CNY)?AssetType.USDT:AssetType.ETH; list.add(newInstance(assetType,new BigDecimal(usdt_cnyRate.doubleValue()) ,fetchId, AssetType.CNY.getCode(), TradeDirectionEnum.STANDARD.getCode(),assetType.getSymbol()+"-CNY标准价",delayTime )); } if(data .has("price")){ usdt_usdRate = Double.valueOf(data.get("price").toString()); AssetType assetType=symbol.equals(USDT_CNY)?AssetType.USDT:AssetType.ETH; list.add(newInstance(assetType,new BigDecimal(usdt_usdRate.doubleValue()) ,fetchId, AssetType.USD.getCode(), TradeDirectionEnum.STANDARD.getCode(),assetType.getSymbol()+"-USD标准价",delayTime )); } } } catch (Throwable e) { log.error("非小号汇率获取异常1,{}",e); } return list;
}
//获取 USDT 买一价 卖一价 均价 private final List<FetchExchangeRatePO> sendRequest2(String fetchId){ long start = System.currentTimeMillis(); List<FetchExchangeRatePO> list= Lists.newArrayList(); try { String scheme = "https"; String host = "dncapi.fxhapp.com"; String path = "api/v1/coin/stable/preminum/USDT"; Map<String, String> headers = new HashMap<>(1); Map<String, String> params = new HashMap<>(2); params.put("webp", "1"); HttpGetRequestBean requestBean = new HttpGetRequestBean(); requestBean.setHeaders(headers); requestBean.setHost(host); requestBean.setParams(params); requestBean.setPath(path); requestBean.setScheme(scheme); String resp = HttpUtil.getResponse(requestBean); log.info("【非小号】USDT买卖一价汇率抓取,请求地址:{} 返回结果:{}",path,resp);
if(StringUtils.isNotBlank(resp)){ long delayTime = System.currentTimeMillis() - start; JSONObject json = new JSONObject(resp); JSONObject data = json.getJSONObject("data"); JSONArray array = data.getJSONArray("list"); if(array!=null && array.length()>0) { for (int i = 0; i < array.length(); i++) { JSONObject json_obj = array.getJSONObject(i); JSONArray data2 = json_obj.getJSONArray("data"); //买一价 卖一价 均价 Double buy_usdt_cny = Double.valueOf(data2.get(0).toString()); Double sell_usdt_cny = Double.valueOf(data2.get(1).toString()); Double vag_usdt_cny = Double.valueOf(data2.get(2).toString()); //String platform= json_obj.getString("platform"); String platformName= json_obj.getString("platform_name"); list.add(newInstance(AssetType.USDT,new BigDecimal(buy_usdt_cny.doubleValue()) ,fetchId, AssetType.CNY.getCode(), TradeDirectionEnum.BUY_FROM_USER.getCode(),platformName+"之USDT-CNY买一价",delayTime )); list.add(newInstance(AssetType.USDT,new BigDecimal(sell_usdt_cny.doubleValue()) ,fetchId, AssetType.CNY.getCode(), TradeDirectionEnum.SELL_TO_USER.getCode(),platformName+"之USDT-CNY卖一价",delayTime )); list.add(newInstance(AssetType.USDT,new BigDecimal(vag_usdt_cny.doubleValue()) ,fetchId, AssetType.CNY.getCode(), 3,platformName+"之USDT-CNY平均价",delayTime )); } } } } catch (Throwable e) { log.error("非小号汇率获取异常2,{}",e); } return list;
}
private final static FetchExchangeRatePO newInstance(AssetType assetType, BigDecimal price,String fetchId, Integer priceUnit, Integer tradeType,String apiInfo ,Long delayTime ){ FetchExchangeRatePO fetchExchangeRatePO=new FetchExchangeRatePO(); fetchExchangeRatePO.setPrice(price); fetchExchangeRatePO.setTradeType(tradeType); fetchExchangeRatePO.setPriceUnit(priceUnit ); fetchExchangeRatePO.setCoinType(assetType.getCode()); fetchExchangeRatePO.setFetchBatches(fetchId); fetchExchangeRatePO.setDelayTime(delayTime); //TODO 增加数据来源枚举类 fetchExchangeRatePO.setApiName(appName); fetchExchangeRatePO.setApiInfo(apiInfo); fetchExchangeRatePO.setStatus(1); return fetchExchangeRatePO; }
}