ybf

ybf


1.10芝麻开门网站汇率抓取代码参考

<p>/**</p> <ul> <li>芝麻开门(www.gate.io)网汇率抓取实现类</li> <li> </li> <li>@author </li> <li> <p>@date 2021-12-01 */ @Slf4j @Component(&quot;gateIoExchangePrice&quot;) public class GateIoExchangePrice extends AbstractFetchExchangeRate {</p> <p>@Value(&quot;${gate.io.base_url:<a href="https://www.gate.io/json_svr/query_push/?u=20">https://www.gate.io/json_svr/query_push/?u=20</a>}&quot;) private String BASE_URL=&quot;<a href="https://www.gate.io/json_svr/query_push/?u=20">https://www.gate.io/json_svr/query_push/?u=20</a>&quot;;</p> <p>private static final String USDT_CNY=&quot;USDT_CNY&quot;;</p> <p>private static final String ETH_CNY=&quot;ETH_CNY&quot;;</p> <p>@Override public List<FetchExchangeRatePO> fetchExchange(String fetchBatchNo) { List<FetchExchangeRatePO> exchangeRates=Lists.newArrayList(); exchangeRates.addAll(this.sendRequest(USDT_CNY)); exchangeRates.addAll(this.sendRequest(ETH_CNY)); //批量保存汇率结果集 super.saveBatch(fetchBatchNo,exchangeRates); return exchangeRates; }</p> <p>private final List<FetchExchangeRatePO> sendRequest(String symbol){ List<FetchExchangeRatePO> list= Lists.newArrayList(); try { Map&lt;String,String &gt; map=new HashMap<>(2); map.put(&quot;type&quot;,&quot;push_main_rates&quot;); map.put(&quot;symbol&quot;,symbol); //开启计时器 StopWatch stopWatch=new StopWatch(); stopWatch.start(); final String resp = HttpClientUtils.postOfFrom(BASE_URL, map); stopWatch.stop(); log.info(&quot;【芝麻开门】{} 汇率抓取,请求地址:{} 返回结果:{}&quot;,symbol,BASE_URL,resp); if(StringUtils.isNotBlank(resp)){ GateIoExchageResp gateIoExchageResp = JSON.parseObject(resp, GateIoExchageResp.class); if(gateIoExchageResp.isResult()){ //买入汇率 BigDecimal buyRate=gateIoExchageResp.getAppraised_rates().getBuy_rate(); //卖出汇率 BigDecimal sellRate=gateIoExchageResp.getAppraised_rates().getSell_rate(); //币种类型 AssetType assetType=symbol.equals(USDT_CNY)?AssetType.USDT:AssetType.ETH;</p> <pre><code> list.add(super.newInstance(assetType,TradeDirectionEnum.BUY_FROM_USER,buyRate,stopWatch.getTotalTimeMillis())); list.add(super.newInstance(assetType,TradeDirectionEnum.SELL_TO_USER,sellRate,stopWatch.getTotalTimeMillis())); } } }catch (Exception e){ log.error("【芝麻开门】汇率抓取请求异常:{}",e.getMessage(),e); } return list;</code></pre> <p>}</p> <p>@Override protected ExchangeRateSourceEnum source() { return ExchangeRateSourceEnum.GATE_IO; }</p> <p>@Override protected String getApiInfo(AssetType assetType, TradeDirectionEnum tradeType){ if(tradeType.equals(TradeDirectionEnum.BUY_FROM_USER)){ if(AssetType.USDT.equals(assetType)){ return ApiInfoConstant.GATEIO_USDT_CNY_FIRST_BUY_PRICE; }else{ return ApiInfoConstant.GATEIO_ETH_CNY_FIRST_BUY_PRICE; } }else{ if(AssetType.USDT.equals(assetType)){ return ApiInfoConstant.GATEIO_USDT_CNY_FIRST_SELL_PRICE; }else{ return ApiInfoConstant.GATEIO_ETH_CNY_FIRST_SELL_PRICE; } } } }</p> </li> </ul>

页面列表

ITEM_HTML