1.10芝麻开门网站汇率抓取代码参考
/**
- 芝麻开门(www.gate.io)网汇率抓取实现类
- @author
-
@date 2021-12-01 */ @Slf4j @Component("gateIoExchangePrice") public class GateIoExchangePrice extends AbstractFetchExchangeRate {
@Value("${gate.io.base_url:https://www.gate.io/json_svr/query_push/?u=20}") private String BASE_URL="https://www.gate.io/json_svr/query_push/?u=20";
private static final String USDT_CNY="USDT_CNY";
private static final String ETH_CNY="ETH_CNY";
@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; }
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;
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;
}
@Override protected ExchangeRateSourceEnum source() { return ExchangeRateSourceEnum.GATE_IO; }
@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; } } } }