Skip to content
This repository has been archived by the owner on Dec 4, 2023. It is now read-only.

Commit

Permalink
remove validate code and ocr module
Browse files Browse the repository at this point in the history
  • Loading branch information
GCS-ZHN committed May 18, 2022
1 parent 0741cab commit d3645d2
Show file tree
Hide file tree
Showing 17 changed files with 23 additions and 1,433 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/schedule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ on:
branches: [ main ]

schedule:
# 东八区09:00,9:20,9:40尝试三次,触发不完全准时,可能延时半个到一个小时等。
- cron: '0/20 1 * * *'
# UTC 01:00 和 UTC 04:00
- cron: '0 1,4 * * *'

# 当star当前仓库时立即执行
watch:
Expand Down Expand Up @@ -43,9 +43,6 @@ jobs:
java-version: '11'
cache: "maven"

- name: "Setup tesseract-ocr"
run: "sudo apt install tesseract-ocr -y"

- name: "Build with maven"
run: "mvn clean package spring-boot:repackage"

Expand Down
3 changes: 1 addition & 2 deletions config/application.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,5 @@
],
"formvalidation": true,
"enablepreview": false,
"testmode": false,
"ocr": "d4_ocr"
"testmode": false
}
24 changes: 2 additions & 22 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

<groupId>top.gcszhn</groupId>
<artifactId>autocard</artifactId>
<version>1.4.9</version>
<version>1.4.10</version>
<packaging>jar</packaging>

<name>Auto Heathy Report for Zhejiang University</name>
<name>Automatically Healthy Report for Zhejiang University</name>

<parent>
<groupId>org.springframework.boot</groupId>
Expand Down Expand Up @@ -107,26 +107,6 @@
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>

<!-- https://mvnrepository.com/artifact/net.java.dev.jna/jna -->
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>5.10.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/net.sourceforge.tess4j/tess4j -->
<dependency>
<groupId>net.sourceforge.tess4j</groupId>
<artifactId>tess4j</artifactId>
<version>5.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.microsoft.onnxruntime/onnxruntime -->
<dependency>
<groupId>com.microsoft.onnxruntime</groupId>
<artifactId>onnxruntime</artifactId>
<version>1.11.0</version>
</dependency>
<!--lombok插件-->
<dependency>
<groupId>org.projectlombok</groupId>
Expand Down
11 changes: 2 additions & 9 deletions src/main/java/top/gcszhn/autocard/AppConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import lombok.Getter;
import top.gcszhn.autocard.service.MailService;
import top.gcszhn.autocard.utils.LogUtils;
import top.gcszhn.autocard.utils.ocr.EngineType;


