Subversion Repositories SmartDukaan

Rev

Rev 10225 | Rev 10229 | 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;
4
import in.shop2020.model.v1.catalog.Item;
10227 vikram.rag 5
import in.shop2020.support.controllers.SnapdealListController;
10225 vikram.rag 6
import in.shop2020.thrift.clients.CatalogClient;
7
import in.shop2020.utils.GmailUtils;
8
 
9
import java.io.BufferedReader;
10
import java.io.File;
11
import java.io.IOException;
12
import java.io.InputStreamReader;
13
import java.io.UnsupportedEncodingException;
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;
25
import org.apache.http.impl.client.DefaultHttpClient;
26
import org.apache.http.message.BasicNameValuePair;
27
import org.apache.thrift.TException;
28
import org.apache.thrift.transport.TTransportException;
10227 vikram.rag 29
import org.slf4j.Logger;
30
import org.slf4j.LoggerFactory;
10225 vikram.rag 31
 
32
public class UpdateSDPricingUsingPanel extends Thread{
33
	private static DefaultHttpClient client = new DefaultHttpClient();
34
	private Float price;
35
	private String supc;
36
	private Item item;
37
	private Long timestamp;
38
	public String[] sendTo = new String[]{"vikram.raghav@shop2020.in"};
10227 vikram.rag 39
	private static Logger logger = LoggerFactory.getLogger(UpdateSDPricingUsingPanel.class);
10225 vikram.rag 40
	/*public String[] sendTo = new String[]{ "sandeep.sachdeva@shop2020.in", "vikram.raghav@shop2020.in", "rajneesh.arora@shop2020.in",
41
			"khushal.bhatia@shop2020.in","manoj.kumar@saholic.com","chaitnaya.vats@saholic.com",
42
			"yukti.jain@shop2020.in","chandan.kumar@shop2020.in","ankush.dhingra@shop2020.in"};
43
	*/public String emailFromAddress = "build@shop2020.in";
44
	public String password = "cafe@nes";
45
	public UpdateSDPricingUsingPanel(Float price,String supc,Item item,Long timestamp){
10227 vikram.rag 46
		logger.info("Calling Update Snapdeal Price Constructor --" + " Price :" +price + " Supc :"+supc +" Item ID:" +item.getId());
10225 vikram.rag 47
		this.price = price;
48
		this.supc = supc;
49
		this.item = item;
50
		this.timestamp = timestamp; 
51
	}
52
	public UpdateSDPricingUsingPanel() {
53
	}
54
	public static void main(String... args) throws ClientProtocolException, IOException{
55
		UpdateSDPricingUsingPanel UDSI = new UpdateSDPricingUsingPanel();
56
		//UDSI.updatePricing(10131f,"SDL668125910","");
57
 
58
	}
59
	int updatePricing(Float price,String supc,Item item,Long timestamp) throws ClientProtocolException, IOException{
10227 vikram.rag 60
		logger.info("Calling Update Snapdeal Price Constructor --" + " Price :" +price + " Supc :"+supc +" Item ID:" +item.getId());
10225 vikram.rag 61
		HttpPost post = new HttpPost("http://seller.snapdeal.com/pricing/update");
62
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
63
		nameValuePairs.add(new BasicNameValuePair("sellingPrice",
64
				String.valueOf(price)));
65
		nameValuePairs.add(new BasicNameValuePair("id",
66
				supc));
67
		nameValuePairs.add(new BasicNameValuePair("oper","edit"));
68
		post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
69
		HttpResponse response = client.execute(post);
70
		BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
71
		String line = "";
72
		while ((line = rd.readLine()) != null) {
73
			System.out.println(line);
74
		}
75
		return 1;
76
	}
77
 
78
	public void handleLogin() throws ClientProtocolException, IOException{
10227 vikram.rag 79
		logger.info("----Inside Handle Login ----" );
10225 vikram.rag 80
		HttpGet get = new HttpGet("http://selleraccounts.snapdeal.com/keymaker/login");
81
		HttpResponse response = null;
82
		try {
83
			response = client.execute(get);
84
		} catch (ClientProtocolException e) {
85
			e.printStackTrace();
86
		} catch (IOException e) {
87
			e.printStackTrace();
88
		}
89
		BufferedReader rd = null;
90
		try {
91
			rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
92
		} catch (IllegalStateException e1) {
93
			e1.printStackTrace();
94
		} catch (IOException e1) {
95
			e1.printStackTrace();
96
		}
97
		String line = "";
98
		StringBuffer sb = new StringBuffer();
99
		try {
100
			while ((line = rd.readLine()) != null) {
101
				sb.append(line);
102
			}
103
		} catch (IOException e) {
104
			e.printStackTrace();
105
		}
106
		int i= sb.toString().indexOf("name=\"lt\" value=");
107
		char[] charArray = sb.toString().toCharArray();
108
		String lt = "";
109
		int j=0;
110
		for(j=i+16;j<=charArray.length;j++){
111
 
112
			if(charArray[j]==' '){
113
				break;
114
			}
115
		}
116
		lt = sb.substring(i+17,j-1);
117
		System.out.println("LT VALUE " + lt);
118
		i= sb.toString().indexOf("name=\"execution\" value=");
119
		charArray = sb.toString().toCharArray();
120
		String ex = "";
121
		j=0;
122
		for(j=i+24;j<=charArray.length;j++){
123
			if(charArray[j]==' '){
124
				break;
125
			}
126
		}
127
		ex = sb.substring(i+24,j-1);
128
		System.out.println("EXECUTION VALUE " + ex);
129
		HttpPost post = new HttpPost("http://selleraccounts.snapdeal.com/login?service=http%3A%2F%2Fseller.snapdeal.com%2Fj_spring_cas_security_check");
130
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
131
		//nameValuePairs.add(new BasicNameValuePair("username",
132
		//"khushal.bhatia@saholic.com"));
133
		nameValuePairs.add(new BasicNameValuePair("username",
134
		"saholic-snapdeal@saholic.com"));
135
		//nameValuePairs.add(new BasicNameValuePair("password",
136
		//"sonline"));
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
		}
10227 vikram.rag 156
		logger.info("Login Successful");
10225 vikram.rag 157
	}
