Subversion Repositories SmartDukaan

Rev

Rev 10251 | Rev 10255 | 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{
10250 vikram.rag 34
	private static DefaultHttpClient httpClient = new DefaultHttpClient();
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"));
10250 vikram.rag 72
		 HttpResponse response = httpClient.execute(post);
10229 vikram.rag 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 {
10250 vikram.rag 86
			 response = httpClient.execute(get);
10229 vikram.rag 87
		 } catch (ClientProtocolException e) {
88
			 e.printStackTrace();
10252 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"));
10250 vikram.rag 145
		 response = httpClient.execute(post);
10229 vikram.rag 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");
10250 vikram.rag 152
		 response = httpClient.execute(get);
10229 vikram.rag 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 {
162
			 this.handleLogin();
163
			 this.updatePricing(this.price,this.supc,this.item,timestamp);
10238 vikram.rag 164
			 boolean retry =true;
165
			 while(retry){
166
				 Thread.sleep(2*60*1000);
167
				 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 168
				 HttpResponse response = httpClient.execute(get);
10238 vikram.rag 169
				 BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
170
				 String line = "";
171
				 while ((line = rd.readLine()) != null) {
172
					 logger.info(line);
173
					 if(line.contains("\"pendingUpdate\":false\"")){
174
						 retry =false;
175
					 }
176
				 }
10231 vikram.rag 177
			 }
10229 vikram.rag 178
			 GmailUtils mailer = new GmailUtils();
179
			 String text = "Product       : " +getProductName(this.item) +"\n"+ 
180
			 "Item ID       : " +this.item.getId() +"\n"+
181
			 "SUPC          : " +this.supc +"\n"+
182
			 "Updated Price : " +this.price;
183
			 mailer.sendSSLMessage(sendTo, "Price updated on Snapdeal Item ID " + this.item.getId(),text, emailFromAddress , password,new ArrayList<File>());
184
			 ArrayList<Long> updateList = new ArrayList<Long>();
185
			 try {
186
				 Client catalogClient = new CatalogClient().getClient();
10225 vikram.rag 187
 
10229 vikram.rag 188
				 updateList.add(this.item.getId());
189
				 catalogClient.updateMarketPlacePriceUpdateStatus(updateList,this.timestamp,7L);
190
			 } catch (TException e) {
191
				 try {
192
					 new CatalogClient().getClient().updateMarketPlacePriceUpdateStatus(updateList, timestamp,8);
193
				 } catch (TTransportException e1) {
194
					 e1.printStackTrace();
10246 vikram.rag 195
					 logger.info("Exception" + e1);
10229 vikram.rag 196
				 } catch (TException e1) {
197
					 e1.printStackTrace();
10246 vikram.rag 198
					 logger.info("Exception" + e1);
10229 vikram.rag 199
				 }
10246 vikram.rag 200
				 logger.info("Exception" + e);
10229 vikram.rag 201
			 }
202
		 } catch (Exception e) {
203
			 e.printStackTrace();
10248 vikram.rag 204
			 //logger.error("Exception" + e);
10229 vikram.rag 205
			 GmailUtils mailer = new GmailUtils();
206
			 String text = "Product       : " +getProductName(this.item) +"\n"+ 
207
			 "Item ID       : " +this.item.getId() +"\n"+
208
			 "SUPC          : " +this.supc +"\n"+
209
			 "Updated Price : " +this.price;
210
			 try {
211
				 mailer.sendSSLMessage(sendTo, "Failed to update Price on Snapdeal Item ID " + this.item.getId(),text, emailFromAddress , password,new ArrayList<File>());
212
			 } catch (MessagingException e1) {
213
				 e1.printStackTrace();
10246 vikram.rag 214
				 logger.info("Exception" + e1);
10229 vikram.rag 215
			 }
10225 vikram.rag 216
 
10229 vikram.rag 217
		 } 
218
	 }
219
	 String getProductName(Item item){
220
		 return getName(item.getBrand())+" " + getName(item.getModelName())+" " + getName(item.getModelNumber())+" " + getName(item.getColor()); 
10225 vikram.rag 221
 
10229 vikram.rag 222
	 }
223
	 String getName(String name){
224
		 if(name==null || name.length()==0){
225
			 return "";
226
		 }
227
		 else{
228
			 return name;
229
		 }
230
	 }
231
 
232
 
10225 vikram.rag 233
}