| 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
|
| 36580 |
amit |
52 |
.selectImeiActivationPendingByBrand("Vivo", 4);
|
| 36253 |
amit |
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);
|
| 37393 |
amit |
82 |
if (captchaBreak == null) {
|
|
|
83 |
// Solver returned something that is not a captcha code (it was down
|
|
|
84 |
// for 46 days returning an nginx 502 page, which used to be submitted
|
|
|
85 |
// to Vivo verbatim). Skip; this IMEI is picked up on the next run.
|
|
|
86 |
continue;
|
|
|
87 |
}
|
| 36420 |
amit |
88 |
this.getImeiActivation(captchaBreak, httpClient, imeiActivationTimestampModel);
|
| 33453 |
amit.gupta |
89 |
}
|
| 36253 |
amit |
90 |
}
|
| 33453 |
amit.gupta |
91 |
}
|
| 29451 |
manish |
92 |
|
| 36420 |
amit |
93 |
public String getNewCaptcha(CloseableHttpClient httpClient, BasicCookieStore cookieStore) throws Exception {
|
|
|
94 |
String vivoSupportUrl = "https://www.vivo.com/in/support/generatingCodes";
|
|
|
95 |
HttpGet request = new HttpGet(vivoSupportUrl);
|
| 29451 |
manish |
96 |
|
| 33453 |
amit.gupta |
97 |
String filePath = "/tmp/captcha.jpg";
|
| 36420 |
amit |
98 |
try (CloseableHttpResponse response = httpClient.execute(request)) {
|
|
|
99 |
LOGGER.info("cookie {}", cookieStore.getCookies());
|
| 36253 |
amit |
100 |
LOGGER.info("vivoSupportUrl" + vivoSupportUrl);
|
| 36420 |
amit |
101 |
byte[] bytes = EntityUtils.toByteArray(response.getEntity());
|
|
|
102 |
Files.write(Paths.get(filePath), bytes);
|
| 36253 |
amit |
103 |
}
|
| 29451 |
manish |
104 |
|
| 33453 |
amit.gupta |
105 |
String captchaBreak = captchaService.getCaptchaCode(filePath);
|
|
|
106 |
System.out.println("Captcha is " + captchaBreak);
|
|
|
107 |
return captchaBreak;
|
|
|
108 |
}
|
| 29451 |
manish |
109 |
|
| 36420 |
amit |
110 |
public void getImeiActivation(String captchaBreak, CloseableHttpClient httpClient,
|
| 33453 |
amit.gupta |
111 |
ImeiActivationTimestampModel vivoImeiAndActivationTimeStampModel) throws Exception {
|
| 29451 |
manish |
112 |
|
| 33453 |
amit.gupta |
113 |
String imei = vivoImeiAndActivationTimeStampModel.getSerialNumber();
|
| 29466 |
amit.gupta |
114 |
|
| 36420 |
amit |
115 |
List<NameValuePair> form = new ArrayList<>();
|
|
|
116 |
form.add(new BasicNameValuePair("imei", imei));
|
|
|
117 |
form.add(new BasicNameValuePair("code", captchaBreak));
|
|
|
118 |
HttpPost post = new HttpPost("https://www.vivo.com/in/support/checkCode");
|
|
|
119 |
post.setEntity(new UrlEncodedFormEntity(form, StandardCharsets.UTF_8));
|
| 29466 |
amit.gupta |
120 |
|
| 36253 |
amit |
121 |
JSONObject imeiActivationJson;
|
| 36420 |
amit |
122 |
try (CloseableHttpResponse response2 = httpClient.execute(post)) {
|
|
|
123 |
imeiActivationJson = new JSONObject(EntityUtils.toString(response2.getEntity()));
|
| 36253 |
amit |
124 |
}
|
|
|
125 |
|
| 33453 |
amit.gupta |
126 |
JSONObject data = imeiActivationJson.getJSONObject("data");
|
|
|
127 |
int status = data.getInt("status");
|
|
|
128 |
if (status == 0) {
|
|
|
129 |
LOGGER.info("Found invalid captcha");
|
|
|
130 |
return;
|
|
|
131 |
} else if (status == 2) {
|
|
|
132 |
LOGGER.info("Received status 2 for {}, data {}", imei, data);
|
| 36253 |
amit |
133 |
activatedImeiRepository.saveActivation(imei, null);
|
| 33453 |
amit.gupta |
134 |
return;
|
|
|
135 |
}
|
| 29451 |
manish |
136 |
|
| 33453 |
amit.gupta |
137 |
JSONObject imeiQueryDto = data.getJSONObject("imeiQueryDto");
|
|
|
138 |
LOGGER.info("imeiQueryDto - {}", imeiQueryDto);
|
|
|
139 |
String dueTimeString = imeiQueryDto.getString("dueTime");
|
| 29466 |
amit.gupta |
140 |
|
| 36253 |
amit |
141 |
if (dueTimeString.equals("")) {
|
|
|
142 |
activatedImeiRepository.saveActivation(imei, null);
|
|
|
143 |
return;
|
| 33453 |
amit.gupta |
144 |
}
|
| 29469 |
amit.gupta |
145 |
|
| 33453 |
amit.gupta |
146 |
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
|
|
|
147 |
dueTimeString = dueTimeString.split(" ")[0];
|
|
|
148 |
LocalDate dueDate = LocalDate.parse(dueTimeString, formatter);
|
| 30343 |
amit.gupta |
149 |
|
| 36253 |
amit |
150 |
activatedImeiRepository.saveActivation(imei, dueDate.atStartOfDay().minusYears(1));
|
|
|
151 |
LOGGER.info("activatedImei saved for {} with dueDate {}", imei, dueDate.atStartOfDay());
|
| 33453 |
amit.gupta |
152 |
}
|
| 36253 |
amit |
153 |
}
|