/**
Expand All @@ -47,6 +46,7 @@
public class AppConfig implements EnvironmentAware {
/**默认字符集 */
public static final Charset APP_CHARSET = StandardCharsets.UTF_8;
/** 创建APP临时目录*/
public static final File TMP_DIR = new File(System.getProperty("java.io.tmpdir") , "autocard");
static {
try {
Expand All @@ -66,8 +66,7 @@ public class AppConfig implements EnvironmentAware {
private @Getter boolean testMode = false;
/**是否启用预览特性 */
private @Getter boolean enablePreview = false;
/**默认OCR引擎 */
private @Getter EngineType ocrEngine = EngineType.D4_OCR;
/** 加载APP配置文件*/
public AppConfig() {
try (FileInputStream fis = new FileInputStream(APP_CACHE)) {
appCache = JSON.parseObject(new String(fis.readAllBytes(), APP_CHARSET));
Expand All @@ -86,12 +85,6 @@ public void setEnvironment(Environment env) {
loadJSONConfig(env.getProperty("app.autoCard.config"));
testMode = appConfig.getBooleanValue("testmode");
enablePreview = appConfig.getBooleanValue("enablepreview");
try {
ocrEngine = EngineType.valueOf(appConfig.getString("ocr").toUpperCase());
LogUtils.printMessage("OCR引擎为" + ocrEngine);
} catch (Exception e) {
LogUtils.printMessage("未配置有效的OCR引擎,使用默认OCR引擎" + ocrEngine);
}

// 通过系统环境变量添加单个打卡用户

Expand Down
114 changes: 16 additions & 98 deletions src/main/java/top/gcszhn/autocard/service/AutoCardService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package top.gcszhn.autocard.service;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
Expand All @@ -29,10 +27,8 @@
import lombok.Getter;
import top.gcszhn.autocard.AppConfig;
import top.gcszhn.autocard.utils.DigestUtils;
import top.gcszhn.autocard.utils.HttpDataPair;
import top.gcszhn.autocard.utils.ImageUtils;
import top.gcszhn.autocard.utils.LogUtils;
import top.gcszhn.autocard.utils.ocr.OCRUtils;
import top.gcszhn.autocard.utils.StatusCode;

import org.apache.http.NameValuePair;
Expand All @@ -45,8 +41,6 @@
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;

/**
* 健康打卡实现类
* @author Zhang.H.N
Expand All @@ -65,8 +59,7 @@ public class AutoCardService implements AppService {
private String reportUrl;
@Value("${app.autoCard.submitUrl}")
private String submitUrl;
@Value("${app.autoCard.codeUrl}")
private String codeUrl;

/**浙大通行证客户端 */
@Autowired
private ZJUClientService client;
Expand Down Expand Up @@ -201,55 +194,7 @@ public ArrayList<NameValuePair> getOldInfo(String page) {
}
return res;
}
public BufferedImage getCodeImage() {
if (isOnline()) {
LogUtils.printMessage("获取验证码", LogUtils.Level.INFO);
return Optional.ofNullable(client.doGet(codeUrl + "?_t=" + Math.random())).map((HttpDataPair pair)-> {
try {
if (pair.getResponse() != null && pair.getResponse().getEntity() != null) {
BufferedImage image = ImageIO.read(pair.getResponse().getEntity().getContent());
return image;

}
} catch (IOException e) {
LogUtils.printMessage(null, e, LogUtils.Level.ERROR);
} finally {
try {
pair.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}).orElse(null);
} else {
LogUtils.printMessage("用户未登录", LogUtils.Level.ERROR);
}
return null;
}
public String getCode(BufferedImage codeImage) {
Optional<String> code = Optional.ofNullable(codeImage).map((BufferedImage image)->{
try {
LogUtils.printMessage("识别验证码", LogUtils.Level.INFO);
String value = OCRUtils.instance(appConfig.getOcrEngine()).recognize(image);
if (value != null) {
value = value.strip().toUpperCase();
value.replaceAll("1", "I");
if (value.length() != 4) {
value = null;
}
}
return value;
} catch (Exception e) {
LogUtils.printMessage("验证码识别异常", e, LogUtils.Level.ERROR);
return null;
}
});
return code.isPresent() ? code.get(): null;
}
public String getCode() {
return getCode(getCodeImage());
}
/**
* 用于提交打卡信息
* @param username 用户名
Expand Down Expand Up @@ -278,13 +223,9 @@ public StatusCode submit(String username, String password) {
LogUtils.printMessage(statusCode.getMessage(), LogUtils.Level.ERROR);
return statusCode;
}
int validCodeMaxTrial = 20;
JSONObject resp = null;
LogUtils.Level level = null;
int status = 3;
NameValuePair codePair = null;
JSONObject resp;
LogUtils.Level level;
String area = null;

ArrayList<NameValuePair> info = getOldInfo(page);
if (info==null) {
LogUtils.printMessage("打卡信息获取失败", LogUtils.Level.ERROR);
Expand All @@ -299,45 +240,22 @@ public StatusCode submit(String username, String password) {
break;
}
}
SAVE: while (validCodeMaxTrial > 0) {

while (validCodeMaxTrial > 0) {
validCodeMaxTrial --;
String code = getCode();
if (code != null) {
if (codePair != null) info.remove(codePair);
codePair = new BasicNameValuePair("verifyCode", code);
info.add(codePair);
break;
} else {
LogUtils.printMessage("验证码识别错误,剩余机会:" + validCodeMaxTrial, LogUtils.Level.ERROR);
}
}

try {
LogUtils.printMessage("准备提交打卡 " + username);
resp = JSONObject.parseObject(client.doPostText(submitUrl, info));
} catch (Exception e) {
resp = new JSONObject();
resp.put("e", 3);
resp.put("m", "打卡提交失败");
}
try {
LogUtils.printMessage("准备提交打卡 " + username);
resp = JSONObject.parseObject(client.doPostText(submitUrl, info));
} catch (Exception e) {
resp = new JSONObject();
resp.put("e", 3);
resp.put("m", "打卡提交失败");
}

status = resp.getIntValue("e");
switch(status) {
case 0:{level= LogUtils.Level.INFO;break;}
case 1:{
if (validCodeMaxTrial > 0 && resp.getString("m").equals("验证码错误")) {
LogUtils.printMessage("验证码识别错误,剩余机会:" + validCodeMaxTrial, LogUtils.Level.ERROR);
Thread.sleep(2000);
continue SAVE;
}
}
default: {
level = LogUtils.Level.ERROR;
}
int status = resp.getIntValue("e");
switch(status) {
case 0:{level= LogUtils.Level.INFO;break;}
default: {
level = LogUtils.Level.ERROR;
}
break;
}

String message = String.format("%s,你好,今日自动健康打卡状态:%s,打卡地区为:%s(如若区域不符,请次日手动打卡更改地址)",
Expand Down
86 changes: 0 additions & 86 deletions src/main/java/top/gcszhn/autocard/utils/ONNXRuntimeUtils.java

This file was deleted.

Loading

0 comments on commit d3645d2

Please sign in to comment.