Subversion Repositories SmartDukaan

Rev

Rev 14862 | Rev 14951 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
10225 vikram.rag 1
package in.shop2020.support.utils;
2
 
3
import in.shop2020.model.v1.catalog.CatalogService.Client;
10237 vikram.rag 4
import in.shop2020.model.v1.catalog.CatalogServiceException;
10225 vikram.rag 5
import in.shop2020.model.v1.catalog.Item;
10262 kshitij.so 6
import in.shop2020.model.v1.order.OrderSource;
10225 vikram.rag 7
import in.shop2020.thrift.clients.CatalogClient;
8
import in.shop2020.utils.GmailUtils;
9
 
10
import java.io.BufferedReader;
11
import java.io.File;
12
import java.io.IOException;
13
import java.io.InputStreamReader;
14
import java.util.ArrayList;
15
import java.util.List;
16
 
17
import javax.mail.MessagingException;
18
 
19
import org.apache.http.HttpResponse;
20
import org.apache.http.NameValuePair;
21
import org.apache.http.client.ClientProtocolException;
22
import org.apache.http.client.entity.UrlEncodedFormEntity;
23
import org.apache.http.client.methods.HttpGet;
24
import org.apache.http.client.methods.HttpPost;
10255 vikram.rag 25
import org.apache.http.conn.ClientConnectionManager;
10225 vikram.rag 26
import org.apache.http.impl.client.DefaultHttpClient;
10255 vikram.rag 27
import org.apache.http.impl.conn.PoolingClientConnectionManager;
10225 vikram.rag 28
import org.apache.http.message.BasicNameValuePair;
29
import org.apache.thrift.TException;
30
import org.apache.thrift.transport.TTransportException;
10227 vikram.rag 31
import org.slf4j.Logger;
32
import org.slf4j.LoggerFactory;
10225 vikram.rag 33
 
