zoukankan      html  css  js  c++  java
  • 自动读取虚拟币ETC行情并语音提醒的小工具(mac OSX)

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    public class Price {
    
        //默认配置无需修改
        public static String API_DOMAIN = "http://api.chbtc.com";
        public static String WIN_STR = "你发财了,你发财了,你发财了,你发财了,你发财了,你发财了";
        public static String LOSE_STR = "要赔哭了,要赔哭了,要赔哭了,要赔哭了,要赔哭了,要赔哭了";
        public static String CUR_PRICE_STR = "当前价格";
        public static String PRICE_UP_STR = "上涨突破";
        public static String PRICE_DOWN_STR = "下跌突破";
        public static String YUAN_STR = "元";
        public static String[] bollNotice = new String[]{"b o l l跌破最低值", "b o l l处于低位", "b o l l处于高位", "b o l l突破最高值",};
        public static String[] bollName = new String[]{"最低","低","高","最高"};
        public static String BTC = "btc_cny";
        public static String LTC = "ltc_cny";
        public static String ETC = "etc_cny";
        public static String ETH = "eth_cny";
    
        //程序动态使用数值
        public static int reawrd = 0;
        public static float etcPrice = 0;
        public static int lastPriceInt = 0;
        public static float mb = 0;
        public static float up = 0;
        public static float dn = 0;
    
        public static int state = 0;
    
        //用户配置数值
        public static float coinCount = 742.103f;//持币数量
        public static float sourceMoney = 33200;//总成本
        public static float max_price = 5000;//超过此价格,提醒发财
        public static float min_price = 0;//跌破此价格,提醒赔哭
        public static int sleepTime = 5000;//查询刷新间隔,建议不要太快,单位毫秒
    
        public static void main(String[] args) {
            if (args != null) {
                if (args.length >= 2) {
                    min_price = Float.valueOf(args[0]);
                    max_price = Float.valueOf(args[1]);
                } else if (args.length == 1) {
                    max_price = Float.valueOf(args[0]);
                }
            }
    
            try {
                while (true) {
                    testTicker(BTC);
                    testTicker(LTC);
                    getKline();
                    testTicker(ETC);
                    testTicker(ETH);
                    reawrd = (int) (coinCount * etcPrice - sourceMoney);
    
                    //价格提醒
                    if (max_price != 0 && etcPrice > max_price) say(WIN_STR);
                    else if (min_price != 0 && etcPrice < min_price) say(LOSE_STR);
    
                    //boll提醒
                    int lastState = state;
                    if (etcPrice < dn) {
                        state = 0;
                    } else if (etcPrice > dn && etcPrice < mb) {
                        state = 1;
                    } else if (etcPrice > mb && etcPrice < up) {
                        state = 2;
                    } else if (etcPrice > up) {
                        state = 3;
                    }
    
                    System.out.println("boll " + bollName[state]);
    
                    if (state != lastState) {
                        say(bollNotice[state]);
                    }
                    Thread.sleep(sleepTime);
    
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 获取行情
         */
        public static void testTicker(String currency) {
            try {
                // String currency = "btc_cny";
                // 请求地址
                String url = API_DOMAIN + "/data/v1/ticker?currency=" + currency;
                // 请求测试
                String callback = get(url, "UTF-8");
                int start = callback.indexOf(""buy":"");
                int end = callback.indexOf("","high"");
    
                System.out.print(currency.substring(0, 3) + " " + callback.substring(start + 7, end) + "	");
                // System.out.print(currency.substring(0, 3) + " " + callback.substring(start + 7, end) + "	" + ", mb " + mb + ", up " + up + " , dn " + dn);
    
                if (currency.equals(ETC)) {
                    float curPrice = Float.valueOf(callback.substring(start + 7, end));
                    if (lastPriceInt == 0) {
                        say(CUR_PRICE_STR + curPrice + YUAN_STR);
                        lastPriceInt = (int) curPrice;
                    } else if ((int) curPrice > lastPriceInt) {
                        say(CUR_PRICE_STR + PRICE_UP_STR + curPrice + YUAN_STR);
                        lastPriceInt = (int) curPrice;
                    } else if ((int) curPrice < lastPriceInt) {
                        say(CUR_PRICE_STR + PRICE_DOWN_STR + curPrice + YUAN_STR);
                        lastPriceInt = (int) curPrice;
                    }
                    etcPrice = curPrice;
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    
        /**
         * @param urlAll  :请求接口
         * @param charset :字符编码
         * @return 返回json结果
         */
        public static String get(String urlAll, String charset) {
            BufferedReader reader = null;
            String result = null;
            StringBuffer sbf = new StringBuffer();
            String userAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36";// 模拟浏览器
            try {
                URL url = new URL(urlAll);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.setReadTimeout(30000);
                connection.setConnectTimeout(30000);
                connection.setRequestProperty("User-agent", userAgent);
                connection.connect();
                InputStream is = connection.getInputStream();
                reader = new BufferedReader(new InputStreamReader(is, charset));
                String strRead = null;
                while ((strRead = reader.readLine()) != null) {
                    sbf.append(strRead);
                    sbf.append("
    ");
                }
                reader.close();
                result = sbf.toString();
    
            } catch (Exception e) {
                e.printStackTrace();
            }
            return result;
        }
    
        public static void say(String content) {
            try {
                //执行命令    
                Runtime.getRuntime().exec("say "" + content + """);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    
        public static void countBOLL(float[] cs) {
            int n = 20;
            int k = 2;
            float endSum = 0;
            for (int i = 0; i < cs.length; i++) {
                endSum += cs[i];
            }
            float ma = endSum / n;
    
            float mdSum = 0;
            for (int i = 0; i < cs.length; i++) {
                mdSum += (cs[i] - ma) * (cs[i] - ma);
            }
            float md = (float) Math.sqrt(mdSum / n);
    
            float mb = (endSum - cs[n - 1]) / (n - 1);
            float up = mb + k * md;
            float dn = mb - k * md;
    
            Price.mb = mb;
            Price.up = up;
            Price.dn = dn;
    
    //        System.out.println("up " + up + " ,mb" + mb + " ,dn " + dn);
        }
    
        public static void getKline() {
            try {
                String currency = "btc_cny";
                // 请求地址
                String url = "http://api.chbtc.com/data/v1/kline?currency=etc_cny&type=5min&size=20";
                // 请求测试
                String callback = get(url, "UTF-8");
                String[] split = callback.split(",");
                int offsex = 0;
                int index = 0;
                float cs[] = new float[20];
                for (int i = 0; i < split.length; i++) {
                    if (i == 0) {
                        offsex += 4;
                    } else {
                        offsex += 6;
                    }
                    if (offsex < split.length) {
                        cs[index] = Float.valueOf(split[offsex]);
                        index++;
                    }
    
                }
                countBOLL(cs);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    
    }

    使用chbtn的API,整数价格变化自动语音提醒,可以自行配置价格语音提醒。
    使用时直接

    javac Price.java

    java Price

     

    更多say命令的使用方式请搜索 mac命令行唱歌

  • 相关阅读:
    Springboot vue 前后分离 跨域 Activiti6 工作流 集成代码生成器 shiro权限
    mybatis3批量更新 批量插入
    Java GC的工作原理详解
    Hadoop的Map侧join
    cut 命令
    head 与 tail
    常用正则
    vim 设置
    Java泛型初探
    linux修改PS1,自定义命令提示符样式
  • 原文地址:https://www.cnblogs.com/xirtam/p/6817028.html
Copyright © 2011-2022 走看看