| 29451 |
manish |
1 |
package com.smartdukaan.cron.scheduled;
|
|
|
2 |
|
| 36420 |
amit |
3 |
import com.spice.profitmandi.common.web.client.HttpClientFactory;
|
| 30209 |
amit.gupta |
4 |
import com.spice.profitmandi.dao.model.ImeiActivationTimestampModel;
|
|
|
5 |
import com.spice.profitmandi.dao.repository.fofo.ActivatedImeiRepository;
|
|
|
6 |
import com.spice.profitmandi.service.inventory.InventoryService;
|
| 36420 |
amit |
7 |
import org.apache.http.NameValuePair;
|
|
|
8 |
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
|
9 |
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
10 |
import org.apache.http.client.methods.HttpGet;
|
|
|
11 |
import org.apache.http.client.methods.HttpPost;
|
|
|
12 |
import org.apache.http.impl.client.BasicCookieStore;
|
|
|
13 |
import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
14 |
import org.apache.http.message.BasicNameValuePair;
|
|
|
15 |
import org.apache.http.util.EntityUtils;
|
| 29451 |
manish |
16 |
import org.apache.logging.log4j.LogManager;
|
|
|
17 |
import org.apache.logging.log4j.Logger;
|
|
|
18 |
import org.json.JSONObject;
|
|
|
19 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
20 |
import org.springframework.stereotype.Component;
|
| 36260 |
amit |
21 |
import org.springframework.transaction.annotation.Propagation;
|
|
|
22 |
import org.springframework.transaction.annotation.Transactional;
|
| 29451 |
manish |
23 |
|
| 36420 |
amit |
24 |
import java.io.IOException;
|
|
|
25 |
import java.nio.charset.StandardCharsets;
|
| 30209 |
amit.gupta |
26 |
import java.nio.file.Files;
|
|
|
27 |
import java.nio.file.Paths;
|
|
|
28 |
import java.time.LocalDate;
|
|
|
29 |
import java.time.format.DateTimeFormatter;
|
| 30335 |
amit.gupta |
30 |
import java.util.ArrayList;
|
|
|
31 |
import java.util.List;
|
| 29451 |
manish |
32 |
|
|
|
33 |
@Component
|
|
|
34 |
class VivoImeiActivationService {
|
|
|
35 |
|
| 33453 |
amit.gupta |
36 |
@Autowired
|
|
|
37 |
InventoryService inventoryService;
|
| 29451 |
manish |
38 |
|
| 33453 |
amit.gupta |
39 |
@Autowired
|
|
|
40 |
CaptchaService captchaService;
|
| 29466 |
amit.gupta |
41 |
|
| 33453 |
amit.gupta |
42 |
@Autowired
|
|
|
43 |
ActivatedImeiRepository activatedImeiRepository;
|
| 29451 |
manish |
44 |
|
| 33453 |
amit.gupta |
45 |
private static final Logger LOGGER = LogManager.getLogger(VivoImeiActivationService.class);
|
| 29451 |
manish |
46 |
|
| 36260 |
amit |
47 |
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
| 33453 |
amit.gupta |
48 |
public void checkImeiActivation() throws Exception {
|
|
|
49 |
if (LocalDate.now().getDayOfMonth() == 1) return;
|
| 36253 |
amit |
50 |
|
|
|
51 |
List<ImeiActivationTimestampModel> imeisActivationList = activatedImeiRepository
|
|
|
52 |
.selectImeiActivationPendingByBrand("Vivo", 2);
|
|
|
53 |
LOGGER.info("Secondary imeisActivationList = {}, size = {}", imeisActivationList, imeisActivationList.size());
|
|
|
54 |
|
|
|
55 |
processImeis(imeisActivationList);
|
|
|
56 |
}
|
|
|
57 |
|
| 36260 |
amit |
58 |
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
| 36253 |
amit |
59 |
public void checkImeiActivationTertiary() throws Exception {
|
|
|
60 |
if (LocalDate.now().getDayOfMonth() == 1) return;
|
|
|
61 |
|
|
|
62 |
List<ImeiActivationTimestampModel> imeisActivationList = activatedImeiRepository
|
|
|
63 |
.selectImeiActivationPendingByBrandTertiary("Vivo", 2);
|
|
|
64 |
LOGGER.info("Tertiary imeisActivationList = {}, size = {}", imeisActivationList, imeisActivationList.size());
|
|
|
65 |
|
|
|
66 |
processImeis(imeisActivationList);
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
private void processImeis(List<ImeiActivationTimestampModel> imeisActivationList) throws Exception {
|
|
|
70 |
if (imeisActivationList.isEmpty()) return;
|
|
|
71 |
|
| 36420 |
amit |
72 |
BasicCookieStore cookieStore = new BasicCookieStore();
|
|
|
73 |
try (CloseableHttpClient httpClient = HttpClientFactory.apacheHttpWithCookies(cookieStore)) {
|
|
|
74 |
// Seed the session cookies
|
|
|
75 |
HttpGet seed = new HttpGet("https://www.vivo.com/in/support/IMEI");
|
|
|
76 |
try (CloseableHttpResponse seedResponse = httpClient.execute(seed)) {
|
|
|
77 |
EntityUtils.consumeQuietly(seedResponse.getEntity());
|
| 33453 |
amit.gupta |
78 |
}
|
| 29466 |
amit.gupta |
79 |
|
| 36420 |
amit |
80 |
for (ImeiActivationTimestampModel imeiActivationTimestampModel : imeisActivationList) {
|
|
|
81 |
String captchaBreak = this.getNewCaptcha(httpClient, cookieStore);
|
|
|
82 |
this.getImeiActivation(captchaBreak, httpClient, imeiActivationTimestampModel);
|
| 33453 |
amit.gupta |
83 |
}
|
| 36253 |
amit |
84 |
}
|
| 33453 |
amit.gupta |
85 |
}
|
| 29451 |
manish |
86 |
|
| 36420 |
amit |
87 |
public String getNewCaptcha(CloseableHttpClient httpClient, BasicCookieStore cookieStore) throws Exception {
|
|
|
88 |
String vivoSupportUrl = "https://www.vivo.com/in/support/generatingCodes";
|
|
|
89 |
HttpGet request = new HttpGet(vivoSupportUrl);
|
| 29451 |
manish |
90 |
|
| 33453 |
amit.gupta |
91 |
String filePath = "/tmp/captcha.jpg";
|
| 36420 |
amit |
92 |
try (CloseableHttpResponse response = httpClient.execute(request)) {
|
|
|
93 |
LOGGER.info("cookie {}", cookieStore.getCookies());
|
| 36253 |
amit |
94 |
LOGGER.info("vivoSupportUrl" + vivoSupportUrl);
|
| 36420 |
amit |
95 |
byte[] bytes = EntityUtils.toByteArray(response.getEntity());
|
|
|
96 |
Files.write(Paths.get(filePath), bytes);
|
| 36253 |
amit |
97 |
}
|
| 29451 |
manish |
98 |
|
| 33453 |
amit.gupta |
99 |
String captchaBreak = captchaService.getCaptchaCode(filePath);
|
|
|
100 |
System.out.println("Captcha is " + captchaBreak);
|
|
|
101 |
return captchaBreak;
|
|
|
102 |
}
|
| 29451 |
manish |
103 |
|
| 36420 |
amit |
104 |
public void getImeiActivation(String captchaBreak, CloseableHttpClient httpClient,
|
| 33453 |
amit.gupta |
105 |
ImeiActivationTimestampModel vivoImeiAndActivationTimeStampModel) throws Exception {
|
| 29451 |
manish |
106 |
|
| 33453 |
amit.gupta |
107 |
String imei = vivoImeiAndActivationTimeStampModel.getSerialNumber();
|
| 29466 |
amit.gupta |
108 |
|
| 36420 |
amit |
109 |
List<NameValuePair> form = new ArrayList<>();
|
|
|
110 |
form.add(new BasicNameValuePair("imei", imei));
|
|
|
111 |
form.add(new BasicNameValuePair("code", captchaBreak));
|
|
|
112 |
HttpPost post = new HttpPost("https://www.vivo.com/in/support/checkCode");
|
|
|
113 |
post.setEntity(new UrlEncodedFormEntity(form, StandardCharsets.UTF_8));
|
| 29466 |
amit.gupta |
114 |
|
| 36253 |
amit |
115 |
JSONObject imeiActivationJson;
|
| 36420 |
amit |
116 |
try (CloseableHttpResponse response2 = httpClient.execute(post)) {
|
|
|
117 |
imeiActivationJson = new JSONObject(EntityUtils.toString(response2.getEntity()));
|
| 36253 |
amit |
118 |
}
|
|
|
119 |
|
| 33453 |
amit.gupta |
120 |
JSONObject data = imeiActivationJson.getJSONObject("data");
|
|
|
121 |
int status = data.getInt("status");
|
|
|
122 |
if (status == 0) {
|
|
|
123 |
LOGGER.info("Found invalid captcha");
|
|
|
124 |
return;
|
|
|
125 |
} else if (status == 2) {
|
|
|
126 |
LOGGER.info("Received status 2 for {}, data {}", imei, data);
|
| 36253 |
amit |
127 |
activatedImeiRepository.saveActivation(imei, null);
|
| 33453 |
amit.gupta |
128 |
return;
|
|
|
129 |
}
|
| 29451 |
manish |
130 |
|
| 33453 |
amit.gupta |
131 |
JSONObject imeiQueryDto = data.getJSONObject("imeiQueryDto");
|
|
|
132 |
LOGGER.info("imeiQueryDto - {}", imeiQueryDto);
|
|
|
133 |
String dueTimeString = imeiQueryDto.getString("dueTime");
|
| 29466 |
amit.gupta |
134 |
|
| 36253 |
amit |
135 |
if (dueTimeString.equals("")) {
|
|
|
136 |
activatedImeiRepository.saveActivation(imei, null);
|
|
|
137 |
return;
|
| 33453 |
amit.gupta |
138 |
}
|
| 29469 |
amit.gupta |
139 |
|
| 33453 |
amit.gupta |
140 |
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
|
|
|
141 |
dueTimeString = dueTimeString.split(" ")[0];
|
|
|
142 |
LocalDate dueDate = LocalDate.parse(dueTimeString, formatter);
|
| 30343 |
amit.gupta |
143 |
|
| 36253 |
amit |
144 |
activatedImeiRepository.saveActivation(imei, dueDate.atStartOfDay().minusYears(1));
|
|
|
145 |
LOGGER.info("activatedImei saved for {} with dueDate {}", imei, dueDate.atStartOfDay());
|
| 33453 |
amit.gupta |
146 |
}
|
| 36253 |
amit |
147 |
}
|