Subversion Repositories SmartDukaan

Rev

Rev 29457 | Rev 29468 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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