package com.philisense.www.utils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class JsuopUtil {
	

	public static Connection getConnection(String url) throws Exception {
		StringBuffer stringBuffer = new StringBuffer();
		if(url.contains("http")) {
			String[] urlString = url.split("/");
			stringBuffer.append(urlString[0]).append("//").append(urlString[1]).append(urlString[2]).append("/");
		}else {
			throw new Exception("请使用正确的URL地址");
		}

		
		Connection connect = Jsoup.connect(url);
		
		// 伪造请求头
        connect.header("Accept", "application/json, text/javascript, */*; q=0.01").header("Accept-Encoding",
                "gzip, deflate");
        connect.header("Accept-Language", "zh-CN,zh;q=0.9").header("Connection", "keep-alive");
        connect.header("Content-Length", "72").header("Content-Type",
                "application/x-www-form-urlencoded; charset=UTF-8");
        connect.header("Host", "qiaoliqiang.cn").header("Referer", stringBuffer.toString());
        connect.header("User-Agent",
                "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36")
                .header("X-Requested-With", "XMLHttpRequest");
        
        return connect;
	}
	
	public static Map<String, Object> getCapture(String url) throws Exception {
		Map<String, Object> resMap = new HashMap<String, Object>();
		Connection connect = getConnection(url);
		if(url.contains("mp.weixin.qq.com")) {
			resMap = getWeiXin(connect);
		}
		if(url.contains("xinhuanet.com")) {
			resMap = getXinHuaWang(connect);
		}
		if(url.contains("people.com.cn")) {
			resMap = getRenMinWang(connect);
		}
		if(url.contains("cpd.com.cn")) {
			resMap = getCpd(connect);
		}
		if(url.contains("cyberpolice.cn")) {
			resMap = getCyberpolice(connect);
		}
		return resMap;
		
	}
	
	
	public static Map<String, Object> getCapture(Connection connect, String title, String content, String author) throws IOException {
		Map<String, Object> resMap = new HashMap<String, Object>();
		Document doc = connect.get();
        Elements elements = doc.select(title);
        for(Element es :elements) {
        	 resMap.put("title", es.text());
        }
        
        Elements elements1 = doc.select(content);
        for(Element es1 :elements1) {
        	 resMap.put("content", es1.html());
        }
        
        Elements elements2 = doc.select(author);
        for(Element es2 :elements2) {
        	 resMap.put("author", es2.text());
        }
		return resMap;
		
	}
	
	/**
	 * 新华网
	 * @param connect
	 * @return
	 * @throws IOException
	 */
	public static Map<String, Object> getXinHuaWang(Connection connect) throws IOException {
		Map<String, Object> resMap = new HashMap<String, Object>();
		Document doc = connect.get();
        Elements elements = doc.select("div[class=h-title]");
        for(Element es :elements) {
        	 resMap.put("title", es.text());
        }
        
        Elements elements1 = doc.select("div[id=p-detail]");
        for(Element es1 :elements1) {
        	 resMap.put("content", es1.html());
        }
        
        Elements elements2 = doc.select("span[class=sub-src]");
        for(Element es2 :elements2) {
        	 resMap.put("author", es2.text());
        }
		return resMap;
		
	}
	
	/**
	 * 微信
	 * @param connect
	 * @return
	 * @throws IOException
	 */
	public static Map<String, Object> getWeiXin(Connection connect) throws IOException {
		Map<String, Object> resMap = new HashMap<String, Object>();
		Document doc = connect.get();
        Elements elements = doc.select("h2[id=activity-name]");
        for(Element es :elements) {
        	 resMap.put("title", es.text());
        }
        
        Elements elements1 = doc.select("div[id=js_content]");
        for(Element es1 :elements1) {
        	 resMap.put("content", es1.html());
        }
        
        Elements elements2 = doc.select("a[id=js_name]");
        for(Element es2 :elements2) {
        	 resMap.put("author", es2.html());
        }
		return resMap;
		
	}
	
	/**
	 * 人民网
	 * @param connect
	 * @return
	 * @throws IOException
	 */
	public static Map<String, Object> getRenMinWang(Connection connect) throws IOException {
		Map<String, Object> resMap = new HashMap<String, Object>();
		Document doc = connect.get();
        Elements elements = doc.select("h1");
        for(Element es :elements) {
        	 resMap.put("title", es.text());
        }
        
        Elements elements1 = doc.select("div[class=box_con]");
        for(Element es1 :elements1) {
        	 resMap.put("content", es1.html());
        }
        Elements elements2 = doc.select("div[class=box01]>div[class=fl]");
        for(Element es2 :elements2) {
        	 resMap.put("author", es2.text());
        }
		return resMap;
		
	}
	
	/**
	 * 人民警察网
	 * @param connect
	 * @return
	 * @throws IOException
	 */
	public static Map<String, Object> getCpd(Connection connect) throws IOException {
		Map<String, Object> resMap = new HashMap<String, Object>();
		Document doc = connect.get();
        Elements elements = doc.select("h1[class=hetitle_01]");
        for(Element es :elements) {
        	 resMap.put("title", es.text());
        }
        
        Elements elements1 = doc.select("div[class=news_contents hetitle_01]");
        for(Element es1 :elements1) {
        	 resMap.put("content", es1.html());
        }
        
        Elements elements2 = doc.select("div[class=newsattr z12]");
        for(Element es2 :elements2) {
        	 resMap.put("author", es2.text());
        }
		return resMap;
		
	}
	
	// 网络违法犯罪举报
	public static Map<String, Object> getCyberpolice(Connection connect) throws IOException {
		Map<String, Object> resMap = new HashMap<String, Object>();
		Document doc = connect.get();
        Elements elements = doc.select("td[class=titlebiaoti]>div");
        for(Element es :elements) {
        	 resMap.put("title", es.text());
        }
        
        Elements elements1 = doc.select("div[class=Section0]");
        for(Element es1 :elements1) {
        	 resMap.put("content", es1.html());
        }
		return resMap;
		
	}

}
