码迷,mamicode.com
首页 > 系统相关 > 详细

自动读取虚拟币行情并提醒的小工具(mac OSX)

时间:2017-05-06 16:34:51      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:tps   html   etc   nts   time   runtime   adt   color   charset   

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.IOException;

public class Price{

    public static String URL_PREFIX = "https://trade.chbtc.com/api/";
    public static String API_DOMAIN = "http://api.chbtc.com";
    public static float etcPrice = 0;
    public static boolean noticed = false;
    public static String win = "你发财了,你发财了,你发财了,你发财了,你发财了,你发财了,你发财了,你发财了,你发财了,你发财了,你发财了,你发财了,你发财了,你发财了,你发财了,你发财了,你发财了,你发财了,你发财了,你发财了,你发财了,你发财了,你发财了,你发财了,";
    public static String lose = "要赔哭了,要赔哭了,要赔哭了,要赔哭了,要赔哭了,要赔哭了,要赔哭了,要赔哭了,要赔哭了,要赔哭了,要赔哭了,要赔哭了,要赔哭了,要赔哭了,要赔哭了,要赔哭了,要赔哭了,要赔哭了,要赔哭了,要赔哭了,要赔哭了,要赔哭了,";
    public static void main(String []args){
        try{
            while(true){
                testTicker("btc_cny");
                testTicker("ltc_cny");
                testTicker("etc_cny");
                testTicker("eth_cny");
                System.out.println((int)(742.103 * etcPrice - 33200));
                // System.out.println("");
                if(!noticed){
                    if((742.103 * etcPrice - 33200) > 5000)
                        say(win);    
                    else if((742.103 * etcPrice - 33200) < 1000)
                        say(lose);
                }
                Thread.sleep(5000);
                
            }
        }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)+"\t");
            if(currency.equals("etc_cny")){
                etcPrice = Float.valueOf(callback.substring(start+7,end));
            }
        } 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("\r\n");
            }
            reader.close();
            result = sbf.toString();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void say(String content){
        try    
         {    
            //执行命令    
            noticed = true;
            Runtime.getRuntime().exec("say \""+content+"\"");
         }    
        catch (IOException e)    
         {    
            e.printStackTrace();    
         }    
    }
}

使用chbtn的API,可以语音提醒。

使用时直接

javac Price.java

java Price

自动读取虚拟币行情并提醒的小工具(mac OSX)

标签:tps   html   etc   nts   time   runtime   adt   color   charset   

原文地址:http://www.cnblogs.com/xirtam/p/6817028.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!