Skip to content

Commit

Permalink
添加一个定时器
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelmofa committed Aug 27, 2016
1 parent d787909 commit 5bfafe5
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
<relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
Expand All @@ -35,6 +35,11 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.9.2</version>
</dependency>
</dependencies>

<build>
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/mc/EteamsTestApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class EteamsTestApplication {

public static void main(String[] args) {
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/mc/controller/TestController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.mc.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value="/test")
public class TestController {
@RequestMapping("/time")
@ResponseBody
public String sayTime(String name){
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return name+":"+sf.format(new Date());
}
}
82 changes: 82 additions & 0 deletions src/main/java/com/mc/util/Login.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.mc.util;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import org.jsoup.Connection;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component("login")
public class Login {
private static Logger log = LoggerFactory.getLogger(Login.class);

// public static void main(String[] args) {
// int a = 10;
// int b = 0;
// try{
// int c = a/b;
// }catch(Exception e){
// log.error("error:",e);
// }
// }

// @Scheduled(fixedDelay=5000)
// public void test(){
// log.info("时间:"+new Date().getTime());
// }

@Scheduled(cron="0 20 8 ? * *")
public void check() throws Exception {
Random r = new Random();
Thread.sleep((r.nextInt(600)+1)*1000);
String url = "https://passport.eteams.cn";
String url_login = "https://passport.eteams.cn/login";

Map<String, String> cookies = new HashMap<String, String>();
Map<String, String> params = new HashMap<String, String>();
Connection conn = Jsoup.connect(url).header("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
Response index = conn.execute();
cookies = index.cookies();
Document doc = index.parse();

String lt = doc.select("input[name='lt']").attr("value");
String execution = doc.select("input[name='execution']").attr("value");
String j_pcClient = doc.select("input[name='j_pcClient']").attr("value");
String _eventId = doc.select("input[name='_eventId']").attr("value");
String isApplyed = doc.select("input[name='isApplyed']").attr("value");
String username = "18565155401";
String password = "zhangdd123";

params.put("lt", lt);
params.put("execution", execution);
params.put("j_pcClient", j_pcClient);
params.put("_eventId", _eventId);
params.put("isApplyed", isApplyed);
params.put("username", username);
params.put("password", password);
Response response = Jsoup.connect(url_login).ignoreContentType(true).method(Method.POST).cookies(cookies).data(params).execute();
cookies = response.cookies();

conn = Jsoup.connect("https://www.eteams.cn/timecard/check.json");
conn.header("Host", "www.eteams.cn");
conn.header("Origin", "https://www.eteams.cn");
conn.header("Pragma", "no-cache");
conn.header("Referer", "https://www.eteams.cn/");
conn.header("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36");
conn.header("X-Requested-With", "XMLHttpRequest");

Map<String, String> data = new HashMap<String, String>();
String op = "CHECKIN";
data.put("type", op);
Response execute = conn.ignoreContentType(true).cookies(cookies).method(Method.POST).data(data).execute();
log.info(execute.body());
}
}
2 changes: 2 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
logging.file=/logs/mc/log.log
logging.level.org.springframework.web = debug
10 changes: 10 additions & 0 deletions src/main/resources/static/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Insert title here</title>
</head>
<body>
this is a html test 123
</body>
</html>

0 comments on commit 5bfafe5

Please sign in to comment.