Explorar el Código

add:coinwatchlist cex flag数据同步v1

lvzhiqiang hace 1 año
padre
commit
548efb2aab

+ 11 - 0
src/main/java/top/lvzhiqiang/config/MyCoinJobs.java

@@ -66,4 +66,15 @@ public class MyCoinJobs {
 
         coinService.syncCoinmarketcapCMap();
     }
+
+    /**
+     * 同步CEX FLAG
+     */
+    @Scheduled(cron = "0 0 7 * * ?", zone = SCHEDULED_ZONE)
+    public void syncCexFlag() {
+        try {
+            coinService.syncCexFlag();
+        } catch (Exception e) {
+        }
+    }
 }

+ 2 - 0
src/main/java/top/lvzhiqiang/entity/CoinWatchlistOther.java

@@ -20,6 +20,8 @@ public class CoinWatchlistOther implements Serializable {
      */
     private Long id;
 
+    private String coingeckoId;
+
     /**
      * 名称
      */

+ 14 - 0
src/main/java/top/lvzhiqiang/mapper/CoinMapper.java

@@ -7,6 +7,7 @@ import org.apache.ibatis.annotations.Select;
 import org.apache.ibatis.annotations.Update;
 import top.lvzhiqiang.entity.*;
 
+import java.util.Collection;
 import java.util.List;
 import java.util.Map;
 
@@ -116,6 +117,19 @@ public interface CoinMapper {
             ",market_cap=values(market_cap),fully_diluted_market_cap=values(fully_diluted_market_cap),cex_spot=values(cex_spot),cex_perpetual=values(cex_perpetual),modify_time=now()")
     int insertOrUpdateCoinWatchlistOther(CoinWatchlistOther coinWatchlistOther);
 
+    @Select("select a.*,b.coingecko_id from coin_watchlist_other a left join coin_watchlist b on a.symbol=b.symbol where a.delete_flag = 1 and a.symbol not in ('BTC','ETH')")
+    List<CoinWatchlistOther> findAllCoinWatchlistOther();
+
+    @Insert({"<script>" +
+            "INSERT INTO coin_watchlist_other(symbol,cex_spot,cex_perpetual,create_time,modify_time)" +
+            " VALUES " +
+            "<foreach collection='collection' item='cwo' index=\"index\" separator=\",\">" +
+            "   (#{cwo.symbol},#{cwo.cexSpot},#{cwo.cexPerpetual},now(),now())" +
+            " </foreach>" +
+            " ON DUPLICATE KEY UPDATE cex_spot=values(cex_spot),cex_perpetual=values(cex_perpetual),modify_time=now()" +
+            "</script>"})
+    int insertOrUpdateCoinWatchlistOtherList(Collection<CoinWatchlistOther> coinWatchlistOtherList);
+
     @Update({"<script>" +
             "update coin_watchlist " +
             "<set>" +

+ 2 - 0
src/main/java/top/lvzhiqiang/service/CoinService.java

@@ -56,5 +56,7 @@ public interface CoinService {
 
     void debugTest();
 
+    void syncCexFlag();
+
     void initWatchlist(CoinWatchlist coinWatchlist);
 }

+ 134 - 4
src/main/java/top/lvzhiqiang/service/impl/CoinServiceImpl.java

@@ -36,6 +36,7 @@ import java.io.UnsupportedEncodingException;
 import java.math.BigDecimal;
 import java.math.MathContext;
 import java.math.RoundingMode;
+import java.net.Proxy;
 import java.security.InvalidKeyException;
 import java.text.DecimalFormat;
 import java.time.Duration;
@@ -930,6 +931,131 @@ public class CoinServiceImpl implements CoinService {
         return null;
     }
 
+    private void initCexSpotFlag(Map<String, CoinWatchlistOther> coinWatchlistOtherMap4Symbol) {
+        // spot
+        String coingeckoExchangeTickersUrl = InitRunner.dicCodeMap.get("coingecko_exchange_tickers_url").getCodeValue();
+        String[] coingeckoExchangeIdArr = InitRunner.dicCodeMap.get("coingecko_exchange_ids").getCodeValue().split(",");
+
+        Map<String, String> headerMap = new HashMap<>();
+        headerMap.put("Accept", "application/json");
+        headerMap.put("Accept-Encoding", "deflate,gzip");
+        Map<String, String> paramMap = new LinkedHashMap<>();
+        paramMap.put("coin_ids", StringUtils.join(coinWatchlistOtherMap4Symbol.keySet(), ","));
+
+        int iii = 0;
+        JSONArray result = null;
+        for (String coingeckoExchangeId : coingeckoExchangeIdArr) {
+            iii++;
+            try {
+                Thread.sleep(10000L);
+
+                Connection.Response response = JsoupUtil.requestBody(String.format(coingeckoExchangeTickersUrl, coingeckoExchangeId), JsoupUtil.HTTP_GET, Proxy.NO_PROXY, headerMap, paramMap);
+                result = JSONObject.parseObject(response.body()).getJSONArray("tickers");
+
+                if (result == null) {
+                    Thread.sleep(10000L);
+                    response = JsoupUtil.requestBody(String.format(coingeckoExchangeTickersUrl, coingeckoExchangeId), JsoupUtil.HTTP_GET, Proxy.NO_PROXY, headerMap, paramMap);
+                    result = JSONObject.parseObject(response.body()).getJSONArray("tickers");
+                }
+
+                Map<String, JSONObject> resultMap = result.stream().collect(Collectors.toMap(i -> {
+                    JSONObject a = (JSONObject) i;
+                    String k = a.getString("base").concat(a.getString("target"));
+                    return k;
+                }, i -> (JSONObject) i, (existing, replacement) -> existing));
+
+
+                for (CoinWatchlistOther coinWatchlistOther : coinWatchlistOtherMap4Symbol.values()) {
+                    String baseTarget = coinWatchlistOther.getSymbol().concat("USDT");
+                    if (iii == 1) {
+                        coinWatchlistOther.setCexSpot("");
+                    }
+                    if (resultMap.containsKey(baseTarget)) {
+                        coinWatchlistOther.setCexSpot(coinWatchlistOther.getCexSpot() + "1");
+                    } else {
+                        coinWatchlistOther.setCexSpot(coinWatchlistOther.getCexSpot() + "0");
+                    }
+                }
+            } catch (Exception e) {
+                log.error("initCexSpotFlag error,url={},paramMap={},result={}", String.format(coingeckoExchangeTickersUrl, coingeckoExchangeId), paramMap, result, e);
+                throw new RuntimeException("initCexSpotFlag error");
+            }
+        }
+    }
+
+    /**
+     * 解析交易所现货+合约标志
+     */
+    @Override
+    @Async
+    public void syncCexFlag() {
+        List<CoinWatchlistOther> watchlistOtherList = coinMapper.findAllCoinWatchlistOther();
+
+        // Perpetual
+        String coingeckoExchangeFuturesTickersUrl = InitRunner.dicCodeMap.get("coingecko_exchange_futures_tickers_url").getCodeValue();
+        String[] coingeckoExchangeFutruesIdArr = InitRunner.dicCodeMap.get("coingecko_exchange_futures_ids").getCodeValue().split(",");
+
+        Map<String, String> headerMap = new HashMap<>();
+        headerMap.put("Accept", "application/json");
+        headerMap.put("Accept-Encoding", "deflate,gzip");
+        Map<String, String> paramMap = new LinkedHashMap<>();
+        paramMap.put("include_tickers", "unexpired");
+
+        int jjj = 0;
+        JSONArray result = null;
+        for (String coingeckoExchangeFutruesId : coingeckoExchangeFutruesIdArr) {
+            jjj++;
+            try {
+                Thread.sleep(10000L);
+
+                Connection.Response response = JsoupUtil.requestBody(String.format(coingeckoExchangeFuturesTickersUrl, coingeckoExchangeFutruesId), JsoupUtil.HTTP_GET, Proxy.NO_PROXY, headerMap, paramMap);
+                result = JSONObject.parseObject(response.body()).getJSONArray("tickers");
+
+                if (result == null) {
+                    Thread.sleep(10000L);
+                    response = JsoupUtil.requestBody(String.format(coingeckoExchangeFuturesTickersUrl, coingeckoExchangeFutruesId), JsoupUtil.HTTP_GET, Proxy.NO_PROXY, headerMap, paramMap);
+                    result = JSONObject.parseObject(response.body()).getJSONArray("tickers");
+                }
+
+                Map<String, JSONObject> resultMap = result.stream().collect(Collectors.toMap(i -> {
+                    JSONObject a = (JSONObject) i;
+                    String k = a.getString("base").concat(a.getString("target"));
+                    return k;
+                }, i -> (JSONObject) i, (existing, replacement) -> existing));
+
+                for (CoinWatchlistOther coinWatchlistOther : watchlistOtherList) {
+                    String baseTarget = coinWatchlistOther.getSymbol().concat("USDT");
+                    if (jjj == 1) {
+                        coinWatchlistOther.setCexPerpetual("");
+                    }
+                    if (resultMap.containsKey(baseTarget) || resultMap.containsKey("1000".concat(baseTarget))) {
+                        coinWatchlistOther.setCexPerpetual(coinWatchlistOther.getCexPerpetual() + "1");
+                    } else {
+                        coinWatchlistOther.setCexPerpetual(coinWatchlistOther.getCexPerpetual() + "0");
+                    }
+                }
+            } catch (Exception e) {
+                log.error("initCexFlag error,url={},paramMap={},result={}", String.format(coingeckoExchangeFuturesTickersUrl, coingeckoExchangeFutruesId), paramMap, result, e);
+                throw new RuntimeException("initCexFlag error");
+            }
+        }
+
+        int MAX_NUMBER2 = 10;
+        Stream.iterate(0, n -> n + 1).limit((watchlistOtherList.size() + MAX_NUMBER2 - 1) / MAX_NUMBER2)
+                .forEach(i -> {
+                    try {
+                        Thread.sleep(5000L);
+                    } catch (InterruptedException e) {
+                        throw new RuntimeException(e);
+                    }
+
+                    Map<String, CoinWatchlistOther> coinWatchlistOtherMap4Symbol = watchlistOtherList.stream().skip((long) i * MAX_NUMBER2).limit(MAX_NUMBER2).collect(Collectors.toMap(CoinWatchlistOther::getCoingeckoId, coinWatchlistOther -> coinWatchlistOther));
+                    initCexSpotFlag(coinWatchlistOtherMap4Symbol);
+                });
+
+        coinMapper.insertOrUpdateCoinWatchlistOtherList(watchlistOtherList);
+    }
+
     public void parseWatchlistMap4Coingecko(Map<String, CoinWatchlist> watchlistMap4Coingecko) {
         String coingeckoCoinsMarketsUrl = InitRunner.dicCodeMap.get("coingecko_coins_markets_url").getCodeValue();
         Map<String, String> headerMap = new HashMap<>();
@@ -2034,14 +2160,16 @@ public class CoinServiceImpl implements CoinService {
         symbolList.add("SNX");
         symbolList.add("REEF");
 
-        List<CoinWatchlist> watchlistList = coinMapper.findWatchlistList(params);
-        watchlistList = watchlistList.stream().filter(x -> symbolList.contains(x.getSymbol())).collect(Collectors.toList());
+        //List<CoinWatchlist> watchlistList = coinMapper.findWatchlistList(params);
+        // watchlistList = watchlistList.stream().filter(x -> symbolList.contains(x.getSymbol())).collect(Collectors.toList());
 
         //Map<String, CoinWatchlist> coinWatchlistMap4CoingeckoId = watchlistList.stream().collect(Collectors.toMap(CoinWatchlist::getCoingeckoId, coinWatchlist -> coinWatchlist));
         //parseWatchlistMap4Coingecko(coinWatchlistMap4CoingeckoId);
 
-        Map<Long, CoinWatchlist> coinWatchlistMap4CmcId = watchlistList.stream().collect(Collectors.toMap(CoinWatchlist::getCmcId, coinWatchlist -> coinWatchlist));
-        parseWatchlistMap4CmC(coinWatchlistMap4CmcId);
+        //Map<Long, CoinWatchlist> coinWatchlistMap4CmcId = watchlistList.stream().collect(Collectors.toMap(CoinWatchlist::getCmcId, coinWatchlist -> coinWatchlist));
+        // parseWatchlistMap4CmC(coinWatchlistMap4CmcId);
+
+        syncCexFlag();
     }
 
     @Override
@@ -2049,6 +2177,8 @@ public class CoinServiceImpl implements CoinService {
     public void initWatchlist(CoinWatchlist coinWatchlist2) {
         Map<String, Object> params = new HashMap<>();
         params.put("symbol", coinWatchlist2.getSymbol());
+        params.put("sortField", Collections.singletonList("create_time"));
+        params.put("sort", "desc");
         List<CoinWatchlist> watchlistListCKO = coinMapper.findWatchlistList(params);
 
         Map<String, CoinWatchlist> coinWatchlistMap4CoingeckoId = watchlistListCKO.stream().collect(Collectors.toMap(CoinWatchlist::getCoingeckoId, coinWatchlist -> coinWatchlist));