Subversion Repositories SmartDukaan

Rev

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