Subversion Repositories SmartDukaan

Rev

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