Subversion Repositories SmartDukaan

Rev

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