ybf

ybf


1.11火币网站汇率抓取代码参考

@Service @Slf4j public class HuoBiExchangePrice implements CoinPriceFetchService { @Value("#{'${huobi.url}'.split(',')}") private List<String> huobiUrlList; private int index = 0;

private final BaseExchangeRateServiceImpl baseExchangeRateService;

private final ExchangeRateCheckService exchangeRateService;

@Autowired
private CoinDisposeExchangePrice coinDisposeExchangePrice;

@Autowired
public HuoBiExchangePrice(BaseExchangeRateServiceImpl baseExchangeRateService
        , ExchangeRateCheckService exchangeRateService
) {
    this.baseExchangeRateService = baseExchangeRateService;
    this.exchangeRateService = exchangeRateService;
}

private final static Map&lt;String, String&gt; HEADERS = buildHeader();

private static Map&lt;String, String&gt; buildHeader() {
    Map&lt;String, String&gt; headers = new HashMap&lt;&gt;();

    headers.put(&quot;portal&quot;, &quot;web&quot;);

// headers.put("client-type", "web"); // headers.put(":authority", "otc-api-hk.eiijo.cn"); // headers.put(":method", "GET"); // headers.put(":scheme", "https"); // headers.put("accept", "application/json, text/plain, /"); // headers.put("accept-encoding", "gzip, deflate, br"); // headers.put("accept-language", "zh-CN,zh;q=0.9"); // headers.put("fingerprint", "bf4852e9e49efbd201abf49b799ed6e1"); // headers.put("origin", "https://c2c.huobi.com";); // headers.put("otc-language", "zh-CN"); // headers.put("referer", "https://c2c.huobi.com/";); // headers.put("sec-fetch-dest", "empty"); // headers.put("sec-fetch-mode", "cors"); // headers.put("sec-fetch-site", "cross-site"); // headers.put("token", "undefined"); // headers.put("trace_id", "b79c073f-8806-4e08-b3e2-f51186378ad9"); // headers.put("uid", "0"); // headers.put("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36"); // headers.put("x-requested-with", "XMLHttpRequest"); return headers; }

public String buildParam(String buyOrSell, String assetType,String currency) {

    StringBuilder sb = new StringBuilder();
    if (AssetType.USDT.getSymbol().equals(assetType)) {
        sb.append(&quot;?coinId=2&quot;);
    } else if (AssetType.BTC.getSymbol().equals(assetType)) {
        sb.append(&quot;?coinId=1&quot;);
    } else {
        throw new RuntimeException(&quot;查询的币种有误.&quot;);
    }
    //currency=1代表人民币   currency=2 USD  currency=5 eth
    if(currency.equals(AssetType.USD.getSymbol())){
        sb.append(&quot;&amp;currency=2&quot;);
    }else{
        sb.append(&quot;&amp;currency=1&quot;);
    }
    sb.append(&quot;&amp;tradeType=&quot;).append(buyOrSell);
    sb.append(&quot;&amp;currPage=1&quot;);
    sb.append(&quot;&amp;payMethod=0&quot;);
    sb.append(&quot;&amp;country=37&quot;);
    sb.append(&quot;&amp;blockType=general&quot;);
    sb.append(&quot;&amp;online=1&quot;);
    sb.append(&quot;&amp;range=0&quot;);
    return sb.toString();
}

@Override
public void fetchPriceJsonAndSave() {

// 注意,sell和buy用小写 getAndSavePriceInfo("sell", AssetType.USDT.getSymbol(),AssetType.CNY.getSymbol()); getAndSavePriceInfo("buy", AssetType.USDT.getSymbol(),AssetType.CNY.getSymbol()); getAndSavePriceInfo("sell", AssetType.BTC.getSymbol(),AssetType.CNY.getSymbol()); getAndSavePriceInfo("buy", AssetType.BTC.getSymbol(),AssetType.CNY.getSymbol()); /**

  • 新增USDT->USD汇率抓取
  • @author mortal
  • @date 2021-12-12 */ getAndSavePriceInfo("sell", AssetType.USDT.getSymbol(),AssetType.USD.getSymbol()); getAndSavePriceInfo("buy", AssetType.USDT.getSymbol(),AssetType.USD.getSymbol()); }

    private void getAndSavePriceInfo(String sellOrBuy, String assetType,String currency) { long start = System.currentTimeMillis(); String responseString; try { int size = huobiUrlList.size(); if (index >= size) { index = 0; } String url = huobiUrlList.get(index++);

        String params = buildParam(sellOrBuy, assetType,currency);
        responseString = HttpClientUtils.getUseProxy(url + params, HEADERS);
        /*log.info(&quot;获取火币网{}币价信息:\n{}&quot;, assetType, responseString);*/
    } catch (Exception e) {
        log.warn(&quot;获取火币网{}币价信息失败:{}&quot;, assetType, e.getMessage());
        responseString = e.getMessage();
    }
    
    long duration = System.currentTimeMillis() - start;
    /*https://otc-api.huobi.com/v1/data/trade-market?
  • 会员买入数币时请求参数,tradeType=sell,价格由低到高:6.53到7.32,第一条即为网站卖出一价 "coinId=2&currency=1&tradeType=sell&currPage=1&payMethod=0&country=37&blockType=general&online=1&range=0&amount="; 会员卖出数币时的请求参数,tradeType=buy,价格由高到低:6.01到5.88,第一条即为网站买入一价 *"coinId=2&currency=1&tradeType=buy&currPage=1&payMethod=0&country=37&blockType=general&online=1&range=0&amount=";
  • */

    /*已改: 注意条件中sell和buy,与方法参数中的方向是同向的*/
    if (&quot;buy&quot;.equals(sellOrBuy) &amp;&amp; AssetType.USDT.getSymbol().equals(assetType)) {
        //USDT-&gt;USD汇率
        if(AssetType.USD.getSymbol().equals(currency)){
            parseAndSave(responseString, AssetType.USDT,AssetType.USD, ApiInfoEnum.HT_USDT_USD_FIRST_BUY_PRICE,RateTradingEnum.USD_USDT, BUY_DIRECTION, duration);
            //根据中国银行汇率进行USD兑换
            coinDisposeExchangePrice.fetchPriceJsonAndSave(AssetType.USD.getSymbol(), RateSourceEnum.HT.getMsg());
            return;
        }
        parseAndSave(responseString, AssetType.USDT,AssetType.CNY,ApiInfoEnum.HT_OTC_USDT_FIRST_BUY_PRICE,RateTradingEnum.CNY_USDT, BUY_DIRECTION, duration);
    } else if (&quot;sell&quot;.equals(sellOrBuy) &amp;&amp; AssetType.USDT.getSymbol().equals(assetType)) {
        //USDT-&gt;USD汇率
        if(AssetType.USD.getSymbol().equals(currency)){
            parseAndSave(responseString, AssetType.USDT,AssetType.USD, ApiInfoEnum.HT_USDT_USD_FIRST_SELL_PRICE,RateTradingEnum.USD_USDT, SELL_DIRECTION, duration);
            //根据中国银行汇率进行USD兑换
            coinDisposeExchangePrice.fetchPriceJsonAndSave(AssetType.USD.getSymbol(), RateSourceEnum.HT.getMsg());
            return;
        }
        parseAndSave(responseString, AssetType.USDT,AssetType.CNY, ApiInfoEnum.HT_OTC_USDT_FIRST_SELL_PRICE,RateTradingEnum.CNY_USDT, SELL_DIRECTION, duration);
    } else if (&quot;buy&quot;.equals(sellOrBuy) &amp;&amp; AssetType.BTC.getSymbol().equals(assetType)) {
        parseAndSave(responseString, AssetType.BTC,AssetType.CNY, ApiInfoEnum.HT_OTC_BTC_FIRST_BUY_PRICE,RateTradingEnum.CNY_BTC, BUY_DIRECTION, duration);
    } else if (&quot;sell&quot;.equals(sellOrBuy) &amp;&amp; AssetType.BTC.getSymbol().equals(assetType)) {
        parseAndSave(responseString, AssetType.BTC,AssetType.CNY, ApiInfoEnum.HT_OTC_BTC_FIRST_SELL_PRICE,RateTradingEnum.CNY_BTC, SELL_DIRECTION, duration);
    }

    }

    private void parseAndSave(String responseString, AssetType assetType, AssetType priceUnit, ApiInfoEnum apiInfoEnum, RateTradingEnum rateTradingEnum, Integer directionCode, long duration) { BigDecimal price = null; if (StringUtils.isNotBlank(responseString)) { try { JSONArray jsonArray = JSONObject.parseObject(responseString).getJSONArray("data"); //取第一条价格即可 JSONObject jsonObject = jsonArray.getJSONObject(0); price = new BigDecimal(jsonObject.getString("price")); try { log.info("HuoBi抓取到的{}价格为:{}", assetType, price); // influxDbHelperService.saveToInfluxDB("BASE", assetType, apiInfoEnum, directionCode, price); // // exchangeRateService.checkPrice(assetType, price);

            } catch (Exception e) {
                return;
            }
        } catch (Exception e) {
            log.error(&quot;HuoBi数据格式异常:{}&quot;, e.getMessage());
        }
    }
    
    try {
        BaseExchangeRateHistoryDTO baseExchangeRateHistoryDTO = new BaseExchangeRateHistoryDTO();
        baseExchangeRateHistoryDTO.setPriceUnit(priceUnit);
        baseExchangeRateHistoryDTO.setCoinType(assetType);
        baseExchangeRateHistoryDTO.setDuration(duration);
        baseExchangeRateHistoryDTO.setResponse(responseString);
        baseExchangeRateHistoryDTO.setPrice(price);
        baseExchangeRateHistoryDTO.setApiInfo(apiInfoEnum);
        baseExchangeRateHistoryDTO.setDirection(directionCode);
        baseExchangeRateHistoryDTO.setApiSource(RateSourceEnum.HT);
        baseExchangeRateHistoryDTO.setPriceTrading(rateTradingEnum);
        baseExchangeRateService.mergeBaseExchangeRate(baseExchangeRateHistoryDTO, true);
    } catch (Exception e) {
        log.error(&quot;HuoBi汇率更新失败:&quot; + e.getMessage(), e);
    }

    }

    public static void main(String[] args) { try { // 会员卖出数币时的请求 String userSellUrl="https://otc-api.huobi.com/v1/data/trade-market?coinId=2&currency=1&tradeType=buy&currPage=1&payMethod=0&country=37&blockType=general&online=1&range=0&amount=";; // 会员买入数币时请求. String userBuyUrl = "https://otc-api.huobi.com/v1/data/trade-market?coinId=2&currency=2&tradeType=sell&currPage=1&payMethod=0&country=37&blockType=general&online=1&range=0&amount=";; // String responseString = HttpClientUtils.getUseProxy(userBuyUrl, HEADERS); log.info(responseString); } catch (Exception e) { log.error("获取huobi币价信息失败:{}", e.getMessage()); } } }

页面列表

ITEM_HTML