ybf

ybf


1.9非小号网站汇率抓取代码参考

<p>/**</p> <ul> <li>非小号(dncapi.fxhapp.com)网汇率抓取实现类</li> <li> </li> <li>@author</li> <li> <p>@date 2021-12-02 */ @Service @Slf4j public class FxhappExchangePrice implements CoinPriceFetchService {</p> <p>private String BASE_URL=&quot;<a href="https://dncapi.fxhapp.com/api/coin/web-coininfo">https://dncapi.fxhapp.com/api/coin/web-coininfo</a>&quot;;</p> <p>private static final String USDT_CNY=&quot;tether&quot;;</p> <p>private static final String ETH_CNY=&quot;ethereum&quot;;</p> <p>private static final String appName=&quot;非小号&quot;;</p> <p>@Autowired private FetchExchangeRateService fetchExchangeRateService;</p> <p>@Override public void fetchPriceJsonAndSave() { }</p> <p>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(&quot;【非小号】查询汇率成功,开始批量入库:{}&quot;,JSON.toJSONString(exchangeRates)); //批量保存汇率结果集 fetchExchangeRateService.saveBatch(exchangeRates); } }</p> <p>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(&quot;addlink&quot;, &quot;1&quot;); js.put(&quot;code&quot;, symbol); js.put(&quot;webp&quot;, &quot;1&quot;); final String resp= HttpUtil.okHttpPost(BASE_URL, js.toString()); log.info(&quot;【非小号】{} 汇率抓取,请求地址:{} 返回结果:{}&quot;,symbol,BASE_URL,resp);</p> <pre><code> 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;</code></pre> <p>}</p> <p>//获取 USDT 买一价 卖一价 均价 private final List<FetchExchangeRatePO> sendRequest2(String fetchId){ long start = System.currentTimeMillis(); List<FetchExchangeRatePO> list= Lists.newArrayList(); try { String scheme = &quot;https&quot;; String host = &quot;dncapi.fxhapp.com&quot;; String path = &quot;api/v1/coin/stable/preminum/USDT&quot;; Map&lt;String, String&gt; headers = new HashMap<>(1); Map&lt;String, String&gt; params = new HashMap<>(2); params.put(&quot;webp&quot;, &quot;1&quot;); 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(&quot;【非小号】USDT买卖一价汇率抓取,请求地址:{} 返回结果:{}&quot;,path,resp);</p> <pre><code> 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 &amp;&amp; array.length()&gt;0) { for (int i = 0; i &lt; 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;</code></pre> <p>}</p> <p>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; }</p> </li> </ul> <p>}</p>

页面列表

ITEM_HTML