package com.zzsn.extractor;


import com.zzsn.extractor.web.Processor;
import com.zzsn.extractor.web.ProcessorReader;
import com.zzsn.util.Constants;
import com.zzsn.util.ContentUtility;
import com.zzsn.util.FileUtil;
import org.apache.http.*;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;
import org.jsoup.nodes.Element;

import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.nio.charset.UnsupportedCharsetException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class WeiXinDispatch {

	
	 public List<String> testMatchTime(String siteTimeMatch, String scopeHtml)
	    {
	        Pattern p=Pattern.compile(siteTimeMatch);
	        Matcher m= p.matcher(scopeHtml);
	        List<String> result = new ArrayList<String>();
	        while (m.find()) {
				result.add(m.group());
			}
	        
	        return result;
	    }
	 
	 private String matchtime(String matchExp,Element element){
	        if(matchExp==null){
	            return null;
	        }
	        matchExp= matchExp.trim();
	        if(matchExp.length()==0){
	            return null;
	        }
	        Element temp = element;
	        try{
	        String[] steps = matchExp.split("@");
	        if(steps.length!=2 ){
	        	return null;
	        }
	        //获取元素上几级包含时间
	        String parant=steps[0];
	        /*Integer lenth=0;
	        try {
				 lenth=Integer.parseInt(parants);
			} catch (Exception e) {
				System.out.println("转换出错");
				return null;
			}*/
	      /*  for (int i = 0; i < lenth; i++) {
	        	temp = temp.parent();
			}*/
	        String [] parants=parant.split("_");
	        for(String step : parants){
	            if(temp==null){
	                break;
	            }
	            if(step.equals("parent")){
	                temp = temp.parent();
	            }else if(step.equals("prev")){
	                temp = temp.previousElementSibling();
	            }else if(step.equals("next")){
	                temp = temp.nextElementSibling();
	            }
	        }
	        String html=temp.html();
	        Pattern p=Pattern.compile(steps[1]);
	        Matcher m= p.matcher(html);
	        while (m.find()) {
				return m.group();
			}
	        
	        }catch(Exception e){
	            return null;
	        }
	        return null;
	    }
	 
	
	 private String convertRelativePath(String currentURI, String relativePath)
	    {
	        int count = 0;
	        if(relativePath.matches("^\\.\\./.*"))
	        {
	            for(; relativePath.matches("^\\.\\./.*"); relativePath = relativePath.replaceFirst("^\\.\\./", ""))
	                count++;

	            count++;
	        } else
	        if(relativePath.matches("^\\./.*"))
	            for(; relativePath.matches("^\\./.*"); relativePath = relativePath.replaceFirst("^\\./", ""))
	                count++;

	        for(int last = currentURI.lastIndexOf("/"); last > 8 && count > 0; count--)
	        {
	            currentURI = currentURI.substring(0, last);
	            last = currentURI.lastIndexOf("/");
	        }

	        return (new StringBuilder(String.valueOf(currentURI))).append("/").append(relativePath).toString();
	    }
	  protected String getBaseHref(String content)
	    {
	        String contentPrefix = "";
	        if(contentPrefix.length() > 3000)
	            contentPrefix = content.substring(0, 3000);
	        else
	            contentPrefix = content;
	        String href = null;
	        Matcher matcher = Pattern.compile("(?is)<base\\s+[^>]*href\\s*=\\s*['\"]*([^\"|'|\\s]+)['\"]*[^>]*>").matcher(contentPrefix);
	        if(matcher.find())
	            href = matcher.group(1);
	        if(href != null)
	            href = href.replace(":80", "");
	        return href;
	    }
	  
	  private String getBaseHrefByUrl(String url)
	    {
	        if(url.endsWith("/"))
	            return url;
	        int index = url.lastIndexOf("/");
	        if(index != -1 && index > 8)
	            url = url.substring(0, index);
	        return url;
	    }
	  
	  
	  public static CloseableHttpClient createSSLClientDefault(){
	        try {
	             SSLContext sslContext = new SSLContextBuilder().useProtocol("TLSv1.2").loadTrustMaterial(null, new TrustStrategy() {
	                 //信任所有
					@Override
					public boolean isTrusted(
							java.security.cert.X509Certificate[] arg0, String arg1)
							throws java.security.cert.CertificateException {
						// TODO Auto-generated method stub
						return true;
					}
					
	             }).build();
	             SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
	             return HttpClients.custom().setSSLSocketFactory(sslsf).build();
	         } catch (KeyManagementException e) {
	             e.printStackTrace();
	         } catch (NoSuchAlgorithmException e) {
	             e.printStackTrace();
	         } catch (KeyStoreException e) {
	             e.printStackTrace();
	         }
	        return  HttpClients.createDefault();
	    }    
	  public String testScopeTag(String siteURI, String scopetags)
	    {
	        String result = "";
	        Processor p = ProcessorReader.getScopeProcessor(scopetags);
	        String siteHtml = "";
	        HttpResponse re = getmethodNokey(siteURI);
	        try
	        {
	        	siteHtml= FileUtil.readHtml(re.getEntity().getContent(), Constants.READ_CHARSET);;
	        }
	        catch(Exception e)
	        {
	            e.printStackTrace();
	        }
	        if(p == null)
	            result = siteHtml;
	        else
	            try
	            {
	                result = p.extract(siteHtml);
	            }
	            catch(Exception e)
	            {
	                result = siteHtml;
	            }
	        result = ContentUtility.convertHtmlToUtf8(result,re.getFirstHeader("Content-Type"));
	        return result;
	    }
	  
	  public  HttpResponse getmethodNokey(String url){
			CloseableHttpClient httpClient = null;
		  httpClient = createSSLClientDefault();
			// HttpClient httpClient = new HttpClient();
		  HttpGet httpGet = null;
			httpGet = null;
			CloseableHttpResponse getMethod1 = null;
			HttpResponse getMethod = null;
			HttpClient client = null;
			httpGet = new HttpGet(url);// Get请求

//			httpGet.getParams().setIntParameter(
//					CoreConnectionPNames.CONNECTION_TIMEOUT, 60000);
//			httpGet.getParams().setParameter(HttpMethodParams.SO_TIMEOUT,
//					60000);
		  RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(500000).setConnectionRequestTimeout(100000).setSocketTimeout(500000).build();
		  httpGet.setConfig(requestConfig);
			// 伪装成浏览器
			httpGet.setHeader("Content-Type",
					"application/x-www-form-urlencoded;charset=utf-8");
			httpGet.setHeader(HttpHeaders.CONNECTION, "close");
			try {
				getMethod1 = httpClient.execute(httpGet);
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} // 发送请求
			getMethod = getMethod1;
			
		  return getMethod;
	  }
	  public ExtEntity getExtractorElement(String url)
	    {
	        
	            HttpResponse getMethod = getmethodNokey(url);
	            List processors = ProcessorReader.readWeChatProcessors();
	            String contentType = ContentUtility.getContentType(getMethod);
	            Extractor extractor = null;
	            ExtEntity entity = new ExtEntity();
	            if(contentType.equals("HTML")){
	            	
	                extractor = new WebExtractorImplforweixin(processors, getMethod);
	            }else{
	            	return null;
	            }
	            
	            try
	            {
	                extractor.readEntity(url, getMethod.getEntity().getContent());
	                extractor.process(entity);
	            }
	            catch(IOException e)
	            {
	                e.printStackTrace();
	            }
	            catch(Exception e)
	            {
	                e.printStackTrace();
	            }
	        

	        return entity;
	    }
	  
	  public  HttpResponse gettarget(String url){
			CloseableHttpClient httpClient = null;
		  httpClient = createSSLClientDefault();
		  HttpContext httpContext = new BasicHttpContext();
			// HttpClient httpClient = new HttpClient();
		  HttpGet httpGet = null;
			httpGet = null;
			CloseableHttpResponse getMethod1 = null;
			HttpResponse getMethod = null;
			HttpClient client = null;
			httpGet = new HttpGet(url);// Get请求

//			httpGet.getParams().setIntParameter(
//					CoreConnectionPNames.CONNECTION_TIMEOUT, 60000);
//			httpGet.getParams().setParameter(HttpMethodParams.SO_TIMEOUT,
//					60000);
		  RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(500000).setConnectionRequestTimeout(100000).setSocketTimeout(500000).build();
		  httpGet.setConfig(requestConfig);
			// 伪装成浏览器
			httpGet.setHeader("Content-Type",
					"application/x-www-form-urlencoded;charset=utf-8");
			httpGet.setHeader(HttpHeaders.CONNECTION, "close");
			try {
				getMethod1 = httpClient.execute(httpGet,httpContext);
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} // 发送请求
			getMethod = getMethod1;
			
			//HttpHost targetHost = (HttpHost)httpContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
			HttpUriRequest realRequest = (HttpUriRequest)httpContext.getAttribute(ExecutionContext.HTTP_REQUEST);
			//System.out.println("主机地址:" + targetHost);
			System.out.println("URI信息:" + realRequest.getURI());
			HttpEntity entity = getMethod.getEntity();
			if(null != entity){
				try {
					System.out.println("响应内容:" + EntityUtils.toString(entity, ContentType.getOrDefault(entity).getCharset()));
				} catch (UnsupportedCharsetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				try {
					EntityUtils.consume(entity);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		  return getMethod;
	  }
	  public static void getRedirectInfo(){
			HttpClient httpClient = new DefaultHttpClient();
			HttpContext httpContext = new BasicHttpContext();
			HttpGet httpGet = new HttpGet("http://m.toutiao.com/group/6793100121849463308/");
			try {
				//将HttpContext对象作为参数传给execute()方法,则HttpClient会把请求响应交互过程中的状态信息存储在HttpContext中
				HttpResponse response = httpClient.execute(httpGet, httpContext);
				//获取重定向之后的主机地址信息,即"http://127.0.0.1:8088"
				HttpHost targetHost = (HttpHost)httpContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
				//获取实际的请求对象的URI,即重定向之后的"/blog/admin/login.jsp"
				HttpUriRequest realRequest = (HttpUriRequest)httpContext.getAttribute(ExecutionContext.HTTP_REQUEST);
				System.out.println("主机地址:" + targetHost);
				System.out.println("URI信息:" + realRequest.getURI());
				HttpEntity entity = response.getEntity();
				if(null != entity){
					System.out.println("响应内容:" + EntityUtils.toString(entity, ContentType.getOrDefault(entity).getCharset()));
					EntityUtils.consume(entity);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				httpClient.getConnectionManager().shutdown();
			}
	  }
	 public static void main(String[] args) {
		 
		// getRedirectInfo();
		//HttpResponse s=new WeiXinDispatch().gettarget("http://m.toutiao.com/group/6793100121849463308/");
		HttpResponse s=new WeiXinDispatch().getmethodNokey("http://www.toutiao.com/a6793100121849463308/");
		try {
			String content=FileUtil.readHtml(s.getEntity().getContent(),Constants.READ_CHARSET);
			System.out.println(content);
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		/*
		 * String patt = "\\d{4}\\-\\d{1,2}\\-\\d{1,2}"; String
		 * html="sdf s搭建手动2018-01-30  2018-01-30 2018-01-30 2018-01-30"; Pattern
		 * p=Pattern.compile(patt); Matcher m=p.matcher(html); while (m.find()) {
		 * System.out.println(1); System.out.println(m.group(0)); }
		 * 
		 * 
		 * String testStr = "2018-01-30 1111"; Pattern p1 =
		 * Pattern.compile("\\d{4}\\-\\d{1,2}\\-\\d{1,2}"); Matcher m1 =
		 * p1.matcher(testStr);
		 * 
		 * while (m1.find()) { System.out.println(1); System.out.println(m1.group(0)); }
		 * 
		 * Pattern pattern = Pattern.compile("\\w+"); Matcher matcher =
		 * pattern.matcher("hello abc bbc cbc ccc");
		 * 
		 * //find向前迭代 while(matcher.find()){ System.out.println(matcher.group()); }
		 * 
		 * Pattern pattern1 = Pattern.compile("(\\w+)\\s\\d+"); Matcher matcher1 =
		 * pattern1.matcher("hello 123 abc bbc cbc ccc"); matcher1.find();
		 * System.out.println(22222); System.out.println(matcher1.group(0));
		 * 
		 * while(matcher.find()){ System.out.println(matcher.group()); }
		 * 
		 * Pattern pattern2 = Pattern.compile("(\\w+)\\d+"); Matcher matcher2 =
		 * pattern2.matcher("hello123 abc bbc cbc ccc"); matcher2.find();
		 * System.out.println(matcher2.groupCount());
		 */
		 
		 
	 }
	 
	 
}
