Subversion Repositories SmartDukaan

Rev

Rev 15599 | 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;
15599 manish.sha 7
import in.shop2020.serving.services.SnapdealSessionCookie;
10225 vikram.rag 8
import in.shop2020.thrift.clients.CatalogClient;
9
import in.shop2020.utils.GmailUtils;
10
 
11
import java.io.BufferedReader;
12
import java.io.File;
13
import java.io.IOException;
14
import java.io.InputStreamReader;
15
import java.util.ArrayList;
15599 manish.sha 16
import java.util.HashMap;
10225 vikram.rag 17
import java.util.List;
15599 manish.sha 18
import java.util.Map;
10225 vikram.rag 19
 
20
import javax.mail.MessagingException;
21
 
22
import org.apache.http.HttpResponse;
23
import org.apache.http.NameValuePair;
24
import org.apache.http.client.ClientProtocolException;
25
import org.apache.http.client.entity.UrlEncodedFormEntity;
26
import org.apache.http.client.methods.HttpGet;
27
import org.apache.http.client.methods.HttpPost;
10255 vikram.rag 28
import org.apache.http.conn.ClientConnectionManager;
10225 vikram.rag 29
import org.apache.http.impl.client.DefaultHttpClient;
10255 vikram.rag 30
import org.apache.http.impl.conn.PoolingClientConnectionManager;
10225 vikram.rag 31
import org.apache.http.message.BasicNameValuePair;
32
import org.apache.thrift.TException;
33
import org.apache.thrift.transport.TTransportException;
15599 manish.sha 34
import org.json.JSONObject;
10227 vikram.rag 35
import org.slf4j.Logger;
36
import org.slf4j.LoggerFactory;
10225 vikram.rag 37
 
