| Line 1... |
Line 1... |
| 1 |
package com.smartdukaan.cron.scheduled;
|
1 |
package com.smartdukaan.cron.scheduled;
|
| 2 |
|
2 |
|
| - |
|
3 |
import com.spice.profitmandi.common.web.client.HttpClientFactory;
|
| 3 |
import com.spice.profitmandi.dao.model.ImeiActivationTimestampModel;
|
4 |
import com.spice.profitmandi.dao.model.ImeiActivationTimestampModel;
|
| 4 |
import com.spice.profitmandi.dao.repository.fofo.ActivatedImeiRepository;
|
5 |
import com.spice.profitmandi.dao.repository.fofo.ActivatedImeiRepository;
|
| 5 |
import com.spice.profitmandi.service.inventory.InventoryService;
|
6 |
import com.spice.profitmandi.service.inventory.InventoryService;
|
| 6 |
import okhttp3.*;
|
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;
|
| 7 |
import org.apache.logging.log4j.LogManager;
|
16 |
import org.apache.logging.log4j.LogManager;
|
| 8 |
import org.apache.logging.log4j.Logger;
|
17 |
import org.apache.logging.log4j.Logger;
|
| 9 |
import org.json.JSONObject;
|
18 |
import org.json.JSONObject;
|
| 10 |
import org.springframework.beans.factory.annotation.Autowired;
|
19 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 11 |
import org.springframework.stereotype.Component;
|
20 |
import org.springframework.stereotype.Component;
|
| 12 |
import org.springframework.transaction.annotation.Propagation;
|
21 |
import org.springframework.transaction.annotation.Propagation;
|
| 13 |
import org.springframework.transaction.annotation.Transactional;
|
22 |
import org.springframework.transaction.annotation.Transactional;
|
| 14 |
|
23 |
|
| - |
|
24 |
import java.io.IOException;
|
| - |
|
25 |
import java.nio.charset.StandardCharsets;
|
| 15 |
import java.nio.file.Files;
|
26 |
import java.nio.file.Files;
|
| 16 |
import java.nio.file.Paths;
|
27 |
import java.nio.file.Paths;
|
| 17 |
import java.time.LocalDate;
|
28 |
import java.time.LocalDate;
|
| 18 |
import java.time.format.DateTimeFormatter;
|
29 |
import java.time.format.DateTimeFormatter;
|
| 19 |
import java.util.ArrayList;
|
30 |
import java.util.ArrayList;
|
| 20 |
import java.util.HashMap;
|
- |
|
| 21 |
import java.util.List;
|
31 |
import java.util.List;
|
| 22 |
import java.util.Map;
|
- |
|
| 23 |
|
32 |
|
| 24 |
@Component
|
33 |
@Component
|
| 25 |
class VivoImeiActivationService {
|
34 |
class VivoImeiActivationService {
|
| 26 |
|
35 |
|
| 27 |
@Autowired
|
36 |
@Autowired
|
| Line 33... |
Line 42... |
| 33 |
@Autowired
|
42 |
@Autowired
|
| 34 |
ActivatedImeiRepository activatedImeiRepository;
|
43 |
ActivatedImeiRepository activatedImeiRepository;
|
| 35 |
|
44 |
|
| 36 |
private static final Logger LOGGER = LogManager.getLogger(VivoImeiActivationService.class);
|
45 |
private static final Logger LOGGER = LogManager.getLogger(VivoImeiActivationService.class);
|
| 37 |
|
46 |
|
| 38 |
private final Map<String, List<Cookie>> cookieStore = new HashMap<>();
|
- |
|
| 39 |
|
- |
|
| 40 |
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
47 |
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
| 41 |
public void checkImeiActivation() throws Exception {
|
48 |
public void checkImeiActivation() throws Exception {
|
| 42 |
if (LocalDate.now().getDayOfMonth() == 1) return;
|
49 |
if (LocalDate.now().getDayOfMonth() == 1) return;
|
| 43 |
|
50 |
|
| 44 |
List<ImeiActivationTimestampModel> imeisActivationList = activatedImeiRepository
|
51 |
List<ImeiActivationTimestampModel> imeisActivationList = activatedImeiRepository
|
| Line 60... |
Line 67... |
| 60 |
}
|
67 |
}
|
| 61 |
|
68 |
|
| 62 |
private void processImeis(List<ImeiActivationTimestampModel> imeisActivationList) throws Exception {
|
69 |
private void processImeis(List<ImeiActivationTimestampModel> imeisActivationList) throws Exception {
|
| 63 |
if (imeisActivationList.isEmpty()) return;
|
70 |
if (imeisActivationList.isEmpty()) return;
|
| 64 |
|
71 |
|
| 65 |
cookieStore.clear();
|
72 |
BasicCookieStore cookieStore = new BasicCookieStore();
|
| 66 |
|
- |
|
| 67 |
OkHttpClient okHttpClient = new OkHttpClient.Builder().cookieJar(new CookieJar() {
|
73 |
try (CloseableHttpClient httpClient = HttpClientFactory.apacheHttpWithCookies(cookieStore)) {
|
| 68 |
@Override
|
74 |
// Seed the session cookies
|
| 69 |
public void saveFromResponse(HttpUrl httpUrl, List<Cookie> list) {
|
75 |
HttpGet seed = new HttpGet("https://www.vivo.com/in/support/IMEI");
|
| 70 |
LOGGER.info("SAVE fROM - {}", httpUrl.host());
|
76 |
try (CloseableHttpResponse seedResponse = httpClient.execute(seed)) {
|
| 71 |
cookieStore.put(httpUrl.host(), list);
|
77 |
EntityUtils.consumeQuietly(seedResponse.getEntity());
|
| 72 |
}
|
78 |
}
|
| 73 |
|
79 |
|
| 74 |
@Override
|
- |
|
| 75 |
public List<Cookie> loadForRequest(HttpUrl httpUrl) {
|
80 |
for (ImeiActivationTimestampModel imeiActivationTimestampModel : imeisActivationList) {
|
| 76 |
List<Cookie> cookies = cookieStore.get(httpUrl.host());
|
81 |
String captchaBreak = this.getNewCaptcha(httpClient, cookieStore);
|
| 77 |
LOGGER.info("load from - {}", httpUrl.host());
|
- |
|
| 78 |
return cookies != null ? cookies : new ArrayList<>();
|
82 |
this.getImeiActivation(captchaBreak, httpClient, imeiActivationTimestampModel);
|
| 79 |
}
|
83 |
}
|
| 80 |
}).build();
|
- |
|
| 81 |
|
- |
|
| 82 |
Request request = new Request.Builder().url("https://www.vivo.com/in/support/IMEI").build();
|
- |
|
| 83 |
try (Response seedResponse = okHttpClient.newCall(request).execute()) {
|
- |
|
| 84 |
// Consume response to establish cookies
|
- |
|
| 85 |
}
|
- |
|
| 86 |
|
- |
|
| 87 |
for (ImeiActivationTimestampModel imeiActivationTimestampModel : imeisActivationList) {
|
- |
|
| 88 |
String captchaBreak = this.getNewCaptcha(okHttpClient);
|
- |
|
| 89 |
this.getImeiActivation(captchaBreak, okHttpClient, imeiActivationTimestampModel);
|
- |
|
| 90 |
}
|
84 |
}
|
| 91 |
}
|
85 |
}
|
| 92 |
|
86 |
|
| 93 |
public String getNewCaptcha(OkHttpClient okHttpClient) throws Exception {
|
87 |
public String getNewCaptcha(CloseableHttpClient httpClient, BasicCookieStore cookieStore) throws Exception {
|
| 94 |
LOGGER.info("okHttpClient" + okHttpClient);
|
- |
|
| 95 |
|
- |
|
| 96 |
HttpUrl vivoSupportUrl = HttpUrl.parse("https://www.vivo.com/in/support/generatingCodes");
|
88 |
String vivoSupportUrl = "https://www.vivo.com/in/support/generatingCodes";
|
| 97 |
Request request = new Request.Builder().url(vivoSupportUrl).build();
|
89 |
HttpGet request = new HttpGet(vivoSupportUrl);
|
| 98 |
|
90 |
|
| 99 |
String filePath = "/tmp/captcha.jpg";
|
91 |
String filePath = "/tmp/captcha.jpg";
|
| 100 |
try (Response response = okHttpClient.newCall(request).execute()) {
|
92 |
try (CloseableHttpResponse response = httpClient.execute(request)) {
|
| 101 |
LOGGER.info("cookie {}", this.cookieStore.values());
|
93 |
LOGGER.info("cookie {}", cookieStore.getCookies());
|
| 102 |
LOGGER.info("vivoSupportUrl" + vivoSupportUrl);
|
94 |
LOGGER.info("vivoSupportUrl" + vivoSupportUrl);
|
| - |
|
95 |
byte[] bytes = EntityUtils.toByteArray(response.getEntity());
|
| 103 |
Files.write(Paths.get(filePath), response.body().bytes());
|
96 |
Files.write(Paths.get(filePath), bytes);
|
| 104 |
}
|
97 |
}
|
| 105 |
|
98 |
|
| 106 |
String captchaBreak = captchaService.getCaptchaCode(filePath);
|
99 |
String captchaBreak = captchaService.getCaptchaCode(filePath);
|
| 107 |
System.out.println("Captcha is " + captchaBreak);
|
100 |
System.out.println("Captcha is " + captchaBreak);
|
| 108 |
return captchaBreak;
|
101 |
return captchaBreak;
|
| 109 |
}
|
102 |
}
|
| 110 |
|
103 |
|
| 111 |
public void getImeiActivation(String captchaBreak, OkHttpClient okHttpClient,
|
104 |
public void getImeiActivation(String captchaBreak, CloseableHttpClient httpClient,
|
| 112 |
ImeiActivationTimestampModel vivoImeiAndActivationTimeStampModel) throws Exception {
|
105 |
ImeiActivationTimestampModel vivoImeiAndActivationTimeStampModel) throws Exception {
|
| 113 |
|
106 |
|
| 114 |
String imei = vivoImeiAndActivationTimeStampModel.getSerialNumber();
|
107 |
String imei = vivoImeiAndActivationTimeStampModel.getSerialNumber();
|
| 115 |
RequestBody formBody = new FormBody.Builder().add("imei", imei).add("code", captchaBreak).build();
|
- |
|
| 116 |
|
108 |
|
| - |
|
109 |
List<NameValuePair> form = new ArrayList<>();
|
| - |
|
110 |
form.add(new BasicNameValuePair("imei", imei));
|
| - |
|
111 |
form.add(new BasicNameValuePair("code", captchaBreak));
|
| 117 |
Request request1 = new Request.Builder().url("https://www.vivo.com/in/support/checkCode").post(formBody)
|
112 |
HttpPost post = new HttpPost("https://www.vivo.com/in/support/checkCode");
|
| 118 |
.build();
|
113 |
post.setEntity(new UrlEncodedFormEntity(form, StandardCharsets.UTF_8));
|
| 119 |
|
114 |
|
| 120 |
JSONObject imeiActivationJson;
|
115 |
JSONObject imeiActivationJson;
|
| 121 |
try (Response response2 = okHttpClient.newCall(request1).execute()) {
|
116 |
try (CloseableHttpResponse response2 = httpClient.execute(post)) {
|
| 122 |
imeiActivationJson = new JSONObject(response2.body().string());
|
117 |
imeiActivationJson = new JSONObject(EntityUtils.toString(response2.getEntity()));
|
| 123 |
}
|
118 |
}
|
| 124 |
|
119 |
|
| 125 |
JSONObject data = imeiActivationJson.getJSONObject("data");
|
120 |
JSONObject data = imeiActivationJson.getJSONObject("data");
|
| 126 |
int status = data.getInt("status");
|
121 |
int status = data.getInt("status");
|
| 127 |
if (status == 0) {
|
122 |
if (status == 0) {
|