| 29337 |
amit.gupta |
1 |
package com.smartdukaan.cron.scheduled;
|
|
|
2 |
|
|
|
3 |
import com.cloudconvert.client.CloudConvertClient;
|
|
|
4 |
import com.cloudconvert.client.setttings.StringSettingsProvider;
|
|
|
5 |
import com.cloudconvert.dto.request.Base64ImportRequest;
|
|
|
6 |
import com.cloudconvert.dto.request.ConvertFilesTaskRequest;
|
|
|
7 |
import com.cloudconvert.dto.request.UrlExportRequest;
|
|
|
8 |
import com.cloudconvert.dto.response.JobResponse;
|
|
|
9 |
import com.cloudconvert.dto.response.TaskResponse;
|
|
|
10 |
import com.google.common.collect.ImmutableMap;
|
|
|
11 |
import com.twocaptcha.TwoCaptcha;
|
|
|
12 |
import com.twocaptcha.captcha.Normal;
|
| 30335 |
amit.gupta |
13 |
import org.apache.commons.io.FileUtils;
|
|
|
14 |
import org.apache.commons.io.IOUtils;
|
|
|
15 |
import org.apache.logging.log4j.LogManager;
|
|
|
16 |
import org.apache.logging.log4j.Logger;
|
|
|
17 |
import org.springframework.stereotype.Service;
|
| 29337 |
amit.gupta |
18 |
|
| 30335 |
amit.gupta |
19 |
import java.io.File;
|
|
|
20 |
import java.io.FileOutputStream;
|
|
|
21 |
import java.io.InputStream;
|
|
|
22 |
import java.io.OutputStream;
|
|
|
23 |
import java.util.Base64;
|
|
|
24 |
|
| 29337 |
amit.gupta |
25 |
@Service
|
|
|
26 |
public class CaptchaService {
|
|
|
27 |
|
| 30335 |
amit.gupta |
28 |
private static final Logger LOGGER = LogManager.getLogger(CaptchaService.class);
|
| 29337 |
amit.gupta |
29 |
|
|
|
30 |
public String getCaptchaCode(String filePath) throws Exception {
|
| 30335 |
amit.gupta |
31 |
File inputFile = new File(filePath);
|
|
|
32 |
//File jpgFile = this.getCloudConvertJpgImg(inputFile);
|
| 29337 |
amit.gupta |
33 |
TwoCaptcha solver = new TwoCaptcha("dbb4583561b064de1c0a48b8232c72e8");
|
| 30335 |
amit.gupta |
34 |
Normal captcha = new Normal(inputFile.getAbsolutePath());
|
| 29337 |
amit.gupta |
35 |
System.out.println("Before captcha");
|
|
|
36 |
solver.solve(captcha);
|
| 30335 |
amit.gupta |
37 |
String captchaCode = captcha.getCode();
|
|
|
38 |
System.out.println("Captcha code - " + captchaCode);
|
|
|
39 |
return captchaCode;
|
| 29337 |
amit.gupta |
40 |
}
|
|
|
41 |
|
|
|
42 |
private File getCloudConvertJpgImg(File avifImg) throws Exception {
|
|
|
43 |
final CloudConvertClient cloudConvertClient = new CloudConvertClient(new StringSettingsProvider(
|
| 30335 |
amit.gupta |
44 |
"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIxIiwianRpIjoiMWU3YmY3ZjllZDY5OGE5Y2I2ZTgyZDJkMDZjMjNhNDM3Zjc5MmY2MjBiMDhlOGU1NzM5ZTZlYWMwZjgwZjBmZjBiMWI3ZDBiNDI3MDdmYTkiLCJpYXQiOjE2NTA2MDgwNzguNjgzMDg2LCJuYmYiOjE2NTA2MDgwNzguNjgzMDg4LCJleHAiOjQ4MDYyODE2NzguNjcwMDA3LCJzdWIiOiI1NzQ5OTY2NCIsInNjb3BlcyI6WyJ1c2VyLnJlYWQiLCJ1c2VyLndyaXRlIiwidGFzay5yZWFkIiwidGFzay53cml0ZSIsIndlYmhvb2sucmVhZCIsInByZXNldC5yZWFkIiwid2ViaG9vay53cml0ZSIsInByZXNldC53cml0ZSJdfQ.KmzPV6tjsE02zdI-7UC_SLjcfkLAbApgSeVK60EP-IsLwSlhLyIoDt4x-MggP3NYUtsQI-IsTGmvSo3djn5nSjvt4ZLor3yhqRK5j4U8rtp47JDvsePZAtOJ7cAz6n7Snqpx5g0Qxi4c0BV8l4xAAgNtxfpmzFwm-Hko8c9eytjrqn064XS5_rHNkiCIc9GKJsL2C4Qnsh-ffxOo02MsSXFLyggkPyG3YZYxY7jrjY3eKuzoDVYF-93Sz2MgSgUfhNCQDVOvw1HvVhuGg3lZACLYTQGogubqbONB4oX9rMr4wgutbc5tsle-17Cuv6KmE7-Hni69doEoUi_7sbUxLlxcNIjYBcl9hQKDOr2TuFRjm4U9W4KPrOEpgRGeWkf-iju5mCW0Ay7rq_TgVxDKCKj2qJXhxOUXvKhIdn068BBhoOlMMFeFMQb8paqXAb-2BLbDf6yEUDiAdDeByp6UP3uiPia8SvgOJoT9C9smEKe3rfv6b0Cf-WldZDb3lA7EzsUR8SngFS50tgUR-GTy2q0jLzJkZLv5NQBlhmfdrBzpDwsiAc_A_kNv9W1WzhuyaDNeNcC9RoZ43minfyQQ86mnEwNCPiZwUBGcqnW1fXJ12FNaalU_GNtNJp1CtagQAA1UPazv_KqYwar_tbATuT1Xdj1aO7Iw6u4NGbMtSqY",
|
| 29337 |
amit.gupta |
45 |
"IemxwFEudKWn89TATevY3Ip492gmmgB1", false));
|
|
|
46 |
|
|
|
47 |
byte[] fileContent = FileUtils.readFileToByteArray(avifImg);
|
|
|
48 |
String encodedString = Base64.getEncoder().encodeToString(fileContent);
|
|
|
49 |
|
|
|
50 |
final JobResponse createJobResponse = cloudConvertClient.jobs()
|
|
|
51 |
.create(ImmutableMap.of("import-my-file",
|
|
|
52 |
new Base64ImportRequest().setFile(encodedString).setFilename(avifImg.getName()),
|
|
|
53 |
"convert-my-file",
|
|
|
54 |
new ConvertFilesTaskRequest().setInput("import-my-file").setInputFormat("avif").setOutputFormat(
|
|
|
55 |
"jpg"),
|
|
|
56 |
"export-my-file", new UrlExportRequest().setInput("convert-my-file")))
|
|
|
57 |
.getBody();
|
|
|
58 |
|
|
|
59 |
LOGGER.info("Create jobResponse - {}", createJobResponse);
|
|
|
60 |
|
|
|
61 |
// Get a job id
|
|
|
62 |
final String jobId = createJobResponse.getId();
|
|
|
63 |
|
|
|
64 |
// Wait for a job completion
|
|
|
65 |
final JobResponse waitJobResponse = cloudConvertClient.jobs().wait(jobId).getBody();
|
|
|
66 |
|
|
|
67 |
final String exportUrlTaskId = waitJobResponse.getTasks().stream()
|
|
|
68 |
.filter(taskResponse -> taskResponse.getName().equals("export-my-file")).findFirst().get().getId();
|
|
|
69 |
|
|
|
70 |
// Wait for an export/url task to be finished
|
|
|
71 |
final TaskResponse waitUrlExportTaskResponse = cloudConvertClient.tasks().wait(exportUrlTaskId).getBody();
|
|
|
72 |
|
|
|
73 |
// Get url and filename of export/url task
|
|
|
74 |
final String exportUrl = waitUrlExportTaskResponse.getResult().getFiles().get(0).get("url");
|
|
|
75 |
final String filename = waitUrlExportTaskResponse.getResult().getFiles().get(0).get("filename");
|
|
|
76 |
|
|
|
77 |
LOGGER.info(exportUrl, filename);
|
|
|
78 |
|
|
|
79 |
// Get file as input stream using url of export/url task
|
|
|
80 |
final InputStream inputStream = cloudConvertClient.files().download(exportUrl).getBody();
|
|
|
81 |
|
|
|
82 |
// Save to local file
|
|
|
83 |
File localFile = new File("/tmp/converted.jpg");
|
|
|
84 |
OutputStream outputStream = new FileOutputStream(localFile);
|
|
|
85 |
IOUtils.copy(inputStream, outputStream);
|
|
|
86 |
|
|
|
87 |
return localFile;
|
|
|
88 |
}
|
|
|
89 |
}
|