Subversion Repositories SmartDukaan

Rev

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

Rev 30329 Rev 30335
Line 1... Line 1...
1
package com.smartdukaan.cron.scheduled;
1
package com.smartdukaan.cron.scheduled;
2
 
2
 
3
import com.smartdukaan.cron.migrations.RunOnceTasks;
-
 
4
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
3
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
5
import com.spice.profitmandi.dao.entity.fofo.ActivatedImei;
4
import com.spice.profitmandi.dao.entity.fofo.ActivatedImei;
6
import com.spice.profitmandi.dao.model.ImeiActivationTimestampModel;
5
import com.spice.profitmandi.dao.model.ImeiActivationTimestampModel;
7
import com.spice.profitmandi.dao.repository.fofo.ActivatedImeiRepository;
6
import com.spice.profitmandi.dao.repository.fofo.ActivatedImeiRepository;
8
import com.spice.profitmandi.service.inventory.InventoryService;
7
import com.spice.profitmandi.service.inventory.InventoryService;
Line 18... Line 17...
18
import java.nio.file.Files;
17
import java.nio.file.Files;
19
import java.nio.file.Paths;
18
import java.nio.file.Paths;
20
import java.time.LocalDate;
19
import java.time.LocalDate;
21
import java.time.LocalDateTime;
20
import java.time.LocalDateTime;
22
import java.time.format.DateTimeFormatter;
21
import java.time.format.DateTimeFormatter;
-
 
22
import java.util.ArrayList;
-
 
23
import java.util.HashMap;
-
 
24
import java.util.List;
23
import java.util.*;
25
import java.util.Map;
24
 
26
 
25
@Component
27
@Component
26
class VivoImeiActivationService {
28
class VivoImeiActivationService {
27
 
29
 
28
	@Autowired
30
	@Autowired
Line 32... Line 34...
32
	CaptchaService captchaService;
34
	CaptchaService captchaService;
33
 
35
 
34
	@Autowired
36
	@Autowired
35
	ActivatedImeiRepository activatedImeiRepository;
37
	ActivatedImeiRepository activatedImeiRepository;
36
 
38
 
37
	private static final Logger LOGGER = LogManager.getLogger(RunOnceTasks.class);
39
	private static final Logger LOGGER = LogManager.getLogger(VivoImeiActivationService.class);
38
 
40
 
39
	private final Map<String, List<Cookie>> cookieStore = new HashMap<>();
41
	private final Map<String, List<Cookie>> cookieStore = new HashMap<>();
40
 
42
 
41
	public void checkImeiActivation() throws ProfitMandiBusinessException, IOException, Exception {
43
	public void checkImeiActivation() throws ProfitMandiBusinessException, IOException, Exception {
42
 
44
 
Line 49... Line 51...
49
 
51
 
50
			@Override
52
			@Override
51
			public List<Cookie> loadForRequest(HttpUrl httpUrl) {
53
			public List<Cookie> loadForRequest(HttpUrl httpUrl) {
52
				List<Cookie> cookies = cookieStore.get(httpUrl.host());
54
				List<Cookie> cookies = cookieStore.get(httpUrl.host());
53
				LOGGER.info("load from - {}", httpUrl.host());
55
				LOGGER.info("load from - {}", httpUrl.host());
54
				return cookies != null ? cookies : new ArrayList<Cookie>();
56
				return cookies != null ? cookies : new ArrayList<>();
55
			}
57
			}
56
		}).build();
58
		}).build();
57
 
59
 
58
		String captchaBreak = this.getNewCaptcha(okHttpClient);
-
 
59
 
60
 
-
 
61
		Request request = new Request.Builder().url("https://www.vivo.com/in/support/IMEI").build();
60
		LOGGER.info("Captcha code {}", captchaBreak);
62
		okHttpClient.newCall(request).execute();
61
 
63
 
-
 
64
 
-
 
65
 
-
 
66
 
62
		List<ImeiActivationTimestampModel> imeisActivationList = activatedImeiRepository
67
/*		List<ImeiActivationTimestampModel> imeisActivationList = activatedImeiRepository
63
				.selectImeiActivationByBrand("Vivo");
68
				.selectImeiActivationByBrand("Vivo");*/
-
 
69
 
-
 
