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