package cn.com.ctop.crawler.modules.jiema.service.impl;

import cn.com.ctop.common.module.utils.HttpUtils;

public class KuaimiServiceImpl {
    public static final String KUAIMI_API_URL = "http://api.kmiyz.com/api/do.php";

    public String kuaimiLogin() throws Exception {
        String result = HttpUtils.httpPostNoParamRequest(KUAIMI_API_URL + "?action=loginIn&name=xuzuoyun&password=heaven01");
        if (result != null && result.startsWith("1")) {
            return result.split("\\|")[1];
        } else {
            throw new Exception();
        }
    }

    public String getMobile(String token, String projectId) throws Exception {
        String url = KUAIMI_API_URL + "?action=getPhone&sid=" + projectId + "&token=" + token;
        String result = HttpUtils.httpPostNoParamRequest(url);
        if (result != null && result.startsWith("1")) {
            return result.split("\\|")[1];
        } else {
            throw new Exception();
        }
    }

    public String getMessage(String token, String projectId, String phone) throws Exception {
        String url = KUAIMI_API_URL + "?action=getMessage&sid=" + projectId + "&token=" + token + "&phone=" + phone;
        String result = HttpUtils.httpPostNoParamRequest(url);
        if (result != null && result.startsWith("1")) {
            return result.split("\\|")[1].substring(6, 12);
        } else {
            throw new Exception();
        }
    }

    public String addBlackList(String token, String projectId, String phone) throws Exception {
        String url = KUAIMI_API_URL + "?action=addBlacklist&sid=" + projectId + "&token=" + token + "&phone=" + phone;
        String result = HttpUtils.httpPostNoParamRequest(url);
        if (result != null && result.startsWith("1")) {
            return result.split("\\|")[1];
        } else {
            throw new Exception();
        }
    }

    public String cancelRecv(String token, String projectId, String phone) throws Exception {
        String url = KUAIMI_API_URL + "?action=cancelRecv&sid=" + projectId + "&token=" + token + "&phone=" + phone;
        String result = HttpUtils.httpPostNoParamRequest(url);
        if (result != null && result.startsWith("1")) {
            return result.split("\\|")[1];
        } else {
            throw new Exception();
        }
    }

    public static void main(String[] args) {
        KuaimiServiceImpl k = new KuaimiServiceImpl();
        try {
            String token = k.kuaimiLogin();
            String mobile = k.getMobile(token, "2307");
            int times = 0;
            while (times < 100) {
                Thread.sleep(5000);
                try {
                    String msg = k.getMessage(token, "2307", mobile);
                    break;
                } catch (Exception e) {
                }
                times++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