70
		List<ImeiActivationTimestampModel> imeisActivationList = new ArrayList<>();
-
 
71
		imeisActivationList.add(new ImeiActivationTimestampModel("862495052177812", null));
-
 
72
		imeisActivationList.add(new ImeiActivationTimestampModel("862495052181434", null));
-
 
73
		imeisActivationList.add(new ImeiActivationTimestampModel("860009056563950", null));
64
 
74
 
65
		LOGGER.info("Complete list size is - {}", imeisActivationList.size());
75
		LOGGER.info("Complete list size is - {}", imeisActivationList.size());
-
 
76
		for (ImeiActivationTimestampModel imeiActivationTimestampModel : imeisActivationList) {
-
 
77
			String captchaBreak = this.getNewCaptcha(okHttpClient);
66
		this.getImeiActivation(captchaBreak, okHttpClient, imeisActivationList);
78
			this.getImeiActivation(captchaBreak, okHttpClient, imeiActivationTimestampModel);
-
 
79
		}
67
 
80
 
68
	}
81
	}
69
 
82
 
70
	public String getNewCaptcha(OkHttpClient okHttpClient) throws Exception {
83
	public String getNewCaptcha(OkHttpClient okHttpClient) throws Exception {
71
 
84
 
Line 77... Line 90...
77
		Response response = okHttpClient.newCall(request).execute();
90
		Response response = okHttpClient.newCall(request).execute();
78
		LOGGER.info("cookie {}", this.cookieStore.values());
91
		LOGGER.info("cookie {}", this.cookieStore.values());
79
 
92
 
80
		LOGGER.info("vivoSupportUrl" + vivoSupportUrl);
93
		LOGGER.info("vivoSupportUrl" + vivoSupportUrl);
81
 
94
 
82
		String filePath = "/tmp/captcha.avif";
95
		String filePath = "/tmp/captcha.jpg";
83
		Files.write(Paths.get(filePath), response.body().bytes());
96
		Files.write(Paths.get(filePath), response.body().bytes());
84
 
97
 
85
		// Get all relevent imes from activatedImei and inventoryItem for vivo
98
		// Get all relevent imes from activatedImei and inventoryItem for vivo
86
 
99
 
87
		String captchaBreak = captchaService.getCaptchaCode(filePath);
100
		String captchaBreak = captchaService.getCaptchaCode(filePath);
88
		System.out.println("Captcha is " + captchaBreak);
101
		System.out.println("Captcha is " + captchaBreak);
89
		return captchaBreak;
102
		return captchaBreak;
90
	}
103
	}
91
 
104
 
