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