package cn.com.ctop.crawler.modules.core.util;

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

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author xuzuoyun
 * 2019年11月12日10:04:00
 */
public class KuaimiUtil {
    public static final String KUAIMI_API_URL = "http://api.caihyz.com/api/do.php";

    public static 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 static 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 static 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 getNumbers(result.split("\\|")[1]);
        } else {
            throw new Exception();
        }
    }

    public static 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 static 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();
        }
    }

    private static Pattern NUMBER_PATTERN = Pattern.compile("\\d+");
    public static String getNumbers(String content) {
        Matcher matcher = NUMBER_PATTERN.matcher(content);
        while (matcher.find()) {
            return matcher.group(0);
        }
        return "";
    }

    public static void main(String[] args) {
        String s = "【快手科技】重置密码的验证码469801，15分钟内有效，为保证信息安全，切勿将验证码告知他人。";
//        String s = "【快手科技】503730快手验证码，15分钟内有效，仅用于登录，请勿告知他人。";
//
//        KuaimiUtil k = new KuaimiUtil();
//        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();
//        }
    }
}