158
	public void run()
159
    {
160
		try {
161
			this.handleLogin();
162
			this.updatePricing(this.price,this.supc,this.item,timestamp);
163
			GmailUtils mailer = new GmailUtils();
164
			String text = "Product       : " +getProductName(this.item) +"\n"+ 
165
						  "Item ID       : " +this.item.getId() +"\n"+
166
						  "SUPC          : " +this.supc +"\n"+
167
						  "Updated Price : " +this.price;
168
			mailer.sendSSLMessage(sendTo, "Price updated on Snapdeal Item ID " + this.item.getId(),text, emailFromAddress , password,new ArrayList<File>());
169
			ArrayList<Long> updateList = new ArrayList<Long>();
170
			try {
171
				Client catalogClient = new CatalogClient().getClient();
172
 
173
				updateList.add(this.item.getId());
174
				catalogClient.updateMarketPlacePriceUpdateStatus(updateList,this.timestamp,7L);
175
			} catch (TException e) {
176
				try {
177
					new CatalogClient().getClient().updateMarketPlacePriceUpdateStatus(updateList, timestamp,8);
178
				} catch (TTransportException e1) {
179
					e1.printStackTrace();
180
				} catch (TException e1) {
181
					e1.printStackTrace();
182
				}
183
				e.printStackTrace();
184
			}
185
		} catch (Exception e) {
186
			e.printStackTrace();
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, "Failed to update Price on Snapdeal Item ID " + this.item.getId(),text, emailFromAddress , password,new ArrayList<File>());
194
			} catch (MessagingException e1) {
195
				e1.printStackTrace();
196
			}
197
 
198
		} 
199
    }
200
	String getProductName(Item item){
201
		return getName(item.getBrand())+" " + getName(item.getModelName())+" " + getName(item.getModelNumber())+" " + getName(item.getColor()); 
202
 
203
	}
204
	String getName(String name){
205
		if(name==null || name.length()==0){
206
			return "";
207
		}
208
		else{
209
			return name;
210
		}
211
	}
212
 
213
 
214
}