package com.zzsn.search;

import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import javax.imageio.ImageIO;

import cn.hutool.core.codec.Base64;
import com.alibaba.fastjson.JSONObject;
import com.zzsn.entity.DocInfo;
import com.zzsn.extrator.ExtEntity;
import com.zzsn.extrator.ProcessorReader;
import com.zzsn.extrator.WebExtractorImplforweixin;
import com.zzsn.orc.BaiduOCR;
import com.zzsn.util.Constants;
import com.zzsn.util.ContentFileFinder;
import com.zzsn.util.ContentUtility;
import com.zzsn.util.FileTag;
import org.apache.commons.io.FileUtils;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

//import ru.yandex.qatools.ashot.AShot;
//import ru.yandex.qatools.ashot.Screenshot;
//import ru.yandex.qatools.ashot.shooting.ShootingStrategies;

public class ChromeJietu {
	public static String redis_name="weixin4";
	//private static MqSender mqSender=DBServiceFactory.getMqSender();
	static ChromeOptions    options1=new ChromeOptions() ;
	static WebDriver driver;
	static{
		  System.setProperty("webdriver.chrome.driver", Constants.CHROMEDRIVE);//chromedriver服务地址
	        System.setProperty("webdriver.chrome.bin", Constants.CHROMEBIN); 
	       
	        options1.setBinary(Constants.CHROMEBIN); 
	        //options1.addArguments("--headless");
	      /*  Map<String, Object> contentSettings = new HashMap<String, Object>();
	        contentSettings.put("images", 2);
	         
	        Map<String, Object> preferences = new HashMap<String, Object>();
	        preferences.put("profile.default_content_settings", contentSettings);*/

	   

	        driver =new ChromeDriver(options1);
	}

