Subversion Repositories SmartDukaan

Rev

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