92
	public void getImeiActivation(String captchaBreak, OkHttpClient okHttpClient,
105
	public void getImeiActivation(String captchaBreak, OkHttpClient okHttpClient,
93
								  List<ImeiActivationTimestampModel> imeisActivationList) throws Exception {
106
								  ImeiActivationTimestampModel vivoImeiAndActivationTimeStampModel) throws Exception {
94
 
-
 
95
		for (ImeiActivationTimestampModel vivoImeiAndActivationTimeStampModel : new HashSet<>(imeisActivationList)) {
-
 
96
			String imei = vivoImeiAndActivationTimeStampModel.getSerialNumber();
-
 
97
			RequestBody formBody = new FormBody.Builder().add("imei", imei).add("code", captchaBreak).build();
-
 
98
 
-
 
99
			Request request1 = new Request.Builder().url("https://www.vivo.com/in/support/checkCode").post(formBody)
-
 
100
					.build();
-
 
101
 
-
 
102
			System.out.println("Getting Response from captcha");
-
 
103
			Response response2 = okHttpClient.newCall(request1).execute();
-
 
104
			JSONObject imeiActivationJson = new JSONObject(response2.body().string());
-
 
105
			JSONObject data = imeiActivationJson.getJSONObject("data");
-
 
106
 
-
 
107
			int status = data.getInt("status");
-
 
108
			if (status == 0) {
-
 
109
				System.out.println("Invalid captcha " + captchaBreak);
-
 
110
				captchaBreak = this.getNewCaptcha(okHttpClient);
-
 
111
				System.exit(0);
-
 
112
				//continue;
-
 
113
			} else if (status == 2) {
-
 
114
				LOGGER.info("Received status 2 for {}, data {}", imei, data);
-
 
115
				System.exit(0);
-
 
116
				continue;
-
 
117
			}
-
 
118
 
-
 
119
			JSONObject imeiQueryDto = data.getJSONObject("imeiQueryDto");
-
 
120
 
107
 
121
			Object dueTimeObject = imeiQueryDto.get("dueTime");
108
		String imei = vivoImeiAndActivationTimeStampModel.getSerialNumber();
122
			String dueTimeString = dueTimeObject.toString();
109
		RequestBody formBody = new FormBody.Builder().add("imei", imei).add("code", captchaBreak).build();
123
 
110
 
124
			DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
111
		Request request1 = new Request.Builder().url("https://www.vivo.com/in/support/checkCode").post(formBody)
125
			String dueTimeSplit = dueTimeString.split(" ")[0];
112
				.build();
126
 
113
 
-
 
114
		Response response2 = okHttpClient.newCall(request1).execute();
-
 
115
		JSONObject imeiActivationJson = new JSONObject(response2.body().string());
-
 
116
		JSONObject data = imeiActivationJson.getJSONObject("data");
-
 
117
		int status = data.getInt("status");
-
 
118
		if (status == 0) {
-
 
119
			LOGGER.info("Found invalid captcha");
-
 
120
			return;
-
 
121
		} else if (status == 2) {
-
 
122
			LOGGER.info("Received status 2 for {}, data {}", imei, data);
-
 
123
			return;
-
 
124
		}
127
 
125
 
-
 
126
		JSONObject imeiQueryDto = data.getJSONObject("imeiQueryDto");
-
 
127
		LOGGER.info("imeiQueryDto - {}", imeiQueryDto);
-
 
128
		String dueTimeString = imeiQueryDto.getString("dueTime");
-
 
129
		if (dueTimeString.equals("")) return;
-
 
130
 
-
 
131
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
-
 
132
		String dueTimeSplit = dueTimeString.split(" ")[0];
128
			if (!StringUtils.isEmpty(dueTimeSplit)) {
133
		if (!StringUtils.isEmpty(dueTimeSplit)) {
129
				LocalDate dateTime = LocalDate.parse(dueTimeSplit, formatter);
134
			LocalDate dateTime = LocalDate.parse(dueTimeSplit, formatter);
130
				ActivatedImei activatedImei = activatedImeiRepository.selectBySerialNumber(imei);
135
			ActivatedImei activatedImei = activatedImeiRepository.selectBySerialNumber(imei);
131
				if (activatedImei == null) {
136
			if (activatedImei == null) {
-
 
137
				LOGGER.info("Adding activation for - {}", imei);
132
					activatedImei = new ActivatedImei();
138
				activatedImei = new ActivatedImei();
133
					activatedImei.setActivationTimestamp(dateTime.atStartOfDay().minusYears(1));
139
				activatedImei.setActivationTimestamp(dateTime.atStartOfDay().minusYears(1));
134
					activatedImei.setSerialNumber(imei);
140
				activatedImei.setSerialNumber(imei);
135
					activatedImei.setCreateTimestamp(LocalDateTime.now());
141
				activatedImei.setCreateTimestamp(LocalDateTime.now());
136
					activatedImeiRepository.persist(activatedImei);
142
				activatedImeiRepository.persist(activatedImei);
137
 
143
 
138
				} else {
144
			} else {
139
					activatedImei.setActivationTimestamp(dateTime.atStartOfDay().minusYears(1));
-
 
140
 
-
 
141
				}
-
 
142
 
-
 
143
				LOGGER.info("activatedImei" + activatedImei);
145
				LOGGER.info("Updating activation for - {}", imei);
144
 
-
 
145
				LOGGER.info("vivoImeiStatusModel" + dateTime.atStartOfDay());
146
				activatedImei.setActivationTimestamp(dateTime.atStartOfDay().minusYears(1));
146
 
147
 
147
			}
148
			}
148
 
-
 
-
 
149
			LOGGER.info("activatedImei" + activatedImei);
-
 
150
			LOGGER.info("vivoImeiStatusModel" + dateTime.atStartOfDay());
149
 
151
 
150
		}
152
		}
-
 
153
 
-
 
154
 
151
	}
155
	}
152
}
156
}
153
157