|
@@ -6,36 +6,41 @@ import com.qmth.boot.core.ai.model.ocr.ParseDocTaskResult;
|
|
|
import com.qmth.boot.core.ai.model.ocr.ParseDocTaskStatus;
|
|
|
import com.qmth.boot.core.exception.StatusException;
|
|
|
import com.qmth.boot.tools.codec.CodecUtils;
|
|
|
+import com.qmth.ops.biz.ai.client.DocApiClient;
|
|
|
import com.qmth.ops.biz.ai.client.OcrApiConfig;
|
|
|
import com.qmth.ops.biz.ai.client.baidu.BceV1Signer;
|
|
|
+import com.qmth.ops.biz.ai.exception.OcrRateLimitExceeded;
|
|
|
import okhttp3.*;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.Date;
|
|
|
import java.util.TimeZone;
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
-public class BaiduParseDocClient {
|
|
|
+public class BaiduParseDocClient extends DocApiClient {
|
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(BaiduParseDocClient.class);
|
|
|
|
|
|
- private OcrApiConfig config;
|
|
|
-
|
|
|
public BaiduParseDocClient(OcrApiConfig config) {
|
|
|
- this.config = config;
|
|
|
+ super(config);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
public ParseDocTask parseDocTask(byte[] fileData, String fileName) throws Exception {
|
|
|
+ if (getQueryRateLimiter() != null && !getQueryRateLimiter().acquire()) {
|
|
|
+ throw new OcrRateLimitExceeded(getConfig().getQps());
|
|
|
+ }
|
|
|
+
|
|
|
FormBody.Builder formBuilder = new FormBody.Builder();
|
|
|
formBuilder.add("file_data", CodecUtils.toBase64(fileData));
|
|
|
formBuilder.add("file_name", CodecUtils.urlEncode(fileName));
|
|
|
- String url = config.getUrl() + "/rest/2.0/brain/online/v2/parser/task";
|
|
|
+ String url = getConfig().getUrl() + "/rest/2.0/brain/online/v2/parser/task";
|
|
|
Request request = this.buildRequest(url, formBuilder);
|
|
|
|
|
|
- try (Response response = this.getHttpClient().newCall(request).execute()) {
|
|
|
+ try (Response response = super.getClient().newCall(request).execute()) {
|
|
|
ResponseBody respBody = response.body();
|
|
|
String respBodyStr = respBody != null ? respBody.string() : "";
|
|
|
log.info(respBodyStr);
|
|
@@ -54,13 +59,18 @@ public class BaiduParseDocClient {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
public ParseDocTaskResult parseDocTaskQuery(String taskId) throws Exception {
|
|
|
+ if (getQueryRateLimiter() != null && !getQueryRateLimiter().acquire()) {
|
|
|
+ throw new OcrRateLimitExceeded(getConfig().getQps());
|
|
|
+ }
|
|
|
+
|
|
|
FormBody.Builder formBuilder = new FormBody.Builder();
|
|
|
formBuilder.add("task_id", taskId);
|
|
|
- String url = config.getUrl() + "/rest/2.0/brain/online/v2/parser/task/query";
|
|
|
+ String url = getConfig().getUrl() + "/rest/2.0/brain/online/v2/parser/task/query";
|
|
|
Request request = this.buildRequest(url, formBuilder);
|
|
|
|
|
|
- try (Response response = this.getHttpClient().newCall(request).execute()) {
|
|
|
+ try (Response response = super.getClient().newCall(request).execute()) {
|
|
|
ResponseBody respBody = response.body();
|
|
|
String respBodyStr = respBody != null ? respBody.string() : "";
|
|
|
log.info(respBodyStr);
|
|
@@ -73,7 +83,11 @@ public class BaiduParseDocClient {
|
|
|
|
|
|
ParseDocTaskResult result = new ParseDocTaskResult();
|
|
|
result.setStatus(status != null ? status : ParseDocTaskStatus.FAILED);
|
|
|
- result.setContent(respResult.getMarkdownUrl());
|
|
|
+ if (ParseDocTaskStatus.SUCCESS == status) {
|
|
|
+ byte[] data = super.download(respResult.getMarkdownUrl());
|
|
|
+ result.setContent(new String(data, StandardCharsets.UTF_8));
|
|
|
+ }
|
|
|
+
|
|
|
return result;
|
|
|
}
|
|
|
}
|
|
@@ -98,14 +112,7 @@ public class BaiduParseDocClient {
|
|
|
.build();
|
|
|
|
|
|
return request.newBuilder().addHeader(HttpHeaders.AUTHORIZATION,
|
|
|
- BceV1Signer.sign(request, config.getKey(), config.getSecret())).build();
|
|
|
- }
|
|
|
-
|
|
|
- private OkHttpClient getHttpClient() {
|
|
|
- return new OkHttpClient.Builder()
|
|
|
- .readTimeout(60, TimeUnit.SECONDS)
|
|
|
- .connectTimeout(60, TimeUnit.SECONDS)
|
|
|
- .build();
|
|
|
+ BceV1Signer.sign(request, getConfig().getKey(), getConfig().getSecret())).build();
|
|
|
}
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
@@ -114,7 +121,7 @@ public class BaiduParseDocClient {
|
|
|
config.setKey("xxx");
|
|
|
config.setSecret("xxx");
|
|
|
config.setQps(10);
|
|
|
- BaiduParseDocClient client = new BaiduParseDocClient(config);
|
|
|
+ DocApiClient client = new BaiduParseDocClient(config);
|
|
|
|
|
|
// File file = new File("D:\\home\\大纲.pdf");
|
|
|
// byte[] fileData = ByteArray.fromFile(file).value();
|