| Line 24... |
Line 24... |
| 24 |
import java.util.Map;
|
24 |
import java.util.Map;
|
| 25 |
|
25 |
|
| 26 |
@Component
|
26 |
@Component
|
| 27 |
class VivoImeiActivationService {
|
27 |
class VivoImeiActivationService {
|
| 28 |
|
28 |
|
| 29 |
@Autowired
|
29 |
@Autowired
|
| 30 |
InventoryService inventoryService;
|
30 |
InventoryService inventoryService;
|
| 31 |
|
31 |
|
| 32 |
@Autowired
|
32 |
@Autowired
|
| 33 |
CaptchaService captchaService;
|
33 |
CaptchaService captchaService;
|
| 34 |
|
34 |
|
| 35 |
@Autowired
|
35 |
@Autowired
|
| 36 |
ActivatedImeiRepository activatedImeiRepository;
|
36 |
ActivatedImeiRepository activatedImeiRepository;
|
| 37 |
|
37 |
|
| 38 |
private static final Logger LOGGER = LogManager.getLogger(VivoImeiActivationService.class);
|
38 |
private static final Logger LOGGER = LogManager.getLogger(VivoImeiActivationService.class);
|
| 39 |
|
39 |
|
| 40 |
private final Map<String, List<Cookie>> cookieStore = new HashMap<>();
|
40 |
private final Map<String, List<Cookie>> cookieStore = new HashMap<>();
|
| 41 |
|
41 |
|
| 42 |
public void checkImeiActivation() throws ProfitMandiBusinessException, IOException, Exception {
|
42 |
public void checkImeiActivation() throws Exception {
|
| - |
|
43 |
if (LocalDate.now().getDayOfMonth() == 1) return;
|
| - |
|
44 |
OkHttpClient okHttpClient = new OkHttpClient.Builder().cookieJar(new CookieJar() {
|
| - |
|
45 |
@Override
|
| - |
|
46 |
public void saveFromResponse(HttpUrl httpUrl, List<Cookie> list) {
|
| - |
|
47 |
LOGGER.info("SAVE fROM - {}", httpUrl.host());
|
| - |
|
48 |
cookieStore.put(httpUrl.host(), list);
|
| - |
|
49 |
}
|
| 43 |
|
50 |
|
| 44 |
OkHttpClient okHttpClient = new OkHttpClient.Builder().cookieJar(new CookieJar() {
|
- |
|
| 45 |
@Override
|
51 |
@Override
|
| 46 |
public void saveFromResponse(HttpUrl httpUrl, List<Cookie> list) {
|
52 |
public List<Cookie> loadForRequest(HttpUrl httpUrl) {
|
| - |
|
53 |
List<Cookie> cookies = cookieStore.get(httpUrl.host());
|
| 47 |
LOGGER.info("SAVE fROM - {}", httpUrl.host());
|
54 |
LOGGER.info("load from - {}", httpUrl.host());
|
| 48 |
cookieStore.put(httpUrl.host(), list);
|
55 |
return cookies != null ? cookies : new ArrayList<>();
|
| 49 |
}
|
56 |
}
|
| - |
|
57 |
}).build();
|
| 50 |
|
58 |
|
| 51 |
@Override
|
- |
|
| 52 |
public List<Cookie> loadForRequest(HttpUrl httpUrl) {
|
- |
|
| 53 |
List<Cookie> cookies = cookieStore.get(httpUrl.host());
|
- |
|
| 54 |
LOGGER.info("load from - {}", httpUrl.host());
|
- |
|
| 55 |
return cookies != null ? cookies : new ArrayList<>();
|
- |
|
| 56 |
}
|
- |
|
| 57 |
}).build();
|
- |
|
| 58 |
|
59 |
|
| - |
|
60 |
Request request = new Request.Builder().url("https://www.vivo.com/in/support/IMEI").build();
|
| - |
|
61 |
okHttpClient.newCall(request).execute();
|
| 59 |
|
62 |
|
| 60 |
Request request = new Request.Builder().url("https://www.vivo.com/in/support/IMEI").build();
|
- |
|
| 61 |
okHttpClient.newCall(request).execute();
|
- |
|
| 62 |
|
63 |
|
| - |
|
64 |
List<ImeiActivationTimestampModel> imeisActivationList = activatedImeiRepository
|
| - |
|
65 |
.selectImeiActivationPendingByBrand("Vivo", 2);
|
| - |
|
66 |
imeisActivationList = imeisActivationList.size() > 20 ? imeisActivationList.subList(0, 20) : imeisActivationList;
|
| - |
|
67 |
LOGGER.info("imeisActivationList = {}", imeisActivationList);
|
| 63 |
|
68 |
|
| 64 |
List<ImeiActivationTimestampModel> imeisActivationList = activatedImeiRepository
|
69 |
LOGGER.info("Complete list size is - {}", imeisActivationList.size());
|
| 65 |
.selectImeiActivationPendingByBrand("Vivo", 2);
|
70 |
for (ImeiActivationTimestampModel imeiActivationTimestampModel : imeisActivationList) {
|
| 66 |
imeisActivationList = imeisActivationList.size() > 20 ? imeisActivationList.subList(0, 20) : imeisActivationList;
|
71 |
String captchaBreak = this.getNewCaptcha(okHttpClient);
|
| 67 |
LOGGER.info("imeisActivationList = {}", imeisActivationList);
|
72 |
this.getImeiActivation(captchaBreak, okHttpClient, imeiActivationTimestampModel);
|
| - |
|
73 |
}
|
| 68 |
|
74 |
|
| 69 |
LOGGER.info("Complete list size is - {}", imeisActivationList.size());
|
- |
|
| 70 |
for (ImeiActivationTimestampModel imeiActivationTimestampModel : imeisActivationList) {
|
- |
|
| 71 |
String captchaBreak = this.getNewCaptcha(okHttpClient);
|
- |
|
| 72 |
this.getImeiActivation(captchaBreak, okHttpClient, imeiActivationTimestampModel);
|
- |
|
| 73 |
}
|
75 |
}
|
| 74 |
|
76 |
|
| 75 |
}
|
- |
|
| - |
|
77 |
public String getNewCaptcha(OkHttpClient okHttpClient) throws Exception {
|
| 76 |
|
78 |
|
| 77 |
public String getNewCaptcha(OkHttpClient okHttpClient) throws Exception {
|
79 |
LOGGER.info("okHttpClient" + okHttpClient);
|
| 78 |
|
80 |
|
| - |
|
81 |
HttpUrl vivoSupportUrl = HttpUrl.parse("https://www.vivo.com/in/support/generatingCodes");
|
| 79 |
LOGGER.info("okHttpClient" + okHttpClient);
|
82 |
Request request = new Request.Builder().url(vivoSupportUrl).build();
|
| 80 |
|
83 |
|
| 81 |
HttpUrl vivoSupportUrl = HttpUrl.parse("https://www.vivo.com/in/support/generatingCodes");
|
84 |
Response response = okHttpClient.newCall(request).execute();
|
| 82 |
Request request = new Request.Builder().url(vivoSupportUrl).build();
|
85 |
LOGGER.info("cookie {}", this.cookieStore.values());
|
| 83 |
|
86 |
|
| 84 |
Response response = okHttpClient.newCall(request).execute();
|
- |
|
| 85 |
LOGGER.info("cookie {}", this.cookieStore.values());
|
87 |
LOGGER.info("vivoSupportUrl" + vivoSupportUrl);
|
| 86 |
|
88 |
|
| 87 |
LOGGER.info("vivoSupportUrl" + vivoSupportUrl);
|
89 |
String filePath = "/tmp/captcha.jpg";
|
| - |
|
90 |
Files.write(Paths.get(filePath), response.body().bytes());
|
| 88 |
|
91 |
|
| 89 |
String filePath = "/tmp/captcha.jpg";
|
- |
|
| 90 |
Files.write(Paths.get(filePath), response.body().bytes());
|
92 |
// Get all relevent imes from activatedImei and inventoryItem for vivo
|
| 91 |
|
93 |
|
| 92 |
// Get all relevent imes from activatedImei and inventoryItem for vivo
|
94 |
String captchaBreak = captchaService.getCaptchaCode(filePath);
|
| - |
|
95 |
System.out.println("Captcha is " + captchaBreak);
|
| - |
|
96 |
return captchaBreak;
|
| - |
|
97 |
}
|
| 93 |
|
98 |
|
| 94 |
String captchaBreak = captchaService.getCaptchaCode(filePath);
|
99 |
public void getImeiActivation(String captchaBreak, OkHttpClient okHttpClient,
|
| 95 |
System.out.println("Captcha is " + captchaBreak);
|
100 |
ImeiActivationTimestampModel vivoImeiAndActivationTimeStampModel) throws Exception {
|
| 96 |
return captchaBreak;
|
- |
|
| 97 |
}
|
- |
|
| 98 |
|
101 |
|
| 99 |
public void getImeiActivation(String captchaBreak, OkHttpClient okHttpClient,
|
102 |
String imei = vivoImeiAndActivationTimeStampModel.getSerialNumber();
|
| 100 |
ImeiActivationTimestampModel vivoImeiAndActivationTimeStampModel) throws Exception {
|
103 |
RequestBody formBody = new FormBody.Builder().add("imei", imei).add("code", captchaBreak).build();
|
| 101 |
|
104 |
|
| 102 |
String imei = vivoImeiAndActivationTimeStampModel.getSerialNumber();
|
- |
|
| 103 |
RequestBody formBody = new FormBody.Builder().add("imei", imei).add("code", captchaBreak).build();
|
105 |
Request request1 = new Request.Builder().url("https://www.vivo.com/in/support/checkCode").post(formBody)
|
| - |
|
106 |
.build();
|
| 104 |
|
107 |
|
| - |
|
108 |
Response response2 = okHttpClient.newCall(request1).execute();
|
| 105 |
Request request1 = new Request.Builder().url("https://www.vivo.com/in/support/checkCode").post(formBody)
|
109 |
JSONObject imeiActivationJson = new JSONObject(response2.body().string());
|
| - |
|
110 |
JSONObject data = imeiActivationJson.getJSONObject("data");
|
| - |
|
111 |
int status = data.getInt("status");
|
| - |
|
112 |
if (status == 0) {
|
| - |
|
113 |
LOGGER.info("Found invalid captcha");
|
| - |
|
114 |
return;
|
| - |
|
115 |
} else if (status == 2) {
|
| - |
|
116 |
LOGGER.info("Received status 2 for {}, data {}", imei, data);
|
| - |
|
117 |
return;
|
| 106 |
.build();
|
118 |
}
|
| 107 |
|
119 |
|
| 108 |
Response response2 = okHttpClient.newCall(request1).execute();
|
- |
|
| 109 |
JSONObject imeiActivationJson = new JSONObject(response2.body().string());
|
- |
|
| 110 |
JSONObject data = imeiActivationJson.getJSONObject("data");
|
120 |
JSONObject imeiQueryDto = data.getJSONObject("imeiQueryDto");
|
| 111 |
int status = data.getInt("status");
|
- |
|
| 112 |
if (status == 0) {
|
- |
|
| 113 |
LOGGER.info("Found invalid captcha");
|
121 |
LOGGER.info("imeiQueryDto - {}", imeiQueryDto);
|
| 114 |
return;
|
- |
|
| 115 |
} else if (status == 2) {
|
- |
|
| 116 |
LOGGER.info("Received status 2 for {}, data {}", imei, data);
|
122 |
String dueTimeString = imeiQueryDto.getString("dueTime");
|
| 117 |
return;
|
- |
|
| 118 |
}
|
- |
|
| 119 |
|
123 |
|
| 120 |
JSONObject imeiQueryDto = data.getJSONObject("imeiQueryDto");
|
124 |
ActivatedImei activatedImei = activatedImeiRepository.selectBySerialNumber(imei);
|
| - |
|
125 |
if (activatedImei == null) {
|
| 121 |
LOGGER.info("imeiQueryDto - {}", imeiQueryDto);
|
126 |
LOGGER.info("Adding activation for - {}", imei);
|
| - |
|
127 |
activatedImei = new ActivatedImei();
|
| 122 |
String dueTimeString = imeiQueryDto.getString("dueTime");
|
128 |
activatedImei.setSerialNumber(imei);
|
| - |
|
129 |
activatedImei.setCreateTimestamp(LocalDateTime.now());
|
| - |
|
130 |
activatedImeiRepository.persist(activatedImei);
|
| - |
|
131 |
}
|
| - |
|
132 |
activatedImei.setCreateTimestamp(LocalDateTime.now());
|
| - |
|
133 |
if (dueTimeString.equals("")) return;
|
| 123 |
|
134 |
|
| 124 |
ActivatedImei activatedImei = activatedImeiRepository.selectBySerialNumber(imei);
|
135 |
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
|
| 125 |
if (activatedImei == null) {
|
- |
|
| 126 |
LOGGER.info("Adding activation for - {}", imei);
|
- |
|
| 127 |
activatedImei = new ActivatedImei();
|
- |
|
| 128 |
activatedImei.setSerialNumber(imei);
|
- |
|
| 129 |
activatedImei.setCreateTimestamp(LocalDateTime.now());
|
136 |
dueTimeString = dueTimeString.split(" ")[0];
|
| 130 |
activatedImeiRepository.persist(activatedImei);
|
137 |
LocalDate dueDate = LocalDate.parse(dueTimeString, formatter);
|
| 131 |
}
|
- |
|
| 132 |
activatedImei.setCreateTimestamp(LocalDateTime.now());
|
138 |
activatedImei.setActivationTimestamp(dueDate.atStartOfDay().minusYears(1));
|
| 133 |
if (dueTimeString.equals("")) return;
|
- |
|
| 134 |
|
139 |
|
| 135 |
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
|
- |
|
| 136 |
dueTimeString = dueTimeString.split(" ")[0];
|
- |
|
| 137 |
LocalDate dueDate = LocalDate.parse(dueTimeString, formatter);
|
140 |
LOGGER.info("activatedImei" + activatedImei);
|
| 138 |
activatedImei.setActivationTimestamp(dueDate.atStartOfDay().minusYears(1));
|
141 |
LOGGER.info("vivoImeiStatusModel" + dueDate.atStartOfDay());
|
| 139 |
|
142 |
|
| 140 |
LOGGER.info("activatedImei" + activatedImei);
|
- |
|
| 141 |
LOGGER.info("vivoImeiStatusModel" + dueDate.atStartOfDay());
|
- |
|
| 142 |
|
143 |
|
| 143 |
|
- |
|
| 144 |
}
|
144 |
}
|
| 145 |
}
|
145 |
}
|
| 146 |
|
146 |
|