	public static String getChromeDoc(String url){
		
	             //新建�?��WebDriver 的对象，但是new 的是FirefoxDriver的驱�?
	        try {
				driver.get("https://www.google.com");
				//打开指定的网�?
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				//e1.printStackTrace();
				  driver =new ChromeDriver(options1);
				  driver.get("https://www.google.com");
			}
	        try {
	        	WebElement searchBox2 = driver.findElement(By.name("q"));
	        	searchBox2.sendKeys(url);
	        	Thread.sleep(2000);
	        	WebElement searchBox3 = driver.findElement(By.name("btnK"));
	        	searchBox3.click();
	        	Thread.sleep(2000);
	        	String local=driver.getCurrentUrl();
	        	String realUrl=local+"&hl=en&lr=lang_en&tbm=nws&num=20&&tbs=qdr:d";
	        	driver.get(realUrl);
				Thread.sleep(2000);
				String doc=driver.getPageSource();
			//	driver.close();
				return doc;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        
	        /**
	         * dr.quit()和dr.close()都可以�?出浏览器,�?��的说�?��两�?的区别：第一个close�?
	         * 如果打开了多个页面是关不干净的，它只关闭当前的一个页面�?第二个quit�?
	         * 是�?出了�?��Webdriver�?��的窗口，�?��非常干净，所以推荐使用quit�?���?��case�?��的方法�?
	         */
		return null;
	}
	
	public static List<ExtEntity> getSougouweixin(String key){
		List<ExtEntity> entitys=new ArrayList<ExtEntity>();
   try {
	   driver.get("https://weixin.sogou.com/");
	} catch (Exception e1) {
		  driver =new ChromeDriver(options1);
		  driver.get("https://weixin.sogou.com/");
	}
   try {
   	WebElement searchBox2 = driver.findElement(By.id("query"));
   	searchBox2.sendKeys(key);
   	Thread.sleep(2000);
   	WebElement searchBox3 = driver.findElement(By.className("swz"));
   	searchBox3.click();
   	Thread.sleep(2000);
	WebElement searchBox4 = driver.findElement(By.id("tool_show"));
   	searchBox4.click();
   	Thread.sleep(2000);
   	WebElement searchBox6 = driver.findElement(By.className("btn-time"));
   	searchBox6.click();
   	Thread.sleep(2000);
	WebElement searchBox5 = driver.findElement(By.linkText("一天内"));
   	searchBox5.click();
   	Thread.sleep(2000);
   	List<WebElement> searchBoxs=	driver.findElement(By.className("news-list")).findElements(By.tagName("h3"));
   	if(searchBoxs.size()>0){
   		System.out.println(searchBoxs.size());
   		System.out.println(driver.getCurrentUrl());
   		for (int i = 0; i < 1; i++) {
   			String titlelike=searchBoxs.get(i).getText();
   			searchBoxs.get(i).click();
   			
   			Thread.sleep(3000);
		}
   		
   		
   		
   		
   	}
   	Set<String> ss=driver.getWindowHandles();
		System.out.println(ss.toString());
		String curenthandle=driver.getWindowHandle();
		
		for (String str : ss) {
			if(str.equals(curenthandle)){
				continue;
				
			}
		WebDriver dirverother=	driver.switchTo().window(str);
		System.out.println(dirverother.getCurrentUrl());
		System.out.println(dirverother.getPageSource().length());
		String result=dirverother.getPageSource();
		List processors = ProcessorReader.readWeChatProcessors();
		WebExtractorImplforweixin extractor = new WebExtractorImplforweixin(processors, result);
		ExtEntity entity = new ExtEntity();
		extractor.process(entity);
		System.out.println(entity.getTitle());
		entity.setUri(dirverother.getCurrentUrl());
		entitys.add(entity);
		}
   	
		Thread.sleep(2000);
		driver.quit();
		return entitys;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	   
	   /**
	    * dr.quit()和dr.close()都可以�?出浏览器,�?��的说�?��两�?的区别：第一个close�?
	    * 如果打开了多个页面是关不干净的，它只关闭当前的一个页面�?第二个quit�?
	    * 是�?出了�?��Webdriver�?��的窗口，�?��非常干净，所以推荐使用quit�?���?��case�?��的方法�?
	    */
	return entitys;
	}
	
	public static List<ExtEntity> getSougouweixinForOrgid(String key,Long orgid){
		List<ExtEntity> entitys=new ArrayList<ExtEntity>();
   try {
	   driver.get("https://weixin.sogou.com/");
	} catch (Exception e1) {
		  driver =new ChromeDriver(options1);
		  driver.get("https://weixin.sogou.com/");
	}
   try {
   	WebElement searchBox2 = driver.findElement(By.id("query"));
   	searchBox2.sendKeys(key);
   	Thread.sleep(2000);
   	WebElement searchBox3 = driver.findElement(By.className("swz"));
   	searchBox3.click();
   	Thread.sleep(2000);
			/*
			 * WebElement searchBox4 = driver.findElement(By.id("tool_show"));
			 * searchBox4.click(); Thread.sleep(2000); WebElement searchBox6 =
			 * driver.findElement(By.className("btn-time")); searchBox6.click();
			 * Thread.sleep(2000); WebElement searchBox5 =
			 * driver.findElement(By.linkText("一天内")); searchBox5.click();
			 * Thread.sleep(2000);
			 */
   	//信息列表
   	List<WebElement> searchBoxs=	driver.findElement(By.className("news-list")).findElements(By.tagName("h3"));
   	if(searchBoxs.size()>0){
   		System.out.println(searchBoxs.size());
   		System.out.println(driver.getCurrentUrl());
   		Set<String> handles=new HashSet<String>();//谷歌打开页面句柄存储
   		String curenthandle=driver.getWindowHandle();//首页句柄
   		handles.add(curenthandle);
   		for (int i = 0; i < searchBoxs.size(); i++) {
   			driver.switchTo().window(curenthandle);//转到主页，否则在副页面执行点击会报错
   			String titlelike=searchBoxs.get(i).getText();
   			String titlelikekey=titlelike+orgid;
   			System.out.println(titlelikekey);
//   			String keyvalue=MemcachedFactory.getKeyStr(titlelikekey);//缓存标题，如果点击过就不再点击
//   			if(StringUtils.isEmpty(keyvalue)){
//
//			}else{
//				continue;
//			}
   			searchBoxs.get(i).click();//点击标题，打开微信信息链接
   			Thread.sleep(5000);
   			Set<String> ss=driver.getWindowHandles();//获取六浏览器句柄的记录
   			System.out.println(ss.toString());
   			
   			for (String str : ss) {//如果副页面句柄已经爬取过就不再爬取
   				if(handles.contains(str)){
   					System.out.println(str);
   					continue;
   					
   				}
   			WebDriver dirverother=	driver.switchTo().window(str);//转到新打开的副页面。爬取信息
   			String otherhandle=dirverother.getWindowHandle();
   			handles.add(otherhandle);
   			System.out.println(dirverother.getCurrentUrl());
   			String result=dirverother.getPageSource();
   			List processors = ProcessorReader.readWeChatProcessors();
   			WebExtractorImplforweixin  extractor = new WebExtractorImplforweixin(processors, result);
   			ExtEntity entity = new ExtEntity();
   			extractor.process(entity);
   			System.out.println(entity.getTitle());
   			entity.setUri(dirverother.getCurrentUrl());
   			entity.setTilteLike(titlelike);
   			entitys.add(entity);
   			dirverother.close();
   			}
   	   	
   			Thread.sleep(2000);
		}
   		
   		
   		
   		
   	}
   	
   	driver.quit();
		return entitys;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	   
	   /**
	    * dr.quit()和dr.close()都可以�?出浏览器,�?��的说�?��两�?的区别：第一个close�?
	    * 如果打开了多个页面是关不干净的，它只关闭当前的一个页面�?第二个quit�?
	    * 是�?出了�?��Webdriver�?��的窗口，�?��非常干净，所以推荐使用quit�?���?��case�?��的方法�?
	    */
	return entitys;
	}
	public static List<ExtEntity> getSougouweixinForOrgid1(String key,Long orgid,Long tid){
		List<ExtEntity> entitys=new ArrayList<ExtEntity>();
   try {
	   driver.get("http://www.baidu.com/");
	} catch (Exception e1) {
		  driver =new ChromeDriver(options1);
		  driver.get("http://www.baidu.com/");
	}
   try {
	driver.manage().window().maximize(); 
	Thread.sleep(2000L);
	File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
	BufferedImage  fullImg = ImageIO.read(scrFile);
	driver.manage().timeouts().implicitlyWait( 10 , TimeUnit.SECONDS); 
	System.out.println(1243);
	ImageIO.write(fullImg, "png", new File("D:/1111.png"));
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
   
//   try { 
//	   Screenshot screenshot= new AShot().shootingStrategy(ShootingStrategies.viewportPasting( 1000 )).takeScreenshot(driver); 
//	   ImageIO.write(screenshot.getImage(), "png", new File("D:/1111.png"));
//   } catch (IOException e) { 
//       // TODO Auto-generated catch block 
//       e.printStackTrace(); 
//   }    
   
   
	   /**
	    * dr.quit()和dr.close()都可以�?出浏览器,�?��的说�?��两�?的区别：第一个close�?
	    * 如果打开了多个页面是关不干净的，它只关闭当前的一个页面�?第二个quit�?
	    * 是�?出了�?��Webdriver�?��的窗口，�?��非常干净，所以推荐使用quit�?���?��case�?��的方法�?
	    */
	return entitys;
	}
	//初始化浏览器
	public static void driverint(WebDriver driver,String curenthandle) {
		
		Set<String> ss=driver.getWindowHandles();//获取六浏览器句柄的记录
		System.out.println(ss.toString());
		
		for (String str : ss) {//如果副页面句柄已经爬取过就不再爬取
			if(curenthandle.equals(str)){
				continue;
				
			}else {
				driver.close();
			}
		}
		
	}
	
	
	
	public static void crawler (String keyword,String orgid,List<ExtEntity> entitys) {
		
		
		try {
			List<WebElement> searchBoxs=	driver.findElement(By.className("news-list")).findElements(By.tagName("h3"));
			if(searchBoxs.size()>0){
				System.out.println(searchBoxs.size());
				System.out.println(driver.getCurrentUrl());
				Set<String> handles=new HashSet<String>();//谷歌打开页面句柄存储
				String curenthandle=driver.getWindowHandle();//首页句柄
				handles.add(curenthandle);
				for (int i = 0; i < searchBoxs.size(); i++) {
					driver.switchTo().window(curenthandle);//转到主页，否则在副页面执行点击会报错
					String titlelike=searchBoxs.get(i).getText();
					String titlelikekey=titlelike+orgid;
					System.out.println(titlelikekey);
					String rediskey=redis_name+orgid+keyword;
//					boolean hasput=MemcachedFactory.hasPut(rediskey, titlelikekey);//缓存标题，如果点击过就不再点击
//
//					if(hasput){
//						System.out.println("paguole");
//						continue;
//					}
					
					searchBoxs.get(i).findElement(By.tagName("a")).click();//点击标题，打开微信信息链接
					Thread.sleep(8000);
					
					Set<String> ss=driver.getWindowHandles();//获取六浏览器句柄的记录
					System.out.println(ss.toString());
					
					for (String str : ss) {//如果副页面句柄已经爬取过就不再爬取
						if(handles.contains(str)){
							System.out.println(str);
							continue;
							
						}
					WebDriver dirverother=	driver.switchTo().window(str);//转到新打开的副页面。爬取信息
					String otherhandle=dirverother.getWindowHandle();
					handles.add(otherhandle);
					System.out.println(dirverother.getCurrentUrl());
					String result=dirverother.getPageSource();
					List processors = ProcessorReader.readWeChatProcessors();
					WebExtractorImplforweixin  extractor = new WebExtractorImplforweixin(processors, result);
					ExtEntity entity = new ExtEntity();
					extractor.process(entity);
					System.out.println(entity.getTitle());
					entity.setUri(dirverother.getCurrentUrl());
					entity.setTilteLike(titlelike);
					
					Map<String, FileTag> imgDataMap= ContentFileFinder.getContentFileTag(entity.getContentWithTag(),"https://mp.weixin.qq.com/s/DePy9GFzh1tL844ik9YuWw");
					//System.out.println(entity.getContentWithTag());
					String formatImgContent=entity.getContentWithTag();
					 for (String key : imgDataMap.keySet()) {
						 FileTag fileTag = imgDataMap.get(key);
						 while (formatImgContent.contains(key)) {
				                //转换为绝对路径
				                formatImgContent = formatImgContent.replace(key, "");
				            }
					 }
					 entity.setContentWithTag(formatImgContent);
					entitys.add(entity);
					dirverother.close();
					}
			   	
					Thread.sleep(1000);
				}
				
				
				
				
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
	
	/**
	 * 验证码判断，并输入验证码
	 */
	
	public static  void sendYzm() {
		try {
			String script="document.getElementsByTagName('body')[0].style.zoom=1.5;";
			 Thread.sleep(1000L);
			 WebElement ele1  = driver.findElement(By.id("seccodeForm")).findElement(By.className("s1"));
			 Point point1 = ele1.getLocation();
			 ((JavascriptExecutor) driver).executeScript(script);
			 Thread.sleep(5000L);
			 File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
			 WebElement ele  = driver.findElement(By.id("seccodeImage"));
			String imgurl= ele.getAttribute("src");
			System.out.println(imgurl);
			//File file= ele .getScreenshotAs(OutputType.FILE);
			BufferedImage  fullImg = ImageIO.read(screenshot);
			Point point = ele.getLocation();
			int eleWidth = ele.getSize().getWidth();
			int eleHeight = ele.getSize().getHeight();
			int xlocation=(int) (point.x*1.5);
			int ylocation=(int) (point.y*1.5);
			System.out.println("x--------"+xlocation);
			System.out.println("y--------"+ylocation);
			BufferedImage eleScreenshot= fullImg.getSubimage(xlocation, ylocation+2,
				    150, 50);
			String savePath = "D:/1111.png";
			String savePath1 = "D:/1111222.png";
			
			 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
				ImageIO.write(eleScreenshot, "png", new File(savePath));
				ImageIO.write(eleScreenshot, "png", outputStream);
			   String base64Img = Base64.encode(outputStream.toByteArray());
			   
			   JSONObject json =new JSONObject();
			   json.put("image", base64Img);
//			   String sendurl=Constants.yzmApi;
//			   String re=post(json, sendurl);
//			   System.out.println(re);
//			   JSONObject jsStr = JSONObject.parseObject(re);
//			   String code=jsStr.getString("message");
			  //识别验证码
			   String ocrBady = BaiduOCR.imageBasicGeneralOcr(outputStream.toByteArray());
			   List<String> strings = BaiduOCR.pasermsgJson(ocrBady);
               String code=strings.get(0);
			   String script1="document.getElementsByTagName('body')[0].style.zoom=1;";
			   ((JavascriptExecutor) driver).executeScript(script1);
			   Thread.sleep(4000);
			   WebElement sendyzm  = driver.findElement(By.id("seccodeInput"));
			   WebElement submit  = driver.findElement(By.id("submit"));
			   sendyzm.sendKeys(code);
			   Thread.sleep(4000);
			   submit.click();
			   Thread.sleep(4000);
			   
			   
			 
			   FileUtils.copyFile(screenshot, new File(savePath1));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			System.out.println("buyongyanzm");
		} 
	       
		 
	}

	public static String post(JSONObject json, String url){
		String result = "";
		HttpPost post = new HttpPost(url);
		try{
			CloseableHttpClient httpClient = HttpClients.createDefault();
        
			post.setHeader("Content-Type","application/json;charset=utf-8");
			//post.addHeader("Authorization", "Basic YWRtaW46");
			StringEntity postingString = new StringEntity(json.toString(),"utf-8");
			post.setEntity(postingString);
			HttpResponse response = httpClient.execute(post);
			
			InputStream in = response.getEntity().getContent();
			BufferedReader br = new BufferedReader(new InputStreamReader(in, "utf-8"));
			StringBuilder strber= new StringBuilder();
			String line = null;
			while((line = br.readLine())!=null){
				strber.append(line+'\n');
			}
			br.close();
			in.close();
			result = strber.toString();
			if(response.getStatusLine().getStatusCode()!=HttpStatus.SC_OK){
				result = "服务器异常";
			}
		} catch (Exception e){
			System.out.println("请求异常");
			throw new RuntimeException(e);
		} finally{
			post.abort();
		}
		return result;
	}
	public static   void sendExtentity(ExtEntity  extEntity,String key,Long orgid,Long tid){
		try {
			String rediskey=redis_name+orgid+key;
			String weixinurl=extEntity.getUri();
			String titlekey=extEntity.getTilteLike()+orgid;
		
			String contentNoTag = null;
			String contentWithTag = extEntity.getContentWithTag();
			contentNoTag = ContentUtility.TransferHTML2Text(contentWithTag);
			DocInfo docInfo=new DocInfo();
 
			docInfo.setSourceType("WeChat");
			// docInfo.setLastModified(lastModified);
			docInfo.setSourceaddress(weixinurl);
			docInfo.setLang("zh_CN");
			docInfo.setContentType("HTML");
			docInfo.setSourceType("WeChat");
			docInfo.setCharset("utf-8");
			docInfo.setTitle(extEntity.getTitle());
			docInfo.setAuthor(extEntity.getAuthor());
			docInfo.setPublishDate(extEntity.getPublishDate());
			docInfo.setOrigin("微信公众号-"+extEntity.getAuthor());
      // docInfo.setKeywords(extEntity.getKeywords());
			//docInfo.setSummary(extEntity.getSummary());
			StringBuffer sb = new StringBuffer();
			sb.append("<html><head>");
			sb.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
			sb.append("<title></title></head><body>");
			sb.append(extEntity.getContentWithTag());
			sb.append("</body></html>");
			
			docInfo.setContentWithTag(sb.toString());
			docInfo.setContentNoTag(contentNoTag);
			docInfo.setContentImgCvtTag(sb.toString());
     
			  docInfo.setOrgId(orgid);
			  Map<String, String> params = new HashMap<String, String>();
				params.put("fromWhere", "weixinsearch");
				if (null!=tid) {
					params.put("tid", tid+"");
				}
				docInfo.setOtherParams(params);
				if(docInfo.getTitle()==null){
						//空了继续爬 不空爬不下来记录了
				}else{
					
				//mqSender.sendMessage(docInfo);
//				MemcachedFactory.putNewURI(rediskey, titlekey);
				
				}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
	}
	public static void main(String[] args) {
//		MemcachedFactory.init();
		//ChromeUtil.getSougouweixin("外交部");
		ChromeJietu.getSougouweixinForOrgid1("伊朗",1L,1L);
	}
}
