创建一个SpringBoot项目
添加依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>${hutool.version}</version></dependency><dependency><groupId>com.squareup.okhttp3</groupId><artifactId>okhttp</artifactId></dependency>
<!-- redis--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>
开启定时任务
编写自动签到代码
@Api(tags = "掘金")
@RestController
@RequestMapping("/jue-jin")
@Slf4j
@ResponseInfoSkin
public class JueJin {public static final String COOKIE = ":cookie";public static final String JUEJIN="juejin";// 每天9点15中开始执行public static final String IN_TIME = "0 15 09 ? * *";// 每天9点17中开始执行public static final String DRAW_TIME = "0 17 09 ? * *";@Resourceprivate RedisServiceUtil redisServiceUtil;@SneakyThrows@GetMapping("sign-in")@ApiOperation("签到")@Scheduled(cron = IN_TIME)public String signIn() {log.info("掘金自动签到开始");String str = redisServiceUtil.get(JUEJIN + COOKIE);//List<String> list = JSONUtil.toBean(str, List.class);List<String> list = JsonUtils.toObject(str, List.class);if (CommUtil.isEmpty(list)) {return "失败";}Map<String, String> header = Maps.newHashMap();//签到任务接口String url = "https://api.juejin.cn/growth_api/v1/check_in";for (String id : list) {//你的cookieString juejinCookie =id;RequestBody requestBody = new FormBody.Builder().build();String post = post(url, juejinCookie, requestBody, header);log.info("执行成功的cookie:{},执行结果为::{}", id, post);}return "成功";}//post请求的工具类方法public static String post(String url, String cookie, RequestBody requestBody, Map<String, String> header) throws Exception {String userAgent = "okhttp/3.12.1;jdmall;android;version/10.3.4;build/92451;";OkHttpClient client = new OkHttpClient().newBuilder().build();Request request = new Request.Builder().url(url).post(requestBody).headers(Headers.of(header)).addHeader("Cookie", cookie).addHeader("User-Agent", userAgent).addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8").addHeader("Cache-Control", "no-cache").addHeader("connection", "Keep-Alive").addHeader("accept", "*/*").build();Response response = client.newCall(request).execute();String result = response.body().string();log.info("post请求,result:{}", result);return result;}@SneakyThrows@GetMapping("lottery-draw")@ApiOperation("抽奖")@Scheduled(cron = DRAW_TIME)public String lotteryDraw() throws Exception {log.info("掘金自动抽奖开始");String str = redisServiceUtil.get(JUEJIN + COOKIE);//List<String> list = JSONUtil.toBean(str, List.class);List<String> list = JsonUtils.toObject(str, List.class);if (CommUtil.isEmpty(list)) {return "失败";}Map<String, String> header = Maps.newHashMap();//抽奖接口String drawUrl = "https://api.juejin.cn/growth_api/v1/lottery/draw";for (String id : list) {//你的cookieString juejinCookie =id;RequestBody requestBody = new FormBody.Builder().build();String response = post(drawUrl, juejinCookie, requestBody, header);log.info("执行成功的cookie:{},执行结果为::{}", id, response);}return "success";}@PostMapping@ApiOperation("存cookie")public String set(@org.springframework.web.bind.annotation.RequestBody List<String> cookies) {String list = JSONUtil.toJsonStr(cookies);redisServiceUtil.set(JUEJIN + ":cookie", list);return "success";}
}
swagger 接口
如何找到自己的掘金cookie
复制自己的cookie
粘贴到接口:存cookie
定时:@Scheduled(cron = IN_TIME)
这个注解就不在这里展开了
编写Dockerfile
FROM eclipse-temurin:8-jre## 创建目录,并使用它作为工作目录
RUN mkdir -p /home/project/kkxx-poi
WORKDIR /home/project/kkxx-poi
## 将后端项目的 Jar 文件,复制到镜像中
COPY ./kkxx-poi-1.1-SNAPSHOT.jar app.jar## 设置 TZ 时区
## 设置 JAVA_OPTS 环境变量,可通过 docker run -e "JAVA_OPTS=" 进行覆盖
ENV TZ=Asia/Shanghai JAVA_OPTS="-Xms128m -Xmx512m"## 暴露后端项目的 9091 端口
EXPOSE 9091# -Djava.security.egd=file:/dev/./urandom 这是加快生成随机数的配置
# https://blog.csdn.net/qq_39581637/article/details/122719838
## 启动后端项目
CMD java ${JAVA_OPTS} -jar app.jar
复制到自己服务器某个目录
构建镜像
docker build -f /home/project/Dockerfile -t kkxx-poi-image:test .
启动镜像
docker run -d --name kkxx-poi-name -p 9092:9091 kkxx-poi-image:test
手动测试swagger文档地址
http://ip:9092/doc.html