Subversion Repositories SmartDukaan

Rev

Rev 15223 | 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;
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);
71
		UpdateSDPricingUsingPanel updatePriceOnSnapdeal = new UpdateSDPricingUsingPanel(22200f,"1108903",item,System.currentTimeMillis());
72
		logger.info("Calling Thread to update price at snapdeal");
14951 manish.sha 73
		updatePriceOnSnapdeal.start();	
74
 
10859 vikram.rag 75
	}
15599 manish.sha 76
	int updatePricing(Float price,String supc,Item item,Long timestamp) throws Exception{
10859 vikram.rag 77
		logger.info("Calling Update Snapdeal Price Constructor --" + " Price :" +price + " Supc :"+supc +" Item ID:" +item.getId());
15599 manish.sha 78
		SnapdealSessionCookie sdSessionCookie = new SnapdealSessionCookie();
79
		String cookies = "";
80
		JSONObject cookieObject = null;
13857 manish.sha 81
		long priceVal = (long)price.floatValue();
13858 manish.sha 82
		HttpPost post = new HttpPost("http://seller.snapdeal.com/pricing/update");
15599 manish.sha 83
		cookies = sdSessionCookie.getCookies();
84
		cookieObject = new JSONObject(cookies);
85
		for(String key:headers.keySet())
86
			post.addHeader(key, headers.get(key));
87
		post.addHeader("Cookie","SERVERID="+cookieObject.get("SERVERID")+";sfJSESSIONID="+cookieObject.get("sfJSESSIONID")+";");
13858 manish.sha 88
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
12065 kshitij.so 89
		nameValuePairs.add(new BasicNameValuePair("changedField",
90
                "sellingPrice"));
91
		nameValuePairs.add(new BasicNameValuePair("changedValue",
13858 manish.sha 92
				priceVal+""));
93
		nameValuePairs.add(new BasicNameValuePair("supc",
10859 vikram.rag 94
				supc));
13858 manish.sha 95
		post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
10859 vikram.rag 96
		HttpResponse response = httpClient.execute(post);
97
		BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
98
		String line = "";
99
		while ((line = rd.readLine()) != null) {
13856 manish.sha 100
			logger.info(line);
10859 vikram.rag 101
			System.out.println(line);
102
		}
103
		return 1;
104
	}
10225 vikram.rag 105
 
10859 vikram.rag 106
	public void handleLogin() throws ClientProtocolException, IOException{
107
		logger.info("----Inside Handle Login ----" );
13854 manish.sha 108
		HttpGet get = new HttpGet("http://selleraccounts.snapdeal.com/login?service=http%3A%2F%2Fseller.snapdeal.com%2Fj_spring_cas_security_check");
10859 vikram.rag 109
		HttpResponse response = null;
110
		boolean retry = true;
111
		while(retry){
112
			try {
113
				response = httpClient.execute(get);
114
				retry = false;
115
			} catch (ClientProtocolException e) {
116
				e.printStackTrace();
117
				logger.error("Exception ",e);
118
			} catch (IOException e) {
119
				e.printStackTrace();
120
				logger.info("Exception " + e);
121
			}
122
		}
123
		BufferedReader rd = null;
124
		try {
125
			rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
126
		} catch (IllegalStateException e1) {
127
			e1.printStackTrace();
128
		} catch (IOException e1) {
129
			e1.printStackTrace();
130
		}
131
		String line = "";
132
		StringBuffer sb = new StringBuffer();
133
		try {
134
			while ((line = rd.readLine()) != null) {
135
				sb.append(line);
136
			}
137
		} catch (IOException e) {
138
			e.printStackTrace();
139
		}
140
		int i= sb.toString().indexOf("name=\"lt\" value=");
141
		char[] charArray = sb.toString().toCharArray();
142
		String lt = "";
143
		int j=0;
144
		for(j=i+16;j<=charArray.length;j++){
10225 vikram.rag 145
 
10859 vikram.rag 146
			if(charArray[j]==' '){
147
				break;
148
			}
149
		}
150
		lt = sb.substring(i+17,j-1);
151
		System.out.println("LT VALUE " + lt);
152
		i= sb.toString().indexOf("name=\"execution\" value=");
153
		charArray = sb.toString().toCharArray();
154
		String ex = "";
155
		j=0;
156
		for(j=i+24;j<=charArray.length;j++){
157
			if(charArray[j]==' '){
158
				break;
159
			}
160
		}
161
		ex = sb.substring(i+24,j-1);
162
		System.out.println("EXECUTION VALUE " + ex);
163
		HttpPost post = new HttpPost("http://selleraccounts.snapdeal.com/login?service=http%3A%2F%2Fseller.snapdeal.com%2Fj_spring_cas_security_check");
164
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
165
		nameValuePairs.add(new BasicNameValuePair("username",
166
		"saholic-snapdeal@saholic.com"));
167
		nameValuePairs.add(new BasicNameValuePair("password",
15223 manish.sha 168
		"saholic15"));
10859 vikram.rag 169
		nameValuePairs.add(new BasicNameValuePair("_eventId","submit"));
170
		nameValuePairs.add(new BasicNameValuePair("execution",ex));
171
		nameValuePairs.add(new BasicNameValuePair("lt",lt));
172
		nameValuePairs.add(new BasicNameValuePair("submit","LOGIN"));
173
		post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
174
		response = httpClient.execute(post);
175
		rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
176
		line = "";
177
		while ((line = rd.readLine()) != null) {
178
			System.out.println(line);
179
		}
180
		get = new HttpGet("http://seller.snapdeal.com/pricing");
181
		response = httpClient.execute(get);
182
		rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
183
		while ((line = rd.readLine()) != null) {
184
			//System.out.println(line);
185
		}
186
		logger.info("----Login Successful----" );
187
	}
