Subversion Repositories SmartDukaan

Rev

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