Subversion Repositories SmartDukaan

Rev

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