38
public class UpdateSDPricingUsingPanel extends Thread{
10342 vikram.rag 39
	static ClientConnectionManager connManager = new PoolingClientConnectionManager();
40
	private static DefaultHttpClient httpClient = new DefaultHttpClient(connManager);
41
	//private static DefaultHttpClient httpClient = new DefaultHttpClient();
10225 vikram.rag 42
	private Float price;
43
	private String supc;
44
	private Item item;
45
	private Long timestamp;
10342 vikram.rag 46
	//public String[] sendTo = new String[]{"vikram.raghav@shop2020.in"};
10227 vikram.rag 47
	private static Logger logger = LoggerFactory.getLogger(UpdateSDPricingUsingPanel.class);
14780 manish.sha 48
	public String[] sendTo = new String[]{ "sandeep.sachdeva@shop2020.in", "manish.sharma@shop2020.in", "rajneesh.arora@shop2020.in",
10225 vikram.rag 49
			"khushal.bhatia@shop2020.in","manoj.kumar@saholic.com","chaitnaya.vats@saholic.com",
10258 vikram.rag 50
			"yukti.jain@shop2020.in","chandan.kumar@shop2020.in","ankush.dhingra@shop2020.in","kshitij.sood@shop2020.in"};
10859 vikram.rag 51
	public String emailFromAddress = "build@shop2020.in";
52
	public String password = "cafe@nes";
15599 manish.sha 53
	private static Map<String, String> headers = new HashMap<String, String>();
54
	static {
55
		headers.put("User-agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11");
56
		headers.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
57
		headers.put("Accept-Language", "en-US,en;q=0.8");
58
		headers.put("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.3");
59
	}
10859 vikram.rag 60
	public UpdateSDPricingUsingPanel(Float price,String supc,Item item,Long timestamp){
61
		logger.info("Calling Update Snapdeal Price Constructor --" + " Price :" +price + " Supc :"+supc +" Item ID:" +item.getId());
62
		this.price = price;
63
		this.supc = supc;
64
		this.item = item;
65
		this.timestamp = timestamp; 
66
	}
67
	public UpdateSDPricingUsingPanel() {
68
	}
69
	public static void main(String... args) throws ClientProtocolException, IOException, TTransportException, CatalogServiceException, TException{
70
		Item item = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient().getItem(2231);
15603 kshitij.so 71
		UpdateSDPricingUsingPanel updatePriceOnSnapdeal = new UpdateSDPricingUsingPanel(49000f,"SDL850558681",item,System.currentTimeMillis());
10859 vikram.rag 72
		logger.info("Calling Thread to update price at snapdeal");
14951 manish.sha 73
		updatePriceOnSnapdeal.start();	
74
 
10859 vikram.rag 75
	}
15603 kshitij.so 76
 
15599 manish.sha 77
	int updatePricing(Float price,String supc,Item item,Long timestamp) throws Exception{
10859 vikram.rag 78
		logger.info("Calling Update Snapdeal Price Constructor --" + " Price :" +price + " Supc :"+supc +" Item ID:" +item.getId());
15599 manish.sha 79
		SnapdealSessionCookie sdSessionCookie = new SnapdealSessionCookie();
80
		String cookies = "";
81
		JSONObject cookieObject = null;
13857 manish.sha 82
		long priceVal = (long)price.floatValue();
13858 manish.sha 83
		HttpPost post = new HttpPost("http://seller.snapdeal.com/pricing/update");
15599 manish.sha 84
		cookies = sdSessionCookie.getCookies();
85
		cookieObject = new JSONObject(cookies);
86
		for(String key:headers.keySet())
87
			post.addHeader(key, headers.get(key));
88
		post.addHeader("Cookie","SERVERID="+cookieObject.get("SERVERID")+";sfJSESSIONID="+cookieObject.get("sfJSESSIONID")+";");
13858 manish.sha 89
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
12065 kshitij.so 90
		nameValuePairs.add(new BasicNameValuePair("changedField",
91
                "sellingPrice"));
92
		nameValuePairs.add(new BasicNameValuePair("changedValue",
13858 manish.sha 93
				priceVal+""));
94
		nameValuePairs.add(new BasicNameValuePair("supc",
10859 vikram.rag 95
				supc));
13858 manish.sha 96
		post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
10859 vikram.rag 97
		HttpResponse response = httpClient.execute(post);
98
		BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
99
		String line = "";
100
		while ((line = rd.readLine()) != null) {
13856 manish.sha 101
			logger.info(line);
10859 vikram.rag 102
			System.out.println(line);
103
		}
104
		return 1;
105
	}
10225 vikram.rag 106
 
10859 vikram.rag 107
	public void handleLogin() throws ClientProtocolException, IOException{
108
		logger.info("----Inside Handle Login ----" );
13854 manish.sha 109
		HttpGet get = new HttpGet("http://selleraccounts.snapdeal.com/login?service=http%3A%2F%2Fseller.snapdeal.com%2Fj_spring_cas_security_check");
10859 vikram.rag 110
		HttpResponse response = null;
111
		boolean retry = true;
112
		while(retry){
113
			try {
114
				response = httpClient.execute(get);
115
				retry = false;
116
			} catch (ClientProtocolException e) {
117
				e.printStackTrace();
118
				logger.error("Exception ",e);
119
			} catch (IOException e) {
120
				e.printStackTrace();
121
				logger.info("Exception " + e);
122
			}
123
		}
124
		BufferedReader rd = null;
125
		try {
126
			rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
127
		} catch (IllegalStateException e1) {
128
			e1.printStackTrace();
129
		} catch (IOException e1) {
130
			e1.printStackTrace();
131
		}
132
		String line = "";
133
		StringBuffer sb = new StringBuffer();
134
		try {
135
			while ((line = rd.readLine()) != null) {
136
				sb.append(line);
137
			}
138
		} catch (IOException e) {
139
			e.printStackTrace();
140
		}
141
		int i= sb.toString().indexOf("name=\"lt\" value=");
142
		char[] charArray = sb.toString().toCharArray();
143
		String lt = "";
144
		int j=0;
145
		for(j=i+16;j<=charArray.length;j++){
10225 vikram.rag 146
 
10859 vikram.rag 147
			if(charArray[j]==' '){
148
				break;
149
			}
150
		}
151
		lt = sb.substring(i+17,j-1);
152
		System.out.println("LT VALUE " + lt);
153
		i= sb.toString().indexOf("name=\"execution\" value=");
154
		charArray = sb.toString().toCharArray();
155
		String ex = "";
156
		j=0;
157
		for(j=i+24;j<=charArray.length;j++){
158
			if(charArray[j]==' '){
159
				break;
160
			}
161
		}
162
		ex = sb.substring(i+24,j-1);
163
		System.out.println("EXECUTION VALUE " + ex);
164
		HttpPost post = new HttpPost("http://selleraccounts.snapdeal.com/login?service=http%3A%2F%2Fseller.snapdeal.com%2Fj_spring_cas_security_check");
165
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
166
		nameValuePairs.add(new BasicNameValuePair("username",
167
		"saholic-snapdeal@saholic.com"));
168
		nameValuePairs.add(new BasicNameValuePair("password",
15223 manish.sha 169
		"saholic15"));
10859 vikram.rag 170
		nameValuePairs.add(new BasicNameValuePair("_eventId","submit"));
171
		nameValuePairs.add(new BasicNameValuePair("execution",ex));
172
		nameValuePairs.add(new BasicNameValuePair("lt",lt));
173
		nameValuePairs.add(new BasicNameValuePair("submit","LOGIN"));
174
		post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
175
		response = httpClient.execute(post);
176
		rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
177
		line = "";
178
		while ((line = rd.readLine()) != null) {
179
			System.out.println(line);
180
		}
181
		get = new HttpGet("http://seller.snapdeal.com/pricing");
182
		response = httpClient.execute(get);
183
		rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
184
		while ((line = rd.readLine()) != null) {
185
			//System.out.println(line);
186
		}
187
		logger.info("----Login Successful----" );
188
	}
189
	public void run()
190
	{
15603 kshitij.so 191
		logger.info("inside run");
10859 vikram.rag 192
		try {
193
			this.updatePricing(this.price,this.supc,this.item,timestamp);
194
			boolean retry =true;
15599 manish.sha 195
			SnapdealSessionCookie sdSessionCookie = new SnapdealSessionCookie();
196
			String cookies = "";
197
			JSONObject cookieObject = null;
10859 vikram.rag 198
			while(retry){
199
				Thread.sleep(2*60*1000);
200
				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");
15599 manish.sha 201
				cookies = sdSessionCookie.getCookies();
202
				cookieObject = new JSONObject(cookies);
203
				for(String key:headers.keySet())
204
					get.addHeader(key, headers.get(key));
205
				get.addHeader("Cookie","SERVERID="+cookieObject.get("SERVERID")+";sfJSESSIONID="+cookieObject.get("sfJSESSIONID")+";");
10859 vikram.rag 206
				HttpResponse response = httpClient.execute(get);
207
				BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
208
				String line = "";
209
				while ((line = rd.readLine()) != null) {
210
					logger.info(line);
211
					if(line.contains("\"pendingUpdate\":false")){
212
						retry =false;
213
					}
214
				}
215
			}
216
			GmailUtils mailer = new GmailUtils();
217
			String text = "Product : " +getProductName(this.item) +"\n"+ 
218
			"Item ID : " +this.item.getId() +"\n"+
219
			"SUPC : " +this.supc +"\n"+
220
			"Updated Price : " +this.price;
221
			try{
14951 manish.sha 222
				mailer.sendSSLMessage(sendTo, "Price updated on Snapdeal ( Item ID " + this.item.getId()+ " )",text, emailFromAddress , password,new ArrayList<File>());
15603 kshitij.so 223
				logger.info("mail sent");
10859 vikram.rag 224
			}
225
			catch(Exception e){
226
				logger.info("Exception"+e);
227
			}
228
			ArrayList<Long> updateList = new ArrayList<Long>();
229
			try {
15603 kshitij.so 230
				logger.info("creating client");
10859 vikram.rag 231
				Client catalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
15603 kshitij.so 232
 
10859 vikram.rag 233
				updateList.add(this.item.getId());
15603 kshitij.so 234
				logger.info(updateList.toString());
10859 vikram.rag 235
				catalogClient.updateMarketPlacePriceUpdateStatus(updateList,this.timestamp,OrderSource.SNAPDEAL.getValue());
15603 kshitij.so 236
				logger.info("client call done");
10859 vikram.rag 237
			} catch (TException e) {
238
				try {
239
					new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient().updateMarketPlacePriceUpdateStatus(updateList, timestamp,OrderSource.SNAPDEAL.getValue());
240
				} catch (TTransportException e1) {
241
					e1.printStackTrace();
242
					logger.info("Exception" + e1);
243
				} catch (TException e1) {
244
					e1.printStackTrace();
245
					logger.info("Exception" + e1);
246
				}
247
				logger.info("Exception" + e);
248
			}
249
		} catch (Exception e) {
250
			e.printStackTrace();
251
			//logger.error("Exception" + e);
252
			GmailUtils mailer = new GmailUtils();
253
			String text = "Product       : " +getProductName(this.item) +"\n"+ 
254
			"Item ID       : " +this.item.getId() +"\n"+
255
			"SUPC          : " +this.supc +"\n"+
256
			"Updated Price : " +this.price;
257
			try {
14951 manish.sha 258
				mailer.sendSSLMessage(sendTo, "Failed to update Price on Snapdeal ( Item ID " + this.item.getId()+" )",text, emailFromAddress , password,new ArrayList<File>());
10859 vikram.rag 259
			} catch (MessagingException e1) {
260
				e1.printStackTrace();
261
				logger.info("Exception" + e1);
262
			}
10225 vikram.rag 263
 
10859 vikram.rag 264
		} 
265
	}
266
	String getProductName(Item item){
267
		return getName(item.getBrand())+" " + getName(item.getModelName())+" " + getName(item.getModelNumber())+" " + getName(item.getColor()); 
10225 vikram.rag 268
 
10859 vikram.rag 269
	}
270
	String getName(String name){
271
		if(name==null || name.length()==0){
272
			return "";
273
		}
274
		else{
275
			return name;
276
		}
277
	}
10229 vikram.rag 278
 
279
 
10225 vikram.rag 280
}