package com.zzsn.service;


import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Tag;
import org.jsoup.select.Elements;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.SocketUtils;
import org.springframework.web.client.RestTemplate;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.*;

public class Test {
    public static void main(String[] args) {
//        Set<String>  set = new HashSet<>();
//        set.add("哈喽");
//        set.add("哈喽");
//        set.forEach(s -> System.out.println(s));
//        set.remove("哈喽");
//        set.forEach(s -> System.out.println("移除后"+s));

//        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
//        requestFactory.setProxy(
//                new Proxy(
//                        Proxy.Type.HTTP,
//                        new InetSocketAddress("123.73.147.183", 32223)  //设置代理服务
//                )
//        );
//        RestTemplate restTemplate = new RestTemplate(requestFactory);
        RestTemplate restTemplate = new RestTemplate();


//        HttpHeaders headers = new HttpHeaders();
//        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
//
//        MultiValueMap<String, String> map= new LinkedMultiValueMap<String, String>();
//        map.add("text", null);
//
//        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);
//
//        ResponseEntity<String> response = restTemplate.postForEntity( "http://localhost:8018/extract/", request , String.class );
//        String body = response.getBody();
//        System.out.println("---->"+body);

//        ResponseEntity<String> forEntity = restTemplate.getForEntity("http://www.baidu.com/s?wd=郑州市人民政府", String.class);
//        String body = forEntity.getBody();
//        System.out.println("---------->" + body);
        Map<String, String> map = new HashMap<>();
        map.put("name", "海南藏族自治州人民政府");
        ResponseEntity response = restTemplate.getForEntity("http://www.baidu.com/s?wd={name}", String.class, map);

        System.out.println("------->" + response.getStatusCode());
        System.out.println("------->" + response.getStatusCodeValue());
        String body = (String) response.getBody();
        Document parse = Jsoup.parse(body);
        Elements elementsByClass = parse.getElementsByClass("OP_LOG_LINK c-text c-text-public c-text-mult c-gap-left-small");
        if(!(elementsByClass.size()>0)){
            //带官方的元素为空，说明没有
            System.out.println("没有带官方的网站");;
        }
        //获取第一个带官方的元素
        Element element = elementsByClass.get(0);
        Element element2 = element.previousElementSibling();
//        Element parent = element.parent().firstElementSibling();
//        Element element1 = element.parent().nextElementSibling();
//        Elements elementsByClass1 = element1.getElementsByClass("c-showurl c-color-gray");
//        Element element2 = elementsByClass1.get(0);
        String text = element2.text();
//        if(text.contains(".gov.cn")){
//            System.out.println("---网址内容"+text);
//        }else{
            //如果不是，而是文本
            String href = element2.attr("href");
            System.out.println("href"+href);
            String href1 = href.replace("http","https")+"&wd=&eqid=";
            System.out.println("href1"+href1);
            ResponseEntity response1 = restTemplate.getForEntity(href1, String.class);
//            HttpHeaders headers = response1.getHeaders();
//            List<String> location = headers.get("Location");
//            System.out.println(location.get(0));
            String body1 = (String) response1.getBody();
            Document parse1 = Jsoup.parse(body1);
            Elements noscript = parse1.getElementsByTag("noscript");
            Element element3 = noscript.get(0);
            Elements meta = element3.getElementsByTag("meta");
            String url = meta.attr("content");
            String s = "";
            if(StringUtils.isNotBlank(url)){
                int i = url.indexOf("'");
                if(i!=-1 && i+1<url.length()-1){
                     s = url.substring(i + 1, url.length() - 1);
                }
            }
            System.out.println("文字的政府地址为："+s);
//        }
    }
}
