Main.java 7.54 KB
Newer Older
ilal committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
package cn.timer.api.controller.kqgl.holiday;

import java.io.IOException;
import java.net.MalformedURLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomNodeList;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

import cn.timer.api.bean.kqmk.KqmkRedletterDay;
import cn.timer.api.dao.kqmk.KqmkRedletterDayMapper;

public class Main {
	private static String latestVocationName = "";

	public String getVocationName(DomNodeList<HtmlElement> htmlElements, String date) throws ParseException {
		String rst = "";
		boolean pastTimeFlag = false;
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
		Date paramDate = dateFormat.parse(date);
		if (new Date().getTime() >= paramDate.getTime()) {
			pastTimeFlag = true;
		}
		//first step //jugde if can get vocation name from html page
		for (int i = 0; i < htmlElements.size(); i++) {
			HtmlElement element = htmlElements.get(i);
			if (element.getAttribute("class").indexOf("vacation") != -1) {
				boolean hitFlag = false;
				String voationName = "";
				for (; i < htmlElements.size(); i++) {
					HtmlElement elementTmp = htmlElements.get(i);
					String liDate = elementTmp.getAttribute("date");
					List<HtmlElement> lunar = elementTmp.getElementsByAttribute("span", "class", "lunar");
					String lanarText = lunar.get(0).asText();
					if (lanarText.equals("元旦")) {
						voationName = "元旦";
					} else if (lanarText.equals("除夕") || lanarText.equals("春节")) {
						voationName = "春节";
					} else if (lanarText.equals("清明")) {
						voationName = "清明";
					} else if (lanarText.equals("国际劳动节")) {
						voationName = "国际劳动节";
					} else if (lanarText.equals("端午节")) {
						voationName = "端午节";
					} else if (lanarText.equals("中秋节")) {
						voationName = "中秋节";
					} else if (lanarText.equals("国庆节")) {
						voationName = "国庆节";
					}
					if (liDate.equals(date)) {
						hitFlag = true;
					}
					if (elementTmp.getAttribute("class").indexOf("vacation") == -1) {
						break;
					}
				}
				if (hitFlag == true && !voationName.equals("")) {
					rst = voationName;
					break;
				}
			} else {
				continue;
			}
		}
		//if first step fail(rarely), get from the latest Vocation name
		if (rst.equals("")) {
			System.out.println("warning: fail to get vocation name from html page.");
			//you can judge by some simple rule 
			//from the latest Vocation name
			rst = Main.latestVocationName;
		} else if (pastTimeFlag == true) {
			//更新《当前时间,且最近一次的可见的假期名
			Main.latestVocationName = rst;
		}
		return rst;
	}

	public List<ChinaDate> getCurrentDateInfo() {
		WebClient webClient = null;
		List<ChinaDate> dateList = null;
		try {
			DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
			dateList = new ArrayList<ChinaDate>();
			webClient = new WebClient();
			HtmlPage page = webClient.getPage("http://hao.360.cn/rili/");
			//最大等待60秒
			for (int k = 0; k < 60; k++) {
				if (!page.getElementById("M-dates").asText().equals(""))
					break;
				Thread.sleep(1000);
			}
			//睡了8秒,等待页面加载完成...,有时候,页面可能获取不到,不稳定()
			//Thread.sleep(8000);
			DomNodeList<HtmlElement> htmlElements = page.getElementById("M-dates").getElementsByTagName("li");
			//System.out.println(htmlElements.size());
			for (HtmlElement element : htmlElements) {
				ChinaDate chinaDate = new ChinaDate();
				List<HtmlElement> lunar = element.getElementsByAttribute("span", "class", "lunar");
				List<HtmlElement> solar = element.getElementsByAttribute("div", "class", "solar");
				chinaDate.setLunar(lunar.get(0).asText());
				chinaDate.setSolar(solar.get(0).asText());
				chinaDate.setSolarDate(dateFormat.parse(element.getAttribute("date")));
				if (element.getAttribute("class").indexOf("vacation") != -1) {
					chinaDate.setVacation(true);
					chinaDate.setVacationName(this.getVocationName(htmlElements, element.getAttribute("date")));
				}
				if (element.getAttribute("class").indexOf("weekend") != -1
						&& element.getAttribute("class").indexOf("last") == -1) {
					chinaDate.setSaturday(true);
				}
				if (element.getAttribute("class").indexOf("last weekend") != -1) {
					chinaDate.setSunday(true);
				}
				if (element.getAttribute("class").indexOf("work") != -1) {
					chinaDate.setWorkFlag(true);
				} else if (chinaDate.isSaturday() == false && chinaDate.isSunday() == false
						&& chinaDate.isVacation() == false) {
					chinaDate.setWorkFlag(true);
				} else {
					chinaDate.setWorkFlag(false);
				}
				dateList.add(chinaDate);
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("get date from http://hao.360.cn/rili/ error~");
		} finally {
			webClient.close();
		}
		return dateList;
	}

	public ChinaDate getTodayInfo() {
		List<ChinaDate> dateList = this.getCurrentDateInfo();
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
		for (ChinaDate date : dateList) {
			if (dateFormat.format(date.getSolarDate()).equals(dateFormat.format(new Date()))) {
				return date;
			}
		}
		return new ChinaDate();
	}
	
	public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException, InterruptedException {
		List<ChinaDate> dateList = new Main().getCurrentDateInfo();
		ChinaDate today = new Main().getTodayInfo();
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
		System.out.println("本月详情:");
		
		String yymm = new SimpleDateFormat("yyyy-MM").format(new Date()).toString();//
		
		for (ChinaDate date : dateList) {
//			System.out.println(dateFormat.format(date.getSolarDate()) + " " + date.getVacationName());
//			System.out.println(dateFormat.format(date.getSolarDate()).replaceAll("/", "-") + " " + date.getVacationName());
			String str = dateFormat.format(date.getSolarDate()).replaceAll("/", "-");
			
			//INSERT INTO kqmk_redletter_day VALUES ( value1, value2,...valueN );
			String jieri = date.getVacationName();
			boolean status = jieri.contains("节");
			int holidaystatus = 0;
			if(status) {
				holidaystatus = 1;
			}
			System.out.println("INSERT INTO kqmk_redletter_day(holiday,holiday_name,holiday_status,belong_yearmonth) VALUES(" + '"'+str +'"'+ "," +'"'+date.getVacationName()+'"'+ "," +holidaystatus+ "," + '"'+yymm+'"'+ ");");

			
		}
		
//		System.out.println("------------------------------------------------------------------------");
//		System.out.println("今日详情:");
//		System.out.println("日期:" + today.getSolarDate());
//		System.out.println("农历:" + today.getLunar());
//		System.out.println("公历:" + today.getSolar());
//		System.out.println("假期名:" + today.getVacationName());
//		System.out.println("是否周六:" + today.isSaturday());
//		System.out.println("是否周日:" + today.isSunday());
//		System.out.println("是否休假:" + today.isVacation());
//		System.out.println("是否工作日:" + today.isWorkFlag());
//		System.out.println("已发生的最近一次假期:" + Main.latestVocationName);
	}
	
	
	
	
}