Subversion Repositories SmartDukaan

Rev

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