package jj.tech.paolu.utils.crawler;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;

import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.fluent.Executor;
import org.apache.http.client.fluent.Request;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.util.EntityUtils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import jj.tech.paolu.utils.YMHttp;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

public class Hgt8Processor implements PageProcessor {
	 private Site site = Site.me().setRetryTimes(3).setSleepTime(100);
	 
	 @Override
     public Site getSite() {
         return site;
     }
	 
	 @Override
     public void process(Page page) {
         page.addTargetRequests(page.getHtml().links().regex("(https://github\\.com/\\w+/\\w+)").all());
         page.putField("author", page.getUrl().regex("https://github\\.com/(\\w+)/.*").toString());
         page.putField("name", page.getHtml().xpath("//h1[@class='entry-title public']/strong/a/text()").toString());
         if (page.getResultItems().get("name")==null){
             //skip this page
             page.setSkip(true);
         }
         page.putField("readme", page.getHtml().xpath("//div[@id='readme']/tidyText()"));
     }
	 
	 public static void main(String[] args) throws Exception{
		 
		
		 
//	     Spider.create(new Hgt8Processor()).addUrl("https://sse8.idc9998.com").thread(1).run();
		 
		 
//		 Map<String, Object> param = new HashMap<>();
//	     param.put("lineCode", "be358");
//	     param.put("isSaveCode", "0");
//	     ObjectMapper objectMapper = new ObjectMapper();
//	        
//		 String stean = Request.Post("https://sse8.idc9998.com")
//				 
//					.connectTimeout(2500)
//					.socketTimeout(2500)
//					.bodyString(objectMapper.writeValueAsString(param), ContentType.APPLICATION_JSON)
//					.execute()
//					.returnContent()
//					.asString();
//		 System.err.println(stean);
		 
		
		 try {
			 
			 BasicCookieStore cookieStore = new BasicCookieStore();
			 BasicClientCookie cookie = new BasicClientCookie("name", "vale");
			 cookie.setDomain("idc9998.com");
			 cookie.setPath("cookie");
			 cookieStore.addCookie(cookie);
			 
			 
			 CloseableHttpClient httpclient = HttpClients.custom()
			     .setDefaultCookieStore(cookieStore)
			     .build();
			 
			 
			 ObjectMapper objectMapper = new ObjectMapper();
			 Map<String, Object> param = new HashMap<>();
		     param.put("lineCode", "be358");
		     param.put("isSaveCode", "0");
		     String paramString = objectMapper.writeValueAsString(param);
			 
			 HttpPost httpPost = new HttpPost("https://sse8.idc9998.com");
	
			 
			 StringEntity paramEntity = new StringEntity(paramString, ContentType.APPLICATION_JSON);
			 httpPost.setEntity(paramEntity);
	     
			 
			 CloseableHttpResponse response = httpclient
			            .execute(httpPost);
			 
			 HttpEntity respoetEntity = response.getEntity();
			 String responseString = EntityUtils.toString(respoetEntity, "UTF-8");
		     
		     System.out.println(responseString);
		     
		     
		     
		     
		     HttpPost httpPost1 = new HttpPost("https://sse8.idc9998.com/captcha_image/2?d="+"");
		     httpPost1.addHeader("content-type", ContentType.IMAGE_TIFF.toString());
		     
		     
		     CloseableHttpResponse response1 = httpclient
			            .execute(httpPost1);
		     
		     //System.err.println(EntityUtils.toString(response1.getEntity(), "UTF-8"));
		     HttpEntity entity = response1.getEntity();
		     ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		     entity.writeTo(byteArrayOutputStream);
		     
		     String image =
		     Base64.encodeBase64String(byteArrayOutputStream.toByteArray());
		     
		     System.err.println(image);
		     
//		     JsonNode jsonNode =
//		     YMHttp.customApi(image);
//		     System.out.println(jsonNode);
		     
		     
		     response.close();
		     response1.close();
	     
		 }catch (Exception e) {
			 System.out.println(e);
		 }
		 
		 
		 
		 
	 }
	 
}
