package com.zzsn.test;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SougouTest {
    public static void main(String[] args) throws Exception{String url="http://114.115.130.239:8005/sougou_wechart/?query=铁甲防务";
        //这个属于发起请求并获取json数据（得引入hutool，即上面引入的第二个依赖）
        String json= HttpUtil.createGet(url).execute().body();
        //转化请求的 json 数据
        JSONObject jsonObject = JSONObject.parseObject(json);
        //获取 error 返回状态码
        String latestNewsTime = jsonObject.getString("latestNewsTime");
        System.out.println(latestNewsTime);
//        SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
//        long lt = new Long( "1652091763000");
//        //将时间戳转换为时间
//        Date date = new Date(lt);
//        //将时间调整为yyyy-MM-dd HH:mm:ss时间样式
//        String oldTime = simpleFormat.format(date);
//        String nowTime = simpleFormat.format(new Date());
//        System.out.println(oldTime);
//        System.out.println(nowTime);
//        int minutes = getHours(oldTime, nowTime);
//        System.out.println(minutes);
//        String s = get("https://weixin.sogou.com/weixin?type=1&s_from=input&query=%E5%85%A8%E7%90%83TMT");
//        Document document = Jsoup.parse(s);
//        Elements elements = document.select("div[class=\"news-box\"]>ul[class=\"news-list2\"]>li");
//        for (Element element :elements) {
//            String name = element.select("p[class=\"tit\"]>a>em").text();
//            String wxhao = element.select("label[name=\"em_weixinhao\"]").text();
//            String title = element.select("dl:eq(2)>dd>a").text();
//            String index  = element.select("dl:eq(2)>dd>span>script").outerHtml();
//            String time="";
//            String matchUrl="\\d{1,}";
//            Pattern pattern = Pattern.compile(matchUrl);
//            Matcher matcher = pattern.matcher(index);
//            if(matcher.find()){
//                time=matcher.group();
//            }
//
//            System.out.println(name);
//            System.out.println(wxhao);
//            System.out.println(title);
//            System.out.println(time);
//        }
    }

    public static int getHours( String oldTime,String nowTime){
        SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        long from = 0;
        long to=0;
        try {
            from = simpleFormat.parse(oldTime).getTime();
            to = simpleFormat.parse(nowTime).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int hours = (int) ((to - from)/(1000 * 60*60));
        return hours;
    }
    /**
     * 发送 get请求
     */
    public static String  get(String url) {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        String content="";
        try {
            // 创建httpget.
            HttpGet httpget = new HttpGet(url);
//            httpget.addHeader("token"," token");
            httpget.addHeader("Connection","keep-alive");
            httpget.addHeader("Cache-Control","max-age=0");
            httpget.addHeader("Upgrade-Insecure-Requests","1");
            httpget.addHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36");
            httpget.addHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
            httpget.addHeader("Sec-Fetch-Site","none");
            httpget.addHeader("Sec-Fetch-Mode","navigate");
            httpget.addHeader("Sec-Fetch-User","?1");
            httpget.addHeader("Sec-Fetch-Dest","document");
            httpget.addHeader("Accept-Language","zh-CN,zh;q=0.9");
            httpget.addHeader("Cookie","IPLOC=CN4101; SUV=001D46FC7D29AE7D61163051AB952530; SUID=D381343DC830A40A0000000061163173; GOTO=Af121011; SNUID=FD28FF2CF0F53E5DB8F35ED0F18CD145; ABTEST=0|1634614242|v1; weixinIndexVisited=1; JSESSIONID=aaaa12QKYfTMweWDxHhUx; ld=Kyllllllll2Pb6lklllllpVmyjllllllKvum1kllll9lllll9Zlll5@@@@@@@@@@");


            System.out.println("executing request " + httpget.getURI());
            // 执行get请求.
            CloseableHttpResponse response = httpclient.execute(httpget);
            try {
                // 获取响应实体
//                HttpEntity entity = response.getEntity();
                System.out.println("--------------------------------------");
                // 打印响应状态
                System.out.println(response.getStatusLine());
                if (response.getStatusLine().getStatusCode() == 200) {
                    HttpEntity entity = response.getEntity();
                    String message = EntityUtils.toString(entity, "utf-8");
                    System.out.println(message);
                    content=message;
                } else {
                    System.out.println("请求失败");
                }

//                if (entity != null) {
//                    // 打印响应内容长度
//                    System.out.println("Response content length: " + entity.getContentLength());
//                    // 打印响应内容
////                    System.out.println("Response content: " + EntityUtils.toString(entity));
//                    String message = EntityUtils.toString(entity, "utf-8");
//                    content=message;
//                }
                System.out.println("------------------------------------");
            } finally {
                response.close();
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        }  catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭连接,释放资源
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return content;
    }
}
