Subversion Repositories SmartDukaan

Rev

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