| 29451 |
manish |
1 |
package com.smartdukaan.cron.scheduled;
|
|
|
2 |
|
| 30209 |
amit.gupta |
3 |
import com.spice.profitmandi.dao.model.ImeiActivationTimestampModel;
|
|
|
4 |
import com.spice.profitmandi.dao.repository.fofo.ActivatedImeiRepository;
|
|
|
5 |
import com.spice.profitmandi.service.inventory.InventoryService;
|
|
|
6 |
import okhttp3.*;
|
| 29451 |
manish |
7 |
import org.apache.logging.log4j.LogManager;
|
|
|
8 |
import org.apache.logging.log4j.Logger;
|
|
|
9 |
import org.json.JSONObject;
|
|
|
10 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
11 |
import org.springframework.stereotype.Component;
|
|
|
12 |
|
| 30209 |
amit.gupta |
13 |
import java.nio.file.Files;
|
|
|
14 |
import java.nio.file.Paths;
|
|
|
15 |
import java.time.LocalDate;
|
|
|
16 |
import java.time.format.DateTimeFormatter;
|
| 30335 |
amit.gupta |
17 |
import java.util.ArrayList;
|
|
|
18 |
import java.util.HashMap;
|
|
|
19 |
import java.util.List;
|
|
|
20 |
import java.util.Map;
|
| 29451 |
manish |
21 |
|
|
|
22 |
@Component
|
|
|
23 |
class VivoImeiActivationService {
|
|
|
24 |
|
| 33453 |
amit.gupta |
25 |
@Autowired
|
|
|
26 |
InventoryService inventoryService;
|
| 29451 |
manish |
27 |
|
| 33453 |
amit.gupta |
28 |
@Autowired
|
|
|
29 |
CaptchaService captchaService;
|
| 29466 |
amit.gupta |
30 |
|
| 33453 |
amit.gupta |
31 |
@Autowired
|
|
|
32 |
ActivatedImeiRepository activatedImeiRepository;
|
| 29451 |
manish |
33 |
|
| 33453 |
amit.gupta |
34 |
private static final Logger LOGGER = LogManager.getLogger(VivoImeiActivationService.class);
|
| 29451 |
manish |
35 |
|
| 33453 |
amit.gupta |
36 |
private final Map<String, List<Cookie>> cookieStore = new HashMap<>();
|
| 29451 |
manish |
37 |
|
| 33453 |
amit.gupta |
38 |
public void checkImeiActivation() throws Exception {
|
|
|
39 |
if (LocalDate.now().getDayOfMonth() == 1) return;
|
| 36253 |
amit |
40 |
|
|
|
41 |
List<ImeiActivationTimestampModel> imeisActivationList = activatedImeiRepository
|
|
|
42 |
.selectImeiActivationPendingByBrand("Vivo", 2);
|
|
|
43 |
LOGGER.info("Secondary imeisActivationList = {}, size = {}", imeisActivationList, imeisActivationList.size());
|
|
|
44 |
|
|
|
45 |
processImeis(imeisActivationList);
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
public void checkImeiActivationTertiary() throws Exception {
|
|
|
49 |
if (LocalDate.now().getDayOfMonth() == 1) return;
|
|
|
50 |
|
|
|
51 |
List<ImeiActivationTimestampModel> imeisActivationList = activatedImeiRepository
|
|
|
52 |
.selectImeiActivationPendingByBrandTertiary("Vivo", 2);
|
|
|
53 |
LOGGER.info("Tertiary imeisActivationList = {}, size = {}", imeisActivationList, imeisActivationList.size());
|
|
|
54 |
|
|
|
55 |
processImeis(imeisActivationList);
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
private void processImeis(List<ImeiActivationTimestampModel> imeisActivationList) throws Exception {
|
|
|
59 |
if (imeisActivationList.isEmpty()) return;
|
|
|
60 |
|
|
|
61 |
cookieStore.clear();
|
|
|
62 |
|
| 33453 |
amit.gupta |
63 |
OkHttpClient okHttpClient = new OkHttpClient.Builder().cookieJar(new CookieJar() {
|
|
|
64 |
@Override
|
|
|
65 |
public void saveFromResponse(HttpUrl httpUrl, List<Cookie> list) {
|
|
|
66 |
LOGGER.info("SAVE fROM - {}", httpUrl.host());
|
|
|
67 |
cookieStore.put(httpUrl.host(), list);
|
|
|
68 |
}
|
| 29466 |
amit.gupta |
69 |
|
| 33453 |
amit.gupta |
70 |
@Override
|
|
|
71 |
public List<Cookie> loadForRequest(HttpUrl httpUrl) {
|
|
|
72 |
List<Cookie> cookies = cookieStore.get(httpUrl.host());
|
|
|
73 |
LOGGER.info("load from - {}", httpUrl.host());
|
|
|
74 |
return cookies != null ? cookies : new ArrayList<>();
|
|
|
75 |
}
|
|
|
76 |
}).build();
|
| 29451 |
manish |
77 |
|
| 33453 |
amit.gupta |
78 |
Request request = new Request.Builder().url("https://www.vivo.com/in/support/IMEI").build();
|
| 36253 |
amit |
79 |
try (Response seedResponse = okHttpClient.newCall(request).execute()) {
|
|
|
80 |
// Consume response to establish cookies
|
|
|
81 |
}
|
| 29451 |
manish |
82 |
|
| 33453 |
amit.gupta |
83 |
for (ImeiActivationTimestampModel imeiActivationTimestampModel : imeisActivationList) {
|
|
|
84 |
String captchaBreak = this.getNewCaptcha(okHttpClient);
|
|
|
85 |
this.getImeiActivation(captchaBreak, okHttpClient, imeiActivationTimestampModel);
|
|
|
86 |
}
|
|
|
87 |
}
|
| 29451 |
manish |
88 |
|
| 33453 |
amit.gupta |
89 |
public String getNewCaptcha(OkHttpClient okHttpClient) throws Exception {
|
|
|
90 |
LOGGER.info("okHttpClient" + okHttpClient);
|
| 29451 |
manish |
91 |
|
| 33453 |
amit.gupta |
92 |
HttpUrl vivoSupportUrl = HttpUrl.parse("https://www.vivo.com/in/support/generatingCodes");
|
|
|
93 |
Request request = new Request.Builder().url(vivoSupportUrl).build();
|
| 29451 |
manish |
94 |
|
| 33453 |
amit.gupta |
95 |
String filePath = "/tmp/captcha.jpg";
|
| 36253 |
amit |
96 |
try (Response response = okHttpClient.newCall(request).execute()) {
|
|
|
97 |
LOGGER.info("cookie {}", this.cookieStore.values());
|
|
|
98 |
LOGGER.info("vivoSupportUrl" + vivoSupportUrl);
|
|
|
99 |
Files.write(Paths.get(filePath), response.body().bytes());
|
|
|
100 |
}
|
| 29451 |
manish |
101 |
|
| 33453 |
amit.gupta |
102 |
String captchaBreak = captchaService.getCaptchaCode(filePath);
|
|
|
103 |
System.out.println("Captcha is " + captchaBreak);
|
|
|
104 |
return captchaBreak;
|
|
|
105 |
}
|
| 29451 |
manish |
106 |
|
| 33453 |
amit.gupta |
107 |
public void getImeiActivation(String captchaBreak, OkHttpClient okHttpClient,
|
|
|
108 |
ImeiActivationTimestampModel vivoImeiAndActivationTimeStampModel) throws Exception {
|
| 29451 |
manish |
109 |
|
| 33453 |
amit.gupta |
110 |
String imei = vivoImeiAndActivationTimeStampModel.getSerialNumber();
|
|
|
111 |
RequestBody formBody = new FormBody.Builder().add("imei", imei).add("code", captchaBreak).build();
|
| 29466 |
amit.gupta |
112 |
|
| 33453 |
amit.gupta |
113 |
Request request1 = new Request.Builder().url("https://www.vivo.com/in/support/checkCode").post(formBody)
|
|
|
114 |
.build();
|
| 29466 |
amit.gupta |
115 |
|
| 36253 |
amit |
116 |
JSONObject imeiActivationJson;
|
|
|
117 |
try (Response response2 = okHttpClient.newCall(request1).execute()) {
|
|
|
118 |
imeiActivationJson = new JSONObject(response2.body().string());
|
|
|
119 |
}
|
|
|
120 |
|
| 33453 |
amit.gupta |
121 |
JSONObject data = imeiActivationJson.getJSONObject("data");
|
|
|
122 |
int status = data.getInt("status");
|
|
|
123 |
if (status == 0) {
|
|
|
124 |
LOGGER.info("Found invalid captcha");
|
|
|
125 |
return;
|
|
|
126 |
} else if (status == 2) {
|
|
|
127 |
LOGGER.info("Received status 2 for {}, data {}", imei, data);
|
| 36253 |
amit |
128 |
activatedImeiRepository.saveActivation(imei, null);
|
| 33453 |
amit.gupta |
129 |
return;
|
|
|
130 |
}
|
| 29451 |
manish |
131 |
|
| 33453 |
amit.gupta |
132 |
JSONObject imeiQueryDto = data.getJSONObject("imeiQueryDto");
|
|
|
133 |
LOGGER.info("imeiQueryDto - {}", imeiQueryDto);
|
|
|
134 |
String dueTimeString = imeiQueryDto.getString("dueTime");
|
| 29466 |
amit.gupta |
135 |
|
| 36253 |
amit |
136 |
if (dueTimeString.equals("")) {
|
|
|
137 |
activatedImeiRepository.saveActivation(imei, null);
|
|
|
138 |
return;
|
| 33453 |
amit.gupta |
139 |
}
|
| 29469 |
amit.gupta |
140 |
|
| 33453 |
amit.gupta |
141 |
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
|
|
|
142 |
dueTimeString = dueTimeString.split(" ")[0];
|
|
|
143 |
LocalDate dueDate = LocalDate.parse(dueTimeString, formatter);
|
| 30343 |
amit.gupta |
144 |
|
| 36253 |
amit |
145 |
activatedImeiRepository.saveActivation(imei, dueDate.atStartOfDay().minusYears(1));
|
|
|
146 |
LOGGER.info("activatedImei saved for {} with dueDate {}", imei, dueDate.atStartOfDay());
|
| 33453 |
amit.gupta |
147 |
}
|
| 36253 |
amit |
148 |
}
|