package ga.yehao.autocar.service;
import ga.yehao.autocar.email.Configuration;
import ga.yehao.autocar.email.Email;
import ga.yehao.autocar.email.Messaging;
import ga.yehao.autocar.util.DataUtil;
import ga.yehao.autocar.util.HttpUtil;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JButton;
import javax.swing.JTextArea;
import org.apache.log4j.Logger;
import org.junit.Test;
/**
* @author yehao
* @ClassName 类名称
* @Description 类描述
*/
public class AutoCarService {
private JButton startButton = null;
private JButton stopButton = null;
private JTextArea logArea = null;
private int counts = 1;
private final static Logger logger = Logger.getLogger(AutoCarService.class);
private String[] receivers = new String[] { "354896153@qq.com" };
private String[] orderDays = new String[] { "星期六", "星期日" };
private String[] orderPeriods = new String[] { "上午", "下午" };
private Integer timeSpace = 5;
public AutoCarService() {
String receivers = Configuration.getConfig("mail.receiver.address");
String orderDays = Configuration.getConfig("time.order.days");
String orderPeriods = Configuration.getConfig("time.order.periods");
String timeSpace = Configuration.getConfig("time.space");
if (!DataUtil.isBlank(receivers)) {
this.receivers = receivers.split(",");
}
System.out.println(orderDays);
if (!DataUtil.isBlank(orderDays)) {
this.orderDays = orderDays.split(",");
}
if (!DataUtil.isBlank(orderPeriods)) {
this.orderPeriods = orderPeriods.split(",");
}
if (!DataUtil.isBlank(timeSpace)) {
this.timeSpace = Integer.parseInt(timeSpace);
}
}
public String getResponseHtml() throws Exception {
// 防session过期请求
logger.info("send 防session过期请求 始... ...");
HttpUtil.doHttpAndHttps(
"http://member.gjjx.com.cn/service/login.ashx",
"sfzh=411122199504040092&time=1533000396&auth=cb1d8225f1a9ea4b325d968eea7def73&selmenu=yyxl&username=yehao279",
HttpUtil.REQ_METHOD_GET);
logger.info("放入登录cookie... ...");
logger.info("send 防session过期请求 完... ...");
logger.info("请求约车url:");
String responseContent = HttpUtil.doHttpAndHttps(
"http://member.gjjx.com.cn/ych2.aspx", "yt=2",
HttpUtil.REQ_METHOD_GET);
return responseContent;
}
/**
* 查询匹配
*
* @param responseContent
* @param tagretMap
*/
public void autoCarSearch(String responseContent,
Map<Integer, Object> tagretMap, String timeStage) {
logger.info("查询匹配... ...");
String regx = null;
switch (timeStage) {
case "上午":
regx = "<td>07:00--11:30</td>.*?</tr>";
break;
case "下午":
regx = "<td>12:30--17:00</td>.*?</tr>";
break;
case "晚上":
regx = "<td>17:45--20:45</td>.*?</tr>";
break;
default:
break;
}
Pattern pattern = Pattern.compile(regx);
Matcher matcher = pattern.matcher(responseContent);
if (!matcher.find()) {
logger.info("请确认是否登录!");
Email email = new Email();
email.setSubject("约车失败");
email.setContent("您的登录已过期。请重新抓取cookie。");
email.setTo(receivers);
Messaging.sendEmail(email);
this.stop();
return;
}
String amContent = matcher.group();
pattern = Pattern.compile("<td>.*?</td>");
matcher = pattern.matcher(amContent);
Long amCounts = 1L;
String amItem = null;
boolean sendEmail = false;
while (matcher.find()) {
amItem = matcher.group();
if (tagretMap.get(amCounts) != null && !amItem.contains("无车")) {
logger.info("恭喜:" + tagretMap.get(amCounts));
logArea.append("恭喜:" + tagretMap.get(amCounts) + "\r\n");
Email email = new Email();
email.setSubject("约车提醒");
email.setContent("您监控的驾校预约信息有余额,请赶快登陆进行预约!"
+ tagretMap.get(amCounts));
email.setTo(receivers);
Messaging.sendEmail(email);
sendEmail = true;
}
amCounts++;
}
if (sendEmail) {
logger.info("查询完毕... ... 已经将预约提醒信息发送至邮箱。");
logArea.append("查询完毕... ... 已经将预约提醒信息发送至邮箱。\r\n");
this.stop();
} else {
logger.info("查询中... ... 未发现可预约车辆,继续循环调度。");
logArea.append("查询中... ... 未发现可预约车辆,继续循环调度。\r\n");
}
}
/**
* 匹配表头,计算times所在位置
*
* @param responseContent
* @param times
*/
public Map<Integer, Object> autoCarCalc(String responseContent,
String[] times) {
Pattern pattern = Pattern.compile("<th>.*?</th>");
Matcher matcher = pattern.matcher(responseContent);
Integer headerCounts = 1;
String header = null;
Map<Integer, Object> tagretMap = new HashMap<>();
while (matcher.find()) {
header = matcher.group();
if (header != null) {
for (String time : times) {
if (header.contains(time)) {
tagretMap.put(headerCounts, header);
}
}
headerCounts++;
}
}
return tagretMap;
}
/**
* 自动查车程序
*
* @param times
* : 日期或星期,例如:星期六,星期日
* @timeStage : 时间段,例如:上午,下午,晚上
* @throws Exception
*/
public void autoCar(String[] times, String[] timeStage) {
logArea.setText("正在进行查车程序,当前第" + (counts++)
+ "次,详细日志请查看autocar.log ... ...");
String responseHtml = null;
try {
responseHtml = this.getResponseHtml();
} catch (Exception e) {
throw new RuntimeException(e);
}
Map<Integer, Object> tagretMap = this.autoCarCalc(responseHtml, times);
for (String stage : timeStage) {
this.autoCarSearch(responseHtml, tagretMap, stage);
}
}
public String[] getReceivers() {
return receivers;
}
public void setReceivers(String[] receivers) {
this.receivers = receivers;
}
public String[] getOrderDays() {
return orderDays;
}
public void setOrderDays(String[] orderDays) {
this.orderDays = orderDays;
}
public String[] getOrderPeriods() {
return orderPeriods;
}
public void setOrderPeriods(String[] orderPeriods) {
this.orderPeriods = orderPeriods;
}
public Integer getTimeSpace() {
return timeSpace;
}
public void setTimeSpace(Integer timeSpace) {
this.timeSpace = timeSpace;
}
private ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = null;
public void start(JButton startButton, JButton stopButton, JTextArea logArea) {
this.startButton = startButton;
this.stopButton = stopButton;
this.logArea = logArea;
this.startButton.setEnabled(false);
this.stopButton.setEnabled(true);
if (scheduledThreadPoolExecutor == null) {
scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(2);
}
scheduledThreadPoolExecutor
.scheduleAtFixedRate(
() -> this.autoCar(this.getOrderDays(),
this.getOrderPeriods()), 0,
this.getTimeSpace(), TimeUnit.SECONDS);
}
public void stop() {
startButton.setEnabled(true);
stopButton.setEnabled(false);
scheduledThreadPoolExecutor.shutdown();
scheduledThreadPoolExecutor = null;
}
@Test
public void test() throws Exception {
String doHttpAndHttps = HttpUtil
.doHttpAndHttps(
"http://member.gjjx.com.cn/service/login.ashx",
"sfzh=411122199504040092&time=1533000396&auth=cb1d8225f1a9ea4b325d968eea7def73&selmenu=yyxl&username=yehao279",
HttpUtil.REQ_METHOD_GET);
System.out.println(doHttpAndHttps);
}
}