package com.zzsn.localsearch;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zzsn.cache.JedisUtil;
import com.zzsn.crawler.db.DBManager;
import com.zzsn.crawler.db.SnowIdUtils;
import com.zzsn.tmpurl.oracledb.OracleDBManager;
import com.zzsn.tmpurl.oracledb.OracleDataTable;
import com.zzsn.entity.ClbAnsProcessitem;
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 lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
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.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.springframework.kafka.core.KafkaTemplate;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.sql.SQLException;
import java.sql.Types;
import java.util.*;


@Slf4j
public class ChromeUtil {
	public static String redis_name="sgwx";
	static ChromeOptions    options1=new ChromeOptions() ;
	static WebDriver driver;
//	public static KafkaTemplate kafkaTemplate= SpringContextUtil.getBean(KafkaTemplate.class);
	public static KafkaTemplate kafkaTemplate= null;
	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);*/
//			options1.addArguments("disable-dev-shm-usage");
//			options1.addArguments("no-sandbox");
			if(Constants.PROXY.equals("1")) {
			String s=getProxyIP();
			System.out.println(s);
			String[] proxys = s.split("-");
			String proxyHost = proxys[0];
			int proxyPort = Integer.parseInt(proxys[1]);
				options1.addArguments("--proxy-server=" + proxyHost + ":" + proxyPort);
		}
		options1.addArguments("--incognito"); // 打开隐私模式
		options1.addArguments("-test-type", "--ignore-certificate-errors"); // 忽略证书错误
		driver =new ChromeDriver(options1);
	}
	public static String getProxyIP(){
		String searchSql = "select proxy from CIS_sys_Proxy where ID = 1";
		String proxy="";
		OracleDBManager dm = new OracleDBManager();
		String[] coulmn = null;
		int[] type = null;
		try {
			OracleDataTable dt = dm.getResultData(coulmn, type, searchSql);
			if(dt != null && dt.getRowCount()> 0){
				for(int i = 0; i<dt.getRowCount(); i++){
					for(int j = 0; j<dt.getColCoun(); j++)
						if(dt.getRow()[i][j].length()>5){
							proxy=dt.getRow()[i][j];
						}
				}
			}else
				System.out.println("查询失败");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return proxy;
	}

	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("https://weixin.sogou.com/");
		} catch (Exception e1) {
		    driver =new ChromeDriver(options1);
		    driver.get("https://weixin.sogou.com/");
		}
//   		String curenthandle=driver.getWindowHandle();//首页句柄
   		String curenthandle="";//首页句柄
   		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);
			curenthandle=driver.getWindowHandle();//首页句柄
			/*
			 * 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);
			 */
   	//信息列表

   	String idname="sogou_page_";//sogou_page_3
   	for (int i = 1; i <=3; i++) {
//   	for (int i = 1; i <=10; i++) {

   		//爬完一页就去发送给消息队列
   		entitys=new ArrayList<ExtEntity>();
		try {
			driver.switchTo().window(curenthandle);//转到主页，否则在副页面执行点击会报错
		}catch (Exception e){
			break;
		}
   		Thread.sleep(3000);
   		if(i==1) {
			WebElement pageClick = driver.findElement(By.id(idname + 2));
			pageClick.click();
			Thread.sleep(5000);
			pageClick = driver.findElement(By.id(idname + 1));
			pageClick.click();
			Thread.sleep(5000);
   			crawler(key,orgid+"",entitys);
//			continue;
   		}else {
				WebElement pageClick = driver.findElement(By.id(idname + i));
				pageClick.click();
				Thread.sleep(5000);
   		}
//		WebElement pageClick = driver.findElement(By.id(idname + i));
//		pageClick.click();
//		Thread.sleep(5000);
   		//判断是否需要输入验证码//最多3次,不用输入验证码时执行会报异常，不影响爬取
   		for (int j = 0; j < 3; j++) {
   			sendYzm();
		}
		if(i>1) {
			//页面恢复伸缩
			String script1 = "document.getElementsByTagName('body')[0].style.zoom=1;";
			((JavascriptExecutor) driver).executeScript(script1);
			//爬取内容
			crawler(key, orgid + "", entitys);
		}
   		//发送消息队列
   		for (int k = 0; k < entitys.size(); k++) {
//			ChromeUtil chromeUtil=new ChromeUtil();
			sendExtentity(entitys.get(k),key,orgid,tid);

		}
	}


   		driver.quit();
//   		driverint(driver,curenthandle);
		return entitys;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			driver.quit();
//			 driverint(driver,curenthandle);
		}
   driver.quit();
