定时任务:
import okhttp3.*;
import java.io.IOException;
import java.util.TimerTask;
public class AutoNose extends TimerTask {
private static final String AUTO_NOSE_URL = "https://api.juejin.cn/growth_api/v1/check_in";
private static final String AUTO_LOTTERY_URL = "https://api.juejin.cn/growth_api/v1/lottery/draw";
//填自己的cookies
private static final String COOKIES = "you cookies";
@Override
public void run() {
try {
//自动签到
request(AUTO_NOSE_URL, COOKIES);
//自动抽奖
request(AUTO_LOTTERY_URL, COOKIES);
} catch (IOException e) {
e.printStackTrace();
}
}
private boolean request(String url, String cookies) throws IOException {
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), "");
Request request = new Request.Builder()
.url(url)
.header("Cookie", cookies)
.post(requestBody)
.build();
OkHttpClient client = new OkHttpClient();
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
String res = response.body().string();
System.out.printf("request success:%s\n", res);
return true;
}
return false;
}
}
执行入口
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
public class Application {
public static void main(String[] args) {
AutoNose task = new AutoNose();
Calendar calendar = Calendar.getInstance();
Date firstTime = calendar.getTime();
//间隔:24小时执行一次
long period = 1000 * 60 * 60 * 24;
Timer timer = new Timer();
timer.schedule(task, firstTime, period);
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.swq</groupId>
<artifactId>JueJinAuto</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.6.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<!--main方法入口-->
<mainClass>Application</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase> <!-- packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
打包上传云服务器执行
//打包
mvn clean package
//执行
nohup java -jar xxx-with-dependencies.jar > log.file 2>&1 &