Subversion Repositories SmartDukaan

Rev

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