package com.zhiwei.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Proxy.Type;
import java.net.SocketAddress;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import com.jayway.jsonpath.JsonPath;
import com.zhiwei.entity.Zhihu;
import com.zhiwei.servlet.MainController;

public class Craw {

	static List<String> ips = new ArrayList<>();
	static int nums = 0;
	static {
		ips.add("80.211.190.76:3128");
		ips.add("187.32.172.65:3128");
		ips.add("80.211.162.17:8888");
		ips.add("80.211.11.229:80");
	}

	public static void craw() {

	}

	public synchronized List<Zhihu> getQuestionAndAnswerByUrl(List<String> list, Proxy proxy, int num, Date start,
			Date end, String cookie, String mail) {
		List<Zhihu> zhList = new ArrayList<Zhihu>();
		int nowRun = 1;
		for (String ul : list) {
			MainController.map.put(mail, nowRun++);
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
			try {
				int ns = 0;
				String title = "";
				try {
					BufferedReader in_0 = null;
					URL url_0;
					String result_0 = "";
					url_0 = new URL(ul);
					URLConnection con = null;
					// if (proxy == null) {
					 con = url_0.openConnection();
					// } else {
					Proxy p = new Proxy(Type.HTTP, new InetSocketAddress(ips.get(nums % ips.size()).split(":")[0],
							Integer.valueOf(ips.get(nums % ips.size()).split(":")[1])));
					nums++;
//					con = url_0.openConnection(p);
					// }
					con.setConnectTimeout(6000);
					con.setRequestProperty("User-Agent",
							"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
					con.setRequestProperty("Cookie", cookie);
					con.setRequestProperty("authorization", "oauth c3cef7c66a1843f8b3a9e6a1e3160e20");
					in_0 = new BufferedReader(new InputStreamReader(con.getInputStream()));
					String line;
					while ((line = in_0.readLine()) != null) {
						result_0 += line + "\n";
					}
					Document doc = Jsoup.parse(result_0);
					Element e = doc.select("title").first();
					title = e.ownText();
				} catch (Exception eee) {
					System.out.println(eee.getMessage());
				}
				ul = ul.replace("question", "questions");
				ul = ul.replace("http://www.zhihu.com", "").replace("https://www.zhihu.com", "");
				int tip = 0;
				while (true) {
					int bean = zhList.size();
					BufferedReader in = null;
					URL url;
					String result = "";
					System.out.println(ul);
					try {
						url = new URL("https://www.zhihu.com/api/v4" + ul
								+ "/answers?include=data%5B*%5D.is_normal%2Cadmin_closed_comment%2Creward_info%2Cis_collapsed%2Cannotation_action%2Cannotation_detail%2Ccollapse_reason%2Cis_sticky%2Ccollapsed_by%2Csuggest_edit%2Ccomment_count%2Ccan_comment%2Ccontent%2Ceditable_content%2Cvoteup_count%2Creshipment_settings%2Ccomment_permission%2Ccreated_time%2Cupdated_time%2Creview_info%2Cquestion%2Cexcerpt%2Crelationship.is_authorized%2Cis_author%2Cvoting%2Cis_thanked%2Cis_nothelp%2Cupvoted_followees%3Bdata%5B*%5D.mark_infos%5B*%5D.url%3Bdata%5B*%5D.author.follower_count%2Cbadge%5B%3F(type%3Dbest_answerer)%5D.topics&offset="
								+ ns + "&limit=20&sort_by=created");
						URLConnection conn = null;
//						 if (proxy == null) {
						 conn = url.openConnection();
						// } else {
						Proxy p = new Proxy(Type.HTTP, new InetSocketAddress(ips.get(nums % ips.size()).split(":")[0],
								Integer.valueOf(ips.get(nums % ips.size()).split(":")[1])));
						nums++;
//						conn = url.openConnection(p);
						// }
						conn.setConnectTimeout(6000);
						conn.setRequestProperty("User-Agent",
								"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
						conn.setRequestProperty("Cookie", cookie);
						conn.setRequestProperty("X-Za-Experiment",
								"ge3:ge3_10,ge2:ge2_1,search_section_style:loosen,android_pass_through_push:all,new_more:new,nwebQAGrowth:experiment,nweb_feed_ad:experiment,qaweb_related_readings_content_control:open,market_tab_banner:market_tab_banner_show,live_store:ls_a2_b1_c1_f1,qaweb_thumbnail_abtest:new,nweb_search:nweb_search_heifetz,recommend_ebook_ac:ebook_old,enable_vote_down_reason_menu:enable,show_video_upload_attention:true,is_office:false,enable_tts_play:post,new_question_diversion:true,recom_answer_rec:recom_answer_ratio,wechat_share_modal:wechat_share_modal_show,new_live_feed_mediacard:old,nweb_feed_ui:expand,search_hybrid_tabs:without-tabs,hybrid_zhmore_video:no,recommend_live_guess_like:live_guess_es,nweb_growth_people:default,qrcode_login:qrcode,is_show_unicom_free_entry:unicom_free_entry_off,new_mobile_column_appheader:new_header,android_db_comment_with_repin_record:open,feed_hybrid_topic_recom_button_icon:yes,android_db_recommend_action:open,zcm-lighting:zcm,android_db_feed_hash_tag_style:button,app_store_rate_dialog:close,recommend_question:rec_question_old,top_weight_search:new_top_search,default:None,recommendation_abtest:new,is_new_noti_panel:no,android_db_repin_selection:open,nweb_related_advert:default,android_profile_panel:panel_b,nweb_write_answer:experiment,recommend_live_detail:live_detail_no_rerank_v2");
						conn.setRequestProperty("X-Req-SSL", "proto=TLSv1.2,sni=,cipher=ECDHE-RSA-AES256-GCM-SHA384");
						conn.setRequestProperty("authorization", "oauth c3cef7c66a1843f8b3a9e6a1e3160e20");
						in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
						String line;
						while ((line = in.readLine()) != null) {
							result += line + "\n";
						}
						List<Object> json = JsonPath.read(result, "$.data.*");
						Calendar cal = Calendar.getInstance();
						for (Object obj : json) {
							Matcher m = Pattern.compile(", content=(.+),").matcher(obj.toString());
							String content = "";
							if (m.find()) {
								content = m.group(1);
								content = content.split(",")[0];
							}
							Matcher m2 = Pattern.compile(", id=(.+), voteup").matcher(obj.toString());
							String id = "";
							if (m2.find()) {
								id = m2.group(1);
							}
							String name = "";
							String userUrl = "";
							Matcher m4 = Pattern.compile(", name=(.+), is_").matcher(obj.toString());
							if (m4.find()) {
								name = m4.group(1);
								name = name.split(",")[0];
								if (name.equals("匿名用户")) {
								} else {
									Matcher m5 = Pattern.compile("url_token=(.+),").matcher(obj.toString());
									if (m5.find()) {
										userUrl = m5.group(1);
										userUrl = userUrl.split(",")[0];
									}
								}
							}
							Matcher m5 = Pattern.compile("url_token=(.+), id").matcher(obj.toString());
							if (m5.find()) {
								userUrl = m5.group(1);
								userUrl = userUrl.split(",")[0];
							}
							String questionId = "";
							Matcher m6 = Pattern.compile("tion, id=(.+), upda").matcher(obj.toString());
							if (m6.find()) {
								questionId = m6.group(1);
								questionId = questionId.split(",")[0];
							}
							String time = "";
							Matcher m7 = Pattern.compile("created_time=(.+), id").matcher(obj.toString());
							if (m7.find()) {
								time = m7.group(1);
								time = time.split(",")[0];
							}

							String pointLikeNumber = "0";
							Matcher mn = Pattern.compile("voteup_count=(.+), col").matcher(obj.toString());
							if (mn.find()) {
								pointLikeNumber = mn.group(1);
								pointLikeNumber = pointLikeNumber.split(",")[0];
							}

							String idUrl = "https://www.zhihu.com/question/" + questionId + "/answer/" + id;
							Zhihu zhihu = new Zhihu();
							zhihu.setContent(content.replaceAll("<[\\S\\s]+?>", ""));
							zhihu.setTitle(title);
							zhihu.setId(idUrl);
							zhihu.setAuthor(name);
							zhihu.setAuthorUrl("https://www.zhihu.com/people/" + userUrl);
							zhihu.setPointLikeNumber(pointLikeNumber);
							Long timestamp = Long.valueOf(time) * 1000l;
							String date = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA)
									.format(new Date(timestamp));
							zhihu.setTime(date);
							try {
								SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
								if (sdf.parse(date).before(start) || end.before(sdf.parse(date))) {
									cal.setTime(sdf.parse(date));
									continue;
								}
							} catch (Exception e) {
								e.printStackTrace();
							}
							zhList.add(zhihu);
						}
//						if (end.before(cal.getTime())) {
//
//						} else {
							if (zhList.size() == bean) {
								break;
							}
//						}
						ns += 20;
						if (ns > num * 20) {
							break;
						}
					} catch (Exception e) {
						tip++;
						if (tip > 3) {
							break;
						}
						e.printStackTrace();
					}
//					Thread.sleep(500);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		MainController.map.remove(mail);
		return zhList;
	}
}
