Subversion Repositories SmartDukaan

Rev

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