34
public class UpdateSDPricingUsingPanel extends Thread{
10342 vikram.rag 35
	static ClientConnectionManager connManager = new PoolingClientConnectionManager();
14947 manish.sha 36
 
10342 vikram.rag 37
	private static DefaultHttpClient httpClient = new DefaultHttpClient(connManager);
14947 manish.sha 38
 
10342 vikram.rag 39
	//private static DefaultHttpClient httpClient = new DefaultHttpClient();
10225 vikram.rag 40
	private Float price;
41
	private String supc;
42
	private Item item;
43
	private Long timestamp;
14780 manish.sha 44
	public boolean voiUpdation;
10342 vikram.rag 45
	//public String[] sendTo = new String[]{"vikram.raghav@shop2020.in"};
10227 vikram.rag 46
	private static Logger logger = LoggerFactory.getLogger(UpdateSDPricingUsingPanel.class);
14780 manish.sha 47
	public String[] sendTo = new String[]{ "sandeep.sachdeva@shop2020.in", "manish.sharma@shop2020.in", "rajneesh.arora@shop2020.in",
10225 vikram.rag 48
			"khushal.bhatia@shop2020.in","manoj.kumar@saholic.com","chaitnaya.vats@saholic.com",
10258 vikram.rag 49
			"yukti.jain@shop2020.in","chandan.kumar@shop2020.in","ankush.dhingra@shop2020.in","kshitij.sood@shop2020.in"};
10859 vikram.rag 50
	public String emailFromAddress = "build@shop2020.in";
51
	public String password = "cafe@nes";
52
	public UpdateSDPricingUsingPanel(Float price,String supc,Item item,Long timestamp){
53
		logger.info("Calling Update Snapdeal Price Constructor --" + " Price :" +price + " Supc :"+supc +" Item ID:" +item.getId());
54
		this.price = price;
55
		this.supc = supc;
56
		this.item = item;
57
		this.timestamp = timestamp; 
58
	}
59
	public UpdateSDPricingUsingPanel() {
60
	}
14780 manish.sha 61
 
62
	public UpdateSDPricingUsingPanel(Float price,String supc,Item item,Long timestamp, boolean voiUpdation){
63
		logger.info("Calling Update Snapdeal Price Constructor --" + " Price :" +price + " Supc :"+supc +" Item ID:" +item.getId()+" VOI Updation: "+voiUpdation);
64
		this.price = price;
65
		this.supc = supc;
66
		this.item = item;
67
		this.timestamp = timestamp; 
68
		this.voiUpdation = voiUpdation;
69
	}
10859 vikram.rag 70
	public static void main(String... args) throws ClientProtocolException, IOException, TTransportException, CatalogServiceException, TException{
71
		Item item = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient().getItem(2231);
72
		UpdateSDPricingUsingPanel updatePriceOnSnapdeal = new UpdateSDPricingUsingPanel(22200f,"1108903",item,System.currentTimeMillis());
73
		logger.info("Calling Thread to update price at snapdeal");
14780 manish.sha 74
		updatePriceOnSnapdeal.start();
10859 vikram.rag 75
	}
76
	int updatePricing(Float price,String supc,Item item,Long timestamp) throws ClientProtocolException, IOException{
77
		logger.info("Calling Update Snapdeal Price Constructor --" + " Price :" +price + " Supc :"+supc +" Item ID:" +item.getId());
13857 manish.sha 78
		long priceVal = (long)price.floatValue();
13858 manish.sha 79
		HttpPost post = new HttpPost("http://seller.snapdeal.com/pricing/update");
80
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
12065 kshitij.so 81
		nameValuePairs.add(new BasicNameValuePair("changedField",
82
                "sellingPrice"));
83
		nameValuePairs.add(new BasicNameValuePair("changedValue",
13858 manish.sha 84
				priceVal+""));
85
		nameValuePairs.add(new BasicNameValuePair("supc",
10859 vikram.rag 86
				supc));
13858 manish.sha 87
		post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
10859 vikram.rag 88
		HttpResponse response = httpClient.execute(post);
89
		BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
90
		String line = "";
91
		while ((line = rd.readLine()) != null) {
13856 manish.sha 92
			logger.info(line);
10859 vikram.rag 93
			System.out.println(line);
94
		}
95
		return 1;
96
	}
10225 vikram.rag 97
 
10859 vikram.rag 98
	public void handleLogin() throws ClientProtocolException, IOException{
99
		logger.info("----Inside Handle Login ----" );
13854 manish.sha 100
		HttpGet get = new HttpGet("http://selleraccounts.snapdeal.com/login?service=http%3A%2F%2Fseller.snapdeal.com%2Fj_spring_cas_security_check");
10859 vikram.rag 101
		HttpResponse response = null;
102
		boolean retry = true;
103
		while(retry){
104
			try {
105
				response = httpClient.execute(get);
106
				retry = false;
107
			} catch (ClientProtocolException e) {
108
				e.printStackTrace();
109
				logger.error("Exception ",e);
110
			} catch (IOException e) {
111
				e.printStackTrace();
112
				logger.info("Exception " + e);
113
			}
114
		}
115
		BufferedReader rd = null;
116
		try {
117
			rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
118
		} catch (IllegalStateException e1) {
119
			e1.printStackTrace();
120
		} catch (IOException e1) {
121
			e1.printStackTrace();
122
		}
123
		String line = "";
124
		StringBuffer sb = new StringBuffer();
125
		try {
126
			while ((line = rd.readLine()) != null) {
127
				sb.append(line);
128
			}
129
		} catch (IOException e) {
130
			e.printStackTrace();
131
		}
132
		int i= sb.toString().indexOf("name=\"lt\" value=");
133
		char[] charArray = sb.toString().toCharArray();
134
		String lt = "";
135
		int j=0;
136
		for(j=i+16;j<=charArray.length;j++){
10225 vikram.rag 137
 
10859 vikram.rag 138
			if(charArray[j]==' '){
139
				break;
140
			}
141
		}
142
		lt = sb.substring(i+17,j-1);
143
		System.out.println("LT VALUE " + lt);
144
		i= sb.toString().indexOf("name=\"execution\" value=");
145
		charArray = sb.toString().toCharArray();
146
		String ex = "";
147
		j=0;
148
		for(j=i+24;j<=charArray.length;j++){
149
			if(charArray[j]==' '){
150
				break;
151
			}
152
		}
153
		ex = sb.substring(i+24,j-1);
154
		System.out.println("EXECUTION VALUE " + ex);
155
		HttpPost post = new HttpPost("http://selleraccounts.snapdeal.com/login?service=http%3A%2F%2Fseller.snapdeal.com%2Fj_spring_cas_security_check");
156
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
157
		nameValuePairs.add(new BasicNameValuePair("username",
158
		"saholic-snapdeal@saholic.com"));
159
		nameValuePairs.add(new BasicNameValuePair("password",
11267 vikram.rag 160
		"snapsaholic"));
10859 vikram.rag 161
		nameValuePairs.add(new BasicNameValuePair("_eventId","submit"));
162
		nameValuePairs.add(new BasicNameValuePair("execution",ex));
163
		nameValuePairs.add(new BasicNameValuePair("lt",lt));
164
		nameValuePairs.add(new BasicNameValuePair("submit","LOGIN"));
165
		post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
166
		response = httpClient.execute(post);
167
		rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
168
		line = "";
169
		while ((line = rd.readLine()) != null) {
170
			System.out.println(line);
171
		}
172
		get = new HttpGet("http://seller.snapdeal.com/pricing");
173
		response = httpClient.execute(get);
174
		rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
175
		while ((line = rd.readLine()) != null) {
176
			//System.out.println(line);
177
		}
178
		logger.info("----Login Successful----" );
179
	}
14780 manish.sha 180
 
181
	public void handleVoiLogin() throws ClientProtocolException, IOException{
182
		logger.info("----Inside Handle Voi Login ----" );
183
		HttpGet get = new HttpGet("http://selleraccounts.snapdeal.com/login?service=http%3A%2F%2Fseller.snapdeal.com%2Fj_spring_cas_security_check");
184
		HttpResponse response = null;
185
		boolean retry = true;
186
		while(retry){
187
			try {
188
				response = httpClient.execute(get);
189
				retry = false;
190
			} catch (ClientProtocolException e) {
191
				e.printStackTrace();
192
				logger.error("Exception ",e);
193
			} catch (IOException e) {
194
				e.printStackTrace();
195
				logger.info("Exception " + e);
196
			}
197
		}
198
		BufferedReader rd = null;
199
		try {
200
			rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
201
		} catch (IllegalStateException e1) {
202
			e1.printStackTrace();
203
		} catch (IOException e1) {
204
			e1.printStackTrace();
205
		}
206
		String line = "";
207
		StringBuffer sb = new StringBuffer();
208
		try {
209
			while ((line = rd.readLine()) != null) {
210
				sb.append(line);
211
			}
212
		} catch (IOException e) {
213
			e.printStackTrace();
214
		}
215
		int i= sb.toString().indexOf("name=\"lt\" value=");
216
		char[] charArray = sb.toString().toCharArray();
217
		String lt = "";
218
		int j=0;
219
		for(j=i+16;j<=charArray.length;j++){
220
 
221
			if(charArray[j]==' '){
222
				break;
223
			}
224
		}
225
		lt = sb.substring(i+17,j-1);
226
		System.out.println("LT VALUE " + lt);
227
		i= sb.toString().indexOf("name=\"execution\" value=");
228
		charArray = sb.toString().toCharArray();
229
		String ex = "";
230
		j=0;
231
		for(j=i+24;j<=charArray.length;j++){
232
			if(charArray[j]==' '){
233
				break;
234
			}
235
		}
236
		ex = sb.substring(i+24,j-1);
237
		System.out.println("EXECUTION VALUE " + ex);
238
		HttpPost post = new HttpPost("http://selleraccounts.snapdeal.com/login?service=http%3A%2F%2Fseller.snapdeal.com%2Fj_spring_cas_security_check");
239
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
240
		nameValuePairs.add(new BasicNameValuePair("username",
241
		"spiceonlineretailvoi@gmail.com"));
242
		nameValuePairs.add(new BasicNameValuePair("password",
243
		"snapdealsaholic"));
244
		nameValuePairs.add(new BasicNameValuePair("_eventId","submit"));
245
		nameValuePairs.add(new BasicNameValuePair("execution",ex));
246
		nameValuePairs.add(new BasicNameValuePair("lt",lt));
247
		nameValuePairs.add(new BasicNameValuePair("submit","LOGIN"));
248
		post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
249
		response = httpClient.execute(post);
250
		rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
251
		line = "";
252
		while ((line = rd.readLine()) != null) {
253
			System.out.println(line);
254
		}
255
		get = new HttpGet("http://seller.snapdeal.com/pricing");
256
		response = httpClient.execute(get);
257
		rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
258
		while ((line = rd.readLine()) != null) {
259
			//System.out.println(line);
260
		}
261
		logger.info("----Login Successful into Snapdeal VOI----" );
262
	}
10859 vikram.rag 263
	public void run()
264
	{
265
		try {
14947 manish.sha 266
			if(this.voiUpdation){
14780 manish.sha 267
				this.handleVoiLogin();
268
			}else{
269
				this.handleLogin();
270
			}
10859 vikram.rag 271
			this.updatePricing(this.price,this.supc,this.item,timestamp);
272
			boolean retry =true;
273
			while(retry){
274
				Thread.sleep(2*60*1000);
275
				HttpGet get = new HttpGet("http://seller.snapdeal.com/pricing/search?searchType=SUPC&searchValue="+this.supc+"&gridType=normal&_search=false&nd="+System.currentTimeMillis()+"&rows=30&page=1&sidx=&sord=asc");
276
				HttpResponse response = httpClient.execute(get);
277
				BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
278
				String line = "";
279
				while ((line = rd.readLine()) != null) {
280
					logger.info(line);
281
					if(line.contains("\"pendingUpdate\":false")){
282
						retry =false;
283
					}
284
				}
285
			}
286
			GmailUtils mailer = new GmailUtils();
287
			String text = "Product : " +getProductName(this.item) +"\n"+ 
288
			"Item ID : " +this.item.getId() +"\n"+
289
			"SUPC : " +this.supc +"\n"+
290
			"Updated Price : " +this.price;
291
			try{
14862 manish.sha 292
				if(voiUpdation){
293
					mailer.sendSSLMessage(sendTo, "Price updated on Snapdeal VOI ( Item ID " + this.item.getId()+ " )",text, emailFromAddress , password,new ArrayList<File>());
294
				}else{
295
					mailer.sendSSLMessage(sendTo, "Price updated on Snapdeal ( Item ID " + this.item.getId()+ " )",text, emailFromAddress , password,new ArrayList<File>());
296
				}
10859 vikram.rag 297
			}
298
			catch(Exception e){
299
				logger.info("Exception"+e);
300
			}
301
			ArrayList<Long> updateList = new ArrayList<Long>();
302
			try {
303
				Client catalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
10225 vikram.rag 304
 
10859 vikram.rag 305
				updateList.add(this.item.getId());
306
				catalogClient.updateMarketPlacePriceUpdateStatus(updateList,this.timestamp,OrderSource.SNAPDEAL.getValue());
307
			} catch (TException e) {
308
				try {
309
					new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient().updateMarketPlacePriceUpdateStatus(updateList, timestamp,OrderSource.SNAPDEAL.getValue());
310
				} catch (TTransportException e1) {
311
					e1.printStackTrace();
312
					logger.info("Exception" + e1);
313
				} catch (TException e1) {
314
					e1.printStackTrace();
315
					logger.info("Exception" + e1);
316
				}
317
				logger.info("Exception" + e);
318
			}
319
		} catch (Exception e) {
320
			e.printStackTrace();
321
			//logger.error("Exception" + e);
322
			GmailUtils mailer = new GmailUtils();
323
			String text = "Product       : " +getProductName(this.item) +"\n"+ 
324
			"Item ID       : " +this.item.getId() +"\n"+
325
			"SUPC          : " +this.supc +"\n"+
326
			"Updated Price : " +this.price;
327
			try {
14862 manish.sha 328
				if(voiUpdation){
329
					mailer.sendSSLMessage(sendTo, "Failed to update Price on Snapdeal VOI ( Item ID " + this.item.getId()+" )",text, emailFromAddress , password,new ArrayList<File>());
330
				}else{
331
					mailer.sendSSLMessage(sendTo, "Failed to update Price on Snapdeal ( Item ID " + this.item.getId()+" )",text, emailFromAddress , password,new ArrayList<File>());
332
				}
10859 vikram.rag 333
			} catch (MessagingException e1) {
334
				e1.printStackTrace();
335
				logger.info("Exception" + e1);
336
			}
10225 vikram.rag 337
 
10859 vikram.rag 338
		} 
339
	}
340
	String getProductName(Item item){
341
		return getName(item.getBrand())+" " + getName(item.getModelName())+" " + getName(item.getModelNumber())+" " + getName(item.getColor()); 
10225 vikram.rag 342
 
10859 vikram.rag 343
	}
344
	String getName(String name){
345
		if(name==null || name.length()==0){
346
			return "";
347
		}
348
		else{
349
			return name;
350
		}
351
	}
10229 vikram.rag 352
 
353
 
10225 vikram.rag 354
}