1.10芝麻开门网站汇率抓取代码参考
<p>/**</p>
<ul>
<li>芝麻开门(www.gate.io)网汇率抓取实现类</li>
<li>
</li>
<li>@author </li>
<li>
<p>@date 2021-12-01
*/
@Slf4j
@Component("gateIoExchangePrice")
public class GateIoExchangePrice extends AbstractFetchExchangeRate {</p>
<p>@Value("${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>}")
private String 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>";</p>
<p>private static final String USDT_CNY="USDT_CNY";</p>
<p>private static final String ETH_CNY="ETH_CNY";</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<String,String > map=new HashMap<>(2);
map.put("type","push_main_rates");
map.put("symbol",symbol);
//开启计时器
StopWatch stopWatch=new StopWatch();
stopWatch.start();
final String resp = HttpClientUtils.postOfFrom(BASE_URL, map);
stopWatch.stop();
log.info("【芝麻开门】{} 汇率抓取,请求地址:{} 返回结果:{}",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>