Subversion Repositories SmartDukaan

Rev

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