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);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
autocar.rar (63个子文件)
autocar
.project 1KB
src
test
java
ga
yehao
autocar
test
HttpTestDemo.java 231B
main
webapp
WEB-INF
web.xml 215B
index.jsp 52B
NewFile.html 7KB
resources
log4j.properties 1KB
configuration.properties 318B
java
ga
yehao
autocar
service
AutoCarService.java 8KB
AutoOaService.java 2KB
view
Gbk2utf8.java 2KB
View.java 1KB
email
Configuration.java 2KB
Email.java 1KB
Messaging.java 4KB
Attachment.java 463B
util
HttpUtil.java 7KB
DataUtil.java 3KB
target
m2e-wtp
web-resources
META-INF
MANIFEST.MF 107B
maven
ga.yehao
autocar
pom.properties 210B
pom.xml 3KB
autocar
WEB-INF
classes
log4j.properties 1KB
ga
yehao
autocar
service
AutoCarService.class 9KB
email
Email.class 2KB
Configuration.class 3KB
Messaging.class 4KB
Attachment.class 788B
util
HttpUtil.class 9KB
DataUtil.class 3KB
configuration.properties 202B
lib
activation-1.1.jar 62KB
javax.mail-1.5.5.jar 589KB
log4j-1.2.17.jar 478KB
web.xml 215B
index.jsp 52B
META-INF
NewFile.html 7KB
generated-test-sources
test-annotations
maven-status
maven-compiler-plugin
testCompile
default-testCompile
createdFiles.lst 0B
inputFiles.lst 76B
compile
default-compile
createdFiles.lst 0B
inputFiles.lst 522B
generated-sources
annotations
classes
ga
yehao
autocar
service
AutoOaService.class 2KB
AutoCarService.class 10KB
view
Gbk2utf8.class 2KB
View.class 3KB
email
Email.class 2KB
Configuration.class 3KB
Messaging.class 4KB
Attachment.class 788B
util
HttpUtil.class 9KB
DataUtil.class 3KB
autocar.war 1.04MB
maven-archiver
pom.properties 94B
test-classes
ga
yehao
autocar
test
HttpTestDemo.class 479B
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 252B
org.eclipse.m2e.core.prefs 90B
org.eclipse.jdt.core.prefs 430B
org.eclipse.wst.validation.prefs 50B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.core.resources.prefs 216B
org.eclipse.wst.common.component 662B
.jsdtscope 575B
pom.xml 3KB
.classpath 1KB
共 63 条
- 1
资源评论
yehao279
- 粉丝: 7
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功