Subversion Repositories SmartDukaan

Rev

Rev 10244 | Rev 10246 | 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();
89
		 } catch (IOException e) {
90
			 e.printStackTrace();
91
		 }
92
		 BufferedReader rd = null;
93
		 try {
94
			 rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
95
		 } catch (IllegalStateException e1) {
96
			 e1.printStackTrace();
97
		 } catch (IOException e1) {
98
			 e1.printStackTrace();
99
		 }
100
		 String line = "";
101
		 StringBuffer sb = new StringBuffer();
102
		 try {
103
			 while ((line = rd.readLine()) != null) {
104
				 sb.append(line);
105
			 }
106
		 } catch (IOException e) {
107
			 e.printStackTrace();
108
		 }
109
		 int i= sb.toString().indexOf("name=\"lt\" value=");
110
		 char[] charArray = sb.toString().toCharArray();
111
		 String lt = "";
112
		 int j=0;
113
		 for(j=i+16;j<=charArray.length;j++){
10225 vikram.rag 114
 
10229 vikram.rag 115
			 if(charArray[j]==' '){
116
				 break;
117
			 }
118
		 }
119
		 lt = sb.substring(i+17,j-1);
120
		 System.out.println("LT VALUE " + lt);
121
		 i= sb.toString().indexOf("name=\"execution\" value=");
122
		 charArray = sb.toString().toCharArray();
123
		 String ex = "";
124
		 j=0;
125
		 for(j=i+24;j<=charArray.length;j++){
126
			 if(charArray[j]==' '){
127
				 break;
128
			 }
129
		 }
130
		 ex = sb.substring(i+24,j-1);
131
		 System.out.println("EXECUTION VALUE " + ex);
132
		 HttpPost post = new HttpPost("http://selleraccounts.snapdeal.com/login?service=http%3A%2F%2Fseller.snapdeal.com%2Fj_spring_cas_security_check");
133
		 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
134
		 nameValuePairs.add(new BasicNameValuePair("username",
135
		 "saholic-snapdeal@saholic.com"));
136
		 nameValuePairs.add(new BasicNameValuePair("password",
137
		 "bc452ce4"));
138
		 nameValuePairs.add(new BasicNameValuePair("_eventId","submit"));
139
		 nameValuePairs.add(new BasicNameValuePair("execution",ex));
140
		 nameValuePairs.add(new BasicNameValuePair("lt",lt));
141
		 nameValuePairs.add(new BasicNameValuePair("submit","LOGIN"));
142
		 post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
143
		 response = client.execute(post);
144
		 rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
145
		 line = "";
146
		 while ((line = rd.readLine()) != null) {
147
			 System.out.println(line);
148
		 }
149
		 get = new HttpGet("http://seller.snapdeal.com/pricing");
150
		 response = client.execute(get);
151
		 rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
152
		 while ((line = rd.readLine()) != null) {
153
			 //System.out.println(line);
154
		 }
155
		 logger.info("----Login Successful----" );
156
	 }
157
	 public void run()
158
	 {
159
		 try {
10240 vikram.rag 160
			 client = new DefaultHttpClient();
10229 vikram.rag 161
			 this.handleLogin();
162
			 this.updatePricing(this.price,this.supc,this.item,timestamp);
10238 vikram.rag 163
			 boolean retry =true;
164
			 while(retry){
165
				 Thread.sleep(2*60*1000);
166
				 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");
167
				 HttpResponse response = client.execute(get);
168
				 BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
169
				 String line = "";
170
				 while ((line = rd.readLine()) != null) {
171
					 logger.info(line);
172
					 if(line.contains("\"pendingUpdate\":false\"")){
173
						 retry =false;
174
					 }
175
				 }
10231 vikram.rag 176
			 }
10229 vikram.rag 177
			 GmailUtils mailer = new GmailUtils();
178
			 String text = "Product       : " +getProductName(this.item) +"\n"+ 
179
			 "Item ID       : " +this.item.getId() +"\n"+
180
			 "SUPC          : " +this.supc +"\n"+
181
			 "Updated Price : " +this.price;
182
			 mailer.sendSSLMessage(sendTo, "Price updated on Snapdeal Item ID " + this.item.getId(),text, emailFromAddress , password,new ArrayList<File>());
183
			 ArrayList<Long> updateList = new ArrayList<Long>();
184
			 try {
185
				 Client catalogClient = new CatalogClient().getClient();
10225 vikram.rag 186
 
10229 vikram.rag 187
				 updateList.add(this.item.getId());
188
				 catalogClient.updateMarketPlacePriceUpdateStatus(updateList,this.timestamp,7L);
189
			 } catch (TException e) {
190
				 try {
191
					 new CatalogClient().getClient().updateMarketPlacePriceUpdateStatus(updateList, timestamp,8);
192
				 } catch (TTransportException e1) {
193
					 e1.printStackTrace();
10243 vikram.rag 194
					 logger.info("Exception" + e1.getMessage());
10229 vikram.rag 195
				 } catch (TException e1) {
196
					 e1.printStackTrace();
10243 vikram.rag 197
					 logger.info("Exception" + e1.getMessage());
10229 vikram.rag 198
				 }
10243 vikram.rag 199
				 logger.info("Exception" + e.getMessage());
10229 vikram.rag 200
			 }
201
		 } catch (Exception e) {
202
			 e.printStackTrace();
10245 vikram.rag 203
			 logger.info("Exception" + e.getStackTrace());
10229 vikram.rag 204
			 GmailUtils mailer = new GmailUtils();
205
			 String text = "Product       : " +getProductName(this.item) +"\n"+ 
206
			 "Item ID       : " +this.item.getId() +"\n"+
207
			 "SUPC          : " +this.supc +"\n"+
208
			 "Updated Price : " +this.price;
209
			 try {
210
				 mailer.sendSSLMessage(sendTo, "Failed to update Price on Snapdeal Item ID " + this.item.getId(),text, emailFromAddress , password,new ArrayList<File>());
211
			 } catch (MessagingException e1) {
212
				 e1.printStackTrace();
10243 vikram.rag 213
				 logger.info("Exception" + e1.getMessage());
10229 vikram.rag 214
			 }
10225 vikram.rag 215
 
10229 vikram.rag 216
		 } 
217
	 }
218
	 String getProductName(Item item){
219
		 return getName(item.getBrand())+" " + getName(item.getModelName())+" " + getName(item.getModelNumber())+" " + getName(item.getColor()); 
10225 vikram.rag 220
 
10229 vikram.rag 221
	 }
222
	 String getName(String name){
223
		 if(name==null || name.length()==0){
224
			 return "";
225
		 }
226
		 else{
227
			 return name;
228
		 }
229
	 }
230
 
231
 
10225 vikram.rag 232
}