188
	public void run()
189
	{
190
		try {
191
			this.updatePricing(this.price,this.supc,this.item,timestamp);
192
			boolean retry =true;
15599 manish.sha 193
			SnapdealSessionCookie sdSessionCookie = new SnapdealSessionCookie();
194
			String cookies = "";
195
			JSONObject cookieObject = null;
10859 vikram.rag 196
			while(retry){
197
				Thread.sleep(2*60*1000);
198
				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 199
				cookies = sdSessionCookie.getCookies();
200
				cookieObject = new JSONObject(cookies);
201
				for(String key:headers.keySet())
202
					get.addHeader(key, headers.get(key));
203
				get.addHeader("Cookie","SERVERID="+cookieObject.get("SERVERID")+";sfJSESSIONID="+cookieObject.get("sfJSESSIONID")+";");
10859 vikram.rag 204
				HttpResponse response = httpClient.execute(get);
205
				BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
206
				String line = "";
207
				while ((line = rd.readLine()) != null) {
208
					logger.info(line);
209
					if(line.contains("\"pendingUpdate\":false")){
210
						retry =false;
211
					}
212
				}
213
			}
214
			GmailUtils mailer = new GmailUtils();
215
			String text = "Product : " +getProductName(this.item) +"\n"+ 
216
			"Item ID : " +this.item.getId() +"\n"+
217
			"SUPC : " +this.supc +"\n"+
218
			"Updated Price : " +this.price;
219
			try{
14951 manish.sha 220
				mailer.sendSSLMessage(sendTo, "Price updated on Snapdeal ( Item ID " + this.item.getId()+ " )",text, emailFromAddress , password,new ArrayList<File>());
10859 vikram.rag 221
			}
222
			catch(Exception e){
223
				logger.info("Exception"+e);
224
			}
225
			ArrayList<Long> updateList = new ArrayList<Long>();
226
			try {
227
				Client catalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
10225 vikram.rag 228
 
10859 vikram.rag 229
				updateList.add(this.item.getId());
230
				catalogClient.updateMarketPlacePriceUpdateStatus(updateList,this.timestamp,OrderSource.SNAPDEAL.getValue());
231
			} catch (TException e) {
232
				try {
233
					new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient().updateMarketPlacePriceUpdateStatus(updateList, timestamp,OrderSource.SNAPDEAL.getValue());
234
				} catch (TTransportException e1) {
235
					e1.printStackTrace();
236
					logger.info("Exception" + e1);
237
				} catch (TException e1) {
238
					e1.printStackTrace();
239
					logger.info("Exception" + e1);
240
				}
241
				logger.info("Exception" + e);
242
			}
243
		} catch (Exception e) {
244
			e.printStackTrace();
245
			//logger.error("Exception" + e);
246
			GmailUtils mailer = new GmailUtils();
247
			String text = "Product       : " +getProductName(this.item) +"\n"+ 
248
			"Item ID       : " +this.item.getId() +"\n"+
249
			"SUPC          : " +this.supc +"\n"+
250
			"Updated Price : " +this.price;
251
			try {
14951 manish.sha 252
				mailer.sendSSLMessage(sendTo, "Failed to update Price on Snapdeal ( Item ID " + this.item.getId()+" )",text, emailFromAddress , password,new ArrayList<File>());
10859 vikram.rag 253
			} catch (MessagingException e1) {
254
				e1.printStackTrace();
255
				logger.info("Exception" + e1);
256
			}
10225 vikram.rag 257
 
10859 vikram.rag 258
		} 
259
	}
260
	String getProductName(Item item){
261
		return getName(item.getBrand())+" " + getName(item.getModelName())+" " + getName(item.getModelNumber())+" " + getName(item.getColor()); 
10225 vikram.rag 262
 
10859 vikram.rag 263
	}
264
	String getName(String name){
265
		if(name==null || name.length()==0){
266
			return "";
267
		}
268
		else{
269
			return name;
270
		}
271
	}
10229 vikram.rag 272
 
273
 
10225 vikram.rag 274
}