| 29451 |
manish |
1 |
package com.smartdukaan.cron.scheduled;
|
|
|
2 |
|
|
|
3 |
import java.io.IOException;
|
|
|
4 |
import java.nio.file.Files;
|
|
|
5 |
import java.nio.file.Paths;
|
|
|
6 |
import java.time.LocalDate;
|
|
|
7 |
import java.time.LocalDateTime;
|
|
|
8 |
import java.time.format.DateTimeFormatter;
|
|
|
9 |
import java.util.ArrayList;
|
|
|
10 |
import java.util.HashMap;
|
|
|
11 |
import java.util.List;
|
|
|
12 |
import java.util.Map;
|
|
|
13 |
|
|
|
14 |
import org.apache.logging.log4j.LogManager;
|
|
|
15 |
import org.apache.logging.log4j.Logger;
|
|
|
16 |
import org.json.JSONObject;
|
|
|
17 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
18 |
import org.springframework.stereotype.Component;
|
|
|
19 |
import org.springframework.util.StringUtils;
|
|
|
20 |
|
|
|
21 |
import com.smartdukaan.cron.migrations.RunOnceTasks;
|
|
|
22 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
23 |
import com.spice.profitmandi.dao.entity.fofo.ActivatedImei;
|
|
|
24 |
import com.spice.profitmandi.dao.model.VivoImeiAndActivationTimeStampModel;
|
|
|
25 |
import com.spice.profitmandi.dao.repository.fofo.ActivatedImeiRepository;
|
|
|
26 |
import com.spice.profitmandi.service.inventory.InventoryService;
|
|
|
27 |
|
|
|
28 |
import okhttp3.Cookie;
|
|
|
29 |
import okhttp3.CookieJar;
|
|
|
30 |
import okhttp3.FormBody;
|
|
|
31 |
import okhttp3.HttpUrl;
|
|
|
32 |
import okhttp3.OkHttpClient;
|
|
|
33 |
import okhttp3.Request;
|
|
|
34 |
import okhttp3.RequestBody;
|
|
|
35 |
import okhttp3.Response;
|
|
|
36 |
|
|
|
37 |
@Component
|
|
|
38 |
class VivoImeiActivationService {
|
|
|
39 |
|
|
|
40 |
@Autowired
|
|
|
41 |
InventoryService inventoryService;
|
|
|
42 |
|
|
|
43 |
@Autowired
|
|
|
44 |
CaptchaService captchaService;
|
|
|
45 |
|
|
|
46 |
@Autowired
|
|
|
47 |
ActivatedImeiRepository activatedImeiRepository;
|
|
|
48 |
|
|
|
49 |
private static final Logger LOGGER = LogManager.getLogger(RunOnceTasks.class);
|
|
|
50 |
|
|
|
51 |
private final Map<String, List<Cookie>> cookieStore = new HashMap<>();
|
|
|
52 |
|
|
|
53 |
public void checkImeiActivation() throws ProfitMandiBusinessException, IOException, Exception {
|
|
|
54 |
|
|
|
55 |
OkHttpClient okHttpClient = new OkHttpClient.Builder().cookieJar(new CookieJar() {
|
|
|
56 |
@Override
|
|
|
57 |
public void saveFromResponse(HttpUrl httpUrl, List<Cookie> list) {
|
|
|
58 |
LOGGER.info("SAVE fROM - {}", httpUrl.host());
|
|
|
59 |
cookieStore.put(httpUrl.host(), list);
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
@Override
|
|
|
63 |
public List<Cookie> loadForRequest(HttpUrl httpUrl) {
|
|
|
64 |
List<Cookie> cookies = cookieStore.get(httpUrl.host());
|
|
|
65 |
LOGGER.info("load from - {}", httpUrl.host());
|
|
|
66 |
return cookies != null ? cookies : new ArrayList<Cookie>();
|
|
|
67 |
}
|
|
|
68 |
}).build();
|
|
|
69 |
|
|
|
70 |
String captchaBreak = this.getNewCaptcha(okHttpClient);
|
|
|
71 |
|
|
|
72 |
Object status = null;
|
|
|
73 |
LOGGER.info("Captcha code {}", captchaBreak);
|
|
|
74 |
|
|
|
75 |
List<VivoImeiAndActivationTimeStampModel> imeisActivationList =activatedImeiRepository.selectVivoImeiAndActivationTimeStamp();
|
|
|
76 |
|
|
|
77 |
LOGGER.info("imeisActivationList {}", imeisActivationList);
|
|
|
78 |
int count= 0;
|
|
|
79 |
|
|
|
80 |
Map<String, LocalDateTime> imeisActivationMap = new HashMap<>();
|
|
|
81 |
|
|
|
82 |
for(VivoImeiAndActivationTimeStampModel imeisActivation :imeisActivationList)
|
|
|
83 |
{
|
|
|
84 |
count++;
|
|
|
85 |
imeisActivationMap.put(imeisActivation.getSerialNumber(), imeisActivation.getActivationTimestamp());
|
|
|
86 |
|
| 29455 |
manish |
87 |
if(count == 5000 )
|
| 29451 |
manish |
88 |
{
|
|
|
89 |
status = this.getImeiActivation(captchaBreak, okHttpClient,imeisActivationMap);
|
|
|
90 |
|
|
|
91 |
|
|
|
92 |
if (status.equals(0)) {
|
|
|
93 |
this.generate(captchaBreak, okHttpClient,imeisActivationMap);
|
|
|
94 |
}
|
| 29455 |
manish |
95 |
count= 5005;
|
| 29451 |
manish |
96 |
|
|
|
97 |
}
|
|
|
98 |
|
| 29455 |
manish |
99 |
if(count==5005)
|
|
|
100 |
{
|
|
|
101 |
break;
|
|
|
102 |
|
|
|
103 |
}
|
| 29451 |
manish |
104 |
}
|
|
|
105 |
|
|
|
106 |
|
|
|
107 |
|
|
|
108 |
|
|
|
109 |
|
|
|
110 |
|
|
|
111 |
|
|
|
112 |
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
// assign uppercase of ch1, ch2 to ch3, ch4
|
|
|
116 |
|
|
|
117 |
|
|
|
118 |
|
|
|
119 |
// Get Captcha file
|
|
|
120 |
|
|
|
121 |
// Call Api to break captcha
|
|
|
122 |
// Iterate and fetch activation information using captcha code.
|
|
|
123 |
|
|
|
124 |
// inventoryService.addActivatedImeis(imeisActivationMap );
|
|
|
125 |
|
|
|
126 |
public void generate(String input, OkHttpClient okHttpClient,Map<String, LocalDateTime> imeisActivationMap) {
|
|
|
127 |
int n = input.length();
|
|
|
128 |
int max = 1 << n;
|
|
|
129 |
input = input.toLowerCase();
|
|
|
130 |
int statusCount = 0;
|
|
|
131 |
|
|
|
132 |
Object statusUpper = null;
|
|
|
133 |
char combination[] = null;
|
|
|
134 |
for (int i = 0; i < max; i++) {
|
|
|
135 |
combination = input.toCharArray();
|
|
|
136 |
for (int j = 0; j < n; j++) {
|
|
|
137 |
if (((i >> j) & 1) == 1)
|
|
|
138 |
combination[j] = (char) (combination[j] - 32);
|
|
|
139 |
}
|
|
|
140 |
LOGGER.info("combination" + String.valueOf(combination));
|
|
|
141 |
try {
|
|
|
142 |
statusUpper = this.getImeiActivation(String.valueOf(combination), okHttpClient,imeisActivationMap);
|
|
|
143 |
} catch (IOException e) {
|
|
|
144 |
// TODO Auto-generated catch block
|
|
|
145 |
e.printStackTrace();
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
if (!statusUpper.equals(0)) {
|
|
|
149 |
statusCount = 1;
|
|
|
150 |
break;
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
|
|
|
156 |
if(statusCount == 0 )
|
|
|
157 |
{
|
|
|
158 |
|
|
|
159 |
try {
|
|
|
160 |
String freshCaptcha = this.getNewCaptcha(okHttpClient);
|
|
|
161 |
|
|
|
162 |
this.generate(freshCaptcha, okHttpClient, imeisActivationMap);
|
|
|
163 |
} catch (Exception e) {
|
|
|
164 |
// TODO Auto-generated catch block
|
|
|
165 |
e.printStackTrace();
|
|
|
166 |
}
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
LOGGER.info("statusUpper" + statusUpper);
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
|
|
|
173 |
public String getNewCaptcha(OkHttpClient okHttpClient) throws Exception
|
|
|
174 |
|
|
|
175 |
{
|
|
|
176 |
|
|
|
177 |
|
|
|
178 |
LOGGER.info("okHttpClient" + okHttpClient);
|
|
|
179 |
|
|
|
180 |
HttpUrl vivoSupportUrl = HttpUrl.parse("https://www.vivo.com/in/support/generatingCodes");
|
|
|
181 |
Request request = new Request.Builder().url(vivoSupportUrl).build();
|
|
|
182 |
|
|
|
183 |
Response response = okHttpClient.newCall(request).execute();
|
|
|
184 |
LOGGER.info("cookie {}", this.cookieStore.values());
|
|
|
185 |
|
|
|
186 |
LOGGER.info("vivoSupportUrl" + vivoSupportUrl);
|
|
|
187 |
LOGGER.info("request" + request);
|
|
|
188 |
LOGGER.info("request" + request);
|
|
|
189 |
|
|
|
190 |
String filePath = "/tmp/captcha.avif";
|
|
|
191 |
Files.write(Paths.get(filePath), response.body().bytes());
|
|
|
192 |
|
|
|
193 |
// Get all relevent imes from activatedImei and inventoryItem for vivo
|
|
|
194 |
|
|
|
195 |
|
|
|
196 |
String captchaBreak = captchaService.getCaptchaCode(filePath);
|
|
|
197 |
|
|
|
198 |
|
|
|
199 |
return captchaBreak;
|
|
|
200 |
}
|
|
|
201 |
|
|
|
202 |
|
|
|
203 |
public Object getImeiActivation(String captchaBreak, OkHttpClient okHttpClient ,Map<String, LocalDateTime> imeisActivationMap ) throws IOException {
|
|
|
204 |
|
|
|
205 |
|
|
|
206 |
Object status = 0;
|
|
|
207 |
|
|
|
208 |
for (Map.Entry<String,LocalDateTime> imei : imeisActivationMap.entrySet()) {
|
|
|
209 |
|
|
|
210 |
LOGGER.info("imei" + imei);
|
|
|
211 |
RequestBody formBody = new FormBody.Builder()
|
|
|
212 |
.add("imei", imei.getKey())
|
|
|
213 |
.add("code", captchaBreak)
|
|
|
214 |
.build();
|
|
|
215 |
|
|
|
216 |
Request request1 = new Request.Builder().url("https://www.vivo.com/in/support/checkCode").post(formBody)
|
|
|
217 |
.build();
|
|
|
218 |
|
|
|
219 |
Response response2 = okHttpClient.newCall(request1).execute();
|
|
|
220 |
JSONObject imeiActivationJson = new JSONObject(response2.body().string());
|
|
|
221 |
LOGGER.info("imeiActivationJson" + imeiActivationJson);
|
|
|
222 |
JSONObject data = imeiActivationJson.getJSONObject("data");
|
|
|
223 |
|
|
|
224 |
JSONObject imeiQueryDto =data.getJSONObject("imeiQueryDto");
|
|
|
225 |
|
|
|
226 |
Object dueTimeObject = imeiQueryDto.get("dueTime");
|
|
|
227 |
String dueTimeString = dueTimeObject.toString();
|
|
|
228 |
|
|
|
229 |
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
|
|
|
230 |
String dueTimeSplit = dueTimeString.split(" ")[0];
|
|
|
231 |
|
|
|
232 |
status = data.get("status");
|
|
|
233 |
|
|
|
234 |
if(!StringUtils.isEmpty(dueTimeSplit))
|
|
|
235 |
{
|
|
|
236 |
|
|
|
237 |
LocalDate dateTime = LocalDate.parse(dueTimeSplit, formatter);
|
|
|
238 |
|
|
|
239 |
LOGGER.info("data" + data);
|
|
|
240 |
|
|
|
241 |
LOGGER.info("status" + status);
|
|
|
242 |
|
|
|
243 |
|
|
|
244 |
ActivatedImei activatedImei= activatedImeiRepository.selectBySerialNumber(imei.getKey());
|
|
|
245 |
|
|
|
246 |
if(activatedImei == null)
|
|
|
247 |
|
|
|
248 |
{
|
|
|
249 |
activatedImei = new ActivatedImei();
|
|
|
250 |
activatedImei.setActivationTimestamp(dateTime.atStartOfDay().minusYears(1));
|
|
|
251 |
activatedImei.setSerialNumber(imei.getKey());
|
|
|
252 |
activatedImei.setCreateTimestamp(LocalDateTime.now());
|
|
|
253 |
activatedImeiRepository.persist(activatedImei);
|
|
|
254 |
|
|
|
255 |
}
|
|
|
256 |
else {
|
|
|
257 |
activatedImei.setActivationTimestamp(dateTime.atStartOfDay().minusYears(1));
|
|
|
258 |
|
|
|
259 |
}
|
|
|
260 |
|
|
|
261 |
LOGGER.info("activatedImei" + activatedImei);
|
|
|
262 |
|
|
|
263 |
LOGGER.info("vivoImeiStatusModel" + dateTime.atStartOfDay());
|
|
|
264 |
|
|
|
265 |
|
|
|
266 |
}
|
|
|
267 |
|
|
|
268 |
|
|
|
269 |
|
|
|
270 |
if (status.equals(0)) {
|
|
|
271 |
|
|
|
272 |
return status;
|
|
|
273 |
}
|
|
|
274 |
}
|
|
|
275 |
|
|
|
276 |
return status;
|
|
|
277 |
}
|
|
|
278 |
}
|