//   driverint(driver,curenthandle);
	   /**
	    * 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;
					try {
						boolean sismember = JedisUtil.sismember(rediskey, titlelikekey);
						if (sismember) {
							continue;
						}
						JedisUtil.sadd(rediskey, titlelikekey);
					}catch (Exception e){
						log.info("缓存出问题");
					}

					searchBoxs.get(i).findElement(By.tagName("a")).click();//点击标题，打开微信信息链接
					Thread.sleep(5000);
					Set<String> ss=driver.getWindowHandles();//获取浏览器句柄的记录
//					System.out.println(ss.toString());
					for (String str : ss) {//如果副页面句柄已经爬取过就不再爬取
						if(str.contains(curenthandle)){
							System.out.println(curenthandle);
							continue;
						}
					    WebDriver dirverother=	driver.switchTo().window(str);//转到新打开的副页面。爬取信息
//						String currentUrl = dirverother.getCurrentUrl();
//						WeixinUtil.sendWxMessage("LiuWeiGang",
//								"点击链接: "+currentUrl,
//								1000002, "ww6bef1e81aacbf27a",
//								"ttZJ_KbO3QABs5Z7IDHNa_X4CZizaojherzwzfQ7wl0");
//						String otherhandle = dirverother.getWindowHandle();//出现dirverother已关闭的问题
//						handles.add(otherhandle);
						if(hasClose(dirverother)){
							String title=driver.getTitle();
							if(title.contains("搜狗搜索")){
								dirverother.close();
								break;
							}
							continue;
						}
						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());
						if(StringUtils.isEmpty(entity.getTitle())){
							break;
						}
						entity.setUri(dirverother.getCurrentUrl());
						entity.setTilteLike(titlelike);
//						String formatImgContent=entity.getContentWithTag();
						//存储图片
//						String formatImgContent="";
//						 formatImgContent= ContentFileFinder.getContentImgTag(entity.getContentWithTag(),"https://mp.weixin.qq.com/s/DePy9GFzh1tL844ik9YuWw");
					//去掉图片
					Map<String, FileTag> imgDataMap= ContentFileFinder.getContentFileTag(entity.getContentWithTag(),"https://mp.weixin.qq.com/s/DePy9GFzh1tL844ik9YuWw");
					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();
//					dirverother.switchTo().window(curenthandle);
					closeWindow( curenthandle);

					}
					Thread.sleep(5000);
				}

			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static boolean hasClose(WebDriver driver) {
		try {
			driver.getTitle();
			return false;
		} catch (Exception e) {
			return true;
		}
	}
	public static void crawlerbak (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;
					try {
						boolean sismember = JedisUtil.sismember(rediskey, titlelikekey);
						if (sismember) {
							continue;
						}
						JedisUtil.sadd(rediskey, titlelikekey);
					}catch (Exception e){
						log.info("缓存出问题");
					}

					searchBoxs.get(i).findElement(By.tagName("a")).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 currentUrl = dirverother.getCurrentUrl();
//						WeixinUtil.sendWxMessage("LiuWeiGang",
//								"点击链接: "+currentUrl,
//								1000002, "ww6bef1e81aacbf27a",
//								"ttZJ_KbO3QABs5Z7IDHNa_X4CZizaojherzwzfQ7wl0");
						try {
							String otherhandle = dirverother.getWindowHandle();//出现dirverother已关闭的问题
							handles.add(otherhandle);
						}catch (Exception e){
							continue;
						}
						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);
//						String formatImgContent=entity.getContentWithTag();
						//存储图片
//						String formatImgContent="";
//						 formatImgContent= ContentFileFinder.getContentImgTag(entity.getContentWithTag(),"https://mp.weixin.qq.com/s/DePy9GFzh1tL844ik9YuWw");
					//去掉图片
					Map<String, FileTag> imgDataMap= ContentFileFinder.getContentFileTag(entity.getContentWithTag(),"https://mp.weixin.qq.com/s/DePy9GFzh1tL844ik9YuWw");
					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();
//					dirverother.switchTo().window(curenthandle);
//					closeWindow(dirverother);
					}
					Thread.sleep(1000);
				}

			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void closeWindow(String curenthandle) {

		try {
			String winHandleBefore = driver.getWindowHandle();//关闭当前窗口前，获取当前窗口句柄
			Set<String> winHandles = driver.getWindowHandles();//使用set集合获取所有窗口句柄
			if(winHandles.size()<2){
				return;
			}
			driver.close();//关闭窗口

			Iterator<String> it = winHandles.iterator();//创建迭代器，迭代winHandles里的句柄
			while (it.hasNext()) {//用it.hasNext()判断时候有下一个窗口,如果有就切换到下一个窗口
				String win = it.next();//获取集合中的元素
				if(win.equals(curenthandle)){
					continue;
				}else if (!win.equals(winHandleBefore)) { //如果此窗口不是关闭前的窗口
					WebDriver webDriver = driver.switchTo().window(win);//切换到新窗口
					webDriver.close();
					break;
				}
			}

		} catch (Exception e) {
			e.printStackTrace();

		}

	}


	/**
	 * 验证码判断，并输入验证码
	 */
	
	public static  void sendYzm() {
		try {
//			String script="document.getElementsByTagName('body')[0].style.zoom=1.5;";
			String script="document.getElementsByTagName('body')[0].style.zoom=1;";
			 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);
			BufferedImage eleScreenshot= fullImg.getSubimage(430, 310,
					120, 50);
			String savePath = "D:/1111.png";
			String savePath1 = "D:/1111222.png";
			String code="";
			String ocrBady = BaiduOCR.imageBasicGeneralOcr(imageToBytes(eleScreenshot));
			List<String> msgList = BaiduOCR.pasermsgJson(ocrBady);

			for (String msg:
					msgList) {
				if(msg.length()>2){
					code=msg;
				}
				System.out.println(msg);
			}

//			 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 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(10000);
			   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");
		} 
	       
		 
	}

	/**
	 * BufferedImage转byte[]
	 *
	 * @param bImage BufferedImage对象
	 * @return byte[]
	 * @auth zhy
	 */
	private static byte[] imageToBytes(BufferedImage bImage) {
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		try {
			ImageIO.write(bImage, "png", out);
		} catch (IOException e) {
			//log.error(e.getMessage());
		}
		return out.toByteArray();
	}

	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.setSid(tid);
			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.getOrigin()==null ? extEntity.getAuthor():extEntity.getOrigin());
			 docInfo.setKeywords(key);
			//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 {
				ObjectMapper mapper = new ObjectMapper();
				try {
//					ClbAnsProcessitem processitem = docInfoTrans2Processitem(docInfo);
//					processitem.setSource("搜狗微信爬虫");
//					if (StringUtils.isEmpty(processitem.getTitle()) || StringUtils.isEmpty(processitem.getContent())) {
//						System.out.println("资讯的信息不全没有发送");
//					}
//					String docjson = mapper.writeValueAsString(processitem);
//					KafkaTemplate kafkaTemplate= SpringContextUtil.getBean(KafkaTemplate.class);
//					kafkaTemplate.send(Constants.KAFKA_PRODUCT_TOPIC, "key", docjson);
//					log.info("发送到kafka成功。");

					intsertData(docInfo);
					JedisUtil.setString(weixinurl + "_" + docInfo.getSid(), "1", -1);
//				mqSender.sendMessage(docInfo);
//				MemcachedFactory.putNewURI(rediskey, titlekey);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}catch (Exception e){

		}
	}

	static String insertSql = "insert into cis_ans_processitem " +
			" (id,sid, title,summary,publish_date,origin,author, content,words,keywords,sourceaddress) " +
			" values(?,?,?,?,?,?,?,?,?,?,?)";
	private static void intsertData(DocInfo docInfo)
	{
		long snowID = SnowIdUtils.uniqueLong();
		String id=snowID+"";
		String sid=docInfo.getSid()+"";
		String title=docInfo.getTitle();
		String summ = docInfo.getSummary();
		if(summ!=null && summ.length()>5000){
			summ=summ.substring(0,4900);
		}
		String summary=summ;
		String publish_date=docInfo.getPublishDate();
		String origin=docInfo.getOrigin();
		String author=docInfo.getAuthor();
		String content=docInfo.getContentNoTag();
		String words=docInfo.getContentWithTag();
		String keywords=docInfo.getKeywords();
		String sourceaddress=docInfo.getSourceaddress();

		DBManager dm = new DBManager();
		String[] coulmn = new String[]{id,  sid, title,summary,publish_date,origin,author,content,words,keywords,sourceaddress};
		int[] type = new int[]{Types.CHAR, Types.CHAR, Types.CHAR, Types.CHAR, Types.CHAR, Types.CHAR, Types.CHAR, Types.CHAR, Types.CHAR, Types.CHAR, Types.CHAR};

		try {
			boolean flag = dm.updateOrAdd(coulmn, type, insertSql);
			if(flag)
				System.out.println("插入成功");
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static ClbAnsProcessitem docInfoTrans2Processitem(DocInfo docInfo){
		ClbAnsProcessitem clbAnsProcessitem=new ClbAnsProcessitem();
		clbAnsProcessitem.setSid(docInfo.getSid()+"");
		clbAnsProcessitem.setTitle(docInfo.getTitle());
		clbAnsProcessitem.setContent(docInfo.getContentNoTag());
		clbAnsProcessitem.setContentWithtag(docInfo.getContentWithTag());
		clbAnsProcessitem.setSummary(docInfo.getSummary());
		clbAnsProcessitem.setAuthor(docInfo.getAuthor());
		clbAnsProcessitem.setOrigin(docInfo.getOrigin());
		clbAnsProcessitem.setPublishDate(docInfo.getPublishDate());
		try {
			String newURL="";
//			for (int i = 0; i <10 ; i++) {
//				Thread.sleep(1000);
//				newURL = JedisUtil.getString(docInfo.getSourceaddress());
//				if(StringUtils.isNotEmpty(newURL)){
//					clbAnsProcessitem.setSourceAddress(newURL);
//					break;
//				}
//			}
			if(StringUtils.isNotEmpty(newURL)){
				clbAnsProcessitem.setSourceAddress(newURL);
			}else {
				clbAnsProcessitem.setSourceAddress(docInfo.getSourceaddress());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return clbAnsProcessitem;
	}
	public static void main(String[] args) {
//		MemcachedFactory.init();
		//ChromeUtil.getSougouweixin("外交部");
//		ChromeUtil.getSougouweixinForOrgid1("伊朗",1L,1L);
	}
}
