Subversion Repositories SmartDukaan

Rev

Rev 15022 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
14951 manish.sha 1
package in.shop2020.support.utils;
2
 
3
import in.shop2020.model.v1.catalog.CatalogService.Client;
4
import in.shop2020.model.v1.catalog.CatalogServiceException;
5
import in.shop2020.model.v1.catalog.Item;
6
import in.shop2020.model.v1.order.OrderSource;
15600 manish.sha 7
import in.shop2020.serving.services.SnapdealSessionCookie;
14951 manish.sha 8
import in.shop2020.thrift.clients.CatalogClient;
9
import in.shop2020.utils.GmailUtils;
10
 
11
import java.io.BufferedReader;
12
import java.io.File;
13
import java.io.IOException;
14
import java.io.InputStreamReader;
15
import java.util.ArrayList;
15600 manish.sha 16
import java.util.HashMap;
14951 manish.sha 17
import java.util.List;
15600 manish.sha 18
import java.util.Map;
14951 manish.sha 19
 
20
import javax.mail.MessagingException;
21
 
22
import org.apache.http.HttpResponse;
23
import org.apache.http.NameValuePair;
24
import org.apache.http.client.ClientProtocolException;
25
import org.apache.http.client.entity.UrlEncodedFormEntity;
26
import org.apache.http.client.methods.HttpGet;
27
import org.apache.http.client.methods.HttpPost;
28
import org.apache.http.conn.ClientConnectionManager;
29
import org.apache.http.impl.client.DefaultHttpClient;
30
import org.apache.http.impl.conn.PoolingClientConnectionManager;
31
import org.apache.http.message.BasicNameValuePair;
32
import org.apache.thrift.TException;
33
import org.apache.thrift.transport.TTransportException;
15600 manish.sha 34
import org.json.JSONObject;
14951 manish.sha 35
import org.slf4j.Logger;
36
import org.slf4j.LoggerFactory;
37
 
38
public class UpdateSnapdealVoiPricingUsingPanel extends Thread{
39
	static ClientConnectionManager connManager = new PoolingClientConnectionManager();
40
	private static DefaultHttpClient httpClient = new DefaultHttpClient(connManager);
41
	//private static DefaultHttpClient httpClient = new DefaultHttpClient();
42
	private Float price;
43
	private String supc;
44
	private Item item;
45
	private Long timestamp;
46
	//public String[] sendTo = new String[]{"vikram.raghav@shop2020.in"};
47
	private static Logger logger = LoggerFactory.getLogger(UpdateSDPricingUsingPanel.class);
48
	public String[] sendTo = new String[]{ "sandeep.sachdeva@shop2020.in", "manish.sharma@shop2020.in", "rajneesh.arora@shop2020.in",
49
			"khushal.bhatia@shop2020.in","manoj.kumar@saholic.com","chaitnaya.vats@saholic.com",
50
			"yukti.jain@shop2020.in","chandan.kumar@shop2020.in","ankush.dhingra@shop2020.in","kshitij.sood@shop2020.in"};
51
	public String emailFromAddress = "build@shop2020.in";
52
	public String password = "cafe@nes";
15600 manish.sha 53
	private static Map<String, String> headers = new HashMap<String, String>();
54
	static {
55
		headers.put("User-agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11");
56
		headers.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
57
		headers.put("Accept-Language", "en-US,en;q=0.8");
58
		headers.put("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.3");
59
	}
14951 manish.sha 60
	public UpdateSnapdealVoiPricingUsingPanel(Float price,String supc,Item item,Long timestamp){
61
		logger.info("Calling Update Snapdeal Price VOI Constructor --" + " Price :" +price + " Supc :"+supc +" Item ID:" +item.getId());
62
		this.price = price;
63
		this.supc = supc;
64
		this.item = item;
65
		this.timestamp = timestamp; 
66
	}
15600 manish.sha 67
 
14951 manish.sha 68
	public UpdateSnapdealVoiPricingUsingPanel() {
69
	}
70
	public static void main(String... args) throws ClientProtocolException, IOException, TTransportException, CatalogServiceException, TException{
71
		Item item = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient().getItem(2231);
72
		UpdateSnapdealVoiPricingUsingPanel updatePriceOnSnapdeal = new UpdateSnapdealVoiPricingUsingPanel(22200f,"1108903",item,System.currentTimeMillis());
73
		logger.info("Calling Thread to update price at snapdeal");
74
		updatePriceOnSnapdeal.start();	
75
 
76
	}
15600 manish.sha 77
	int updatePricing(Float price,String supc,Item item,Long timestamp) throws Exception{
15022 manish.sha 78
		logger.info("Calling Update Snapdeal VOI Price Constructor --" + " Price :" +price + " supc :"+supc +" Item ID:" +item.getId());
15600 manish.sha 79
		SnapdealSessionCookie sdSessionCookie = new SnapdealSessionCookie();
80
		String cookies = "";
81
		JSONObject cookieObject = null;
14951 manish.sha 82
		long priceVal = (long)price.floatValue();
83
		HttpPost post = new HttpPost("http://seller.snapdeal.com/pricing/update");
15600 manish.sha 84
		cookies = sdSessionCookie.getVoiCookies();
85
		cookieObject = new JSONObject(cookies);
86
		for(String key:headers.keySet())
87
			post.addHeader(key, headers.get(key));
88
		post.addHeader("Cookie","SERVERID="+cookieObject.get("SERVERID")+";sfJSESSIONID="+cookieObject.get("sfJSESSIONID")+";");
14951 manish.sha 89
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
90
		nameValuePairs.add(new BasicNameValuePair("changedField",
91
                "sellingPrice"));
92
		nameValuePairs.add(new BasicNameValuePair("changedValue",
93
				priceVal+""));
15022 manish.sha 94
		nameValuePairs.add(new BasicNameValuePair("supc",
14951 manish.sha 95
				supc));
96
		post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
97
		HttpResponse response = httpClient.execute(post);
98
		BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
99
		String line = "";
100
		while ((line = rd.readLine()) != null) {
101
			logger.info(line);
102
			System.out.println(line);
103
		}
104
		return 1;
105
	}
106
 
107
	public void run()
108
	{
109
		try {
110
			this.updatePricing(this.price,this.supc,this.item,timestamp);
111
			boolean retry =true;
15600 manish.sha 112
			SnapdealSessionCookie sdSessionCookie = new SnapdealSessionCookie();
113
			String cookies = "";
114
			JSONObject cookieObject = null;
14951 manish.sha 115
			while(retry){
116
				Thread.sleep(2*60*1000);
117
				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");
15600 manish.sha 118
				cookies = sdSessionCookie.getVoiCookies();
119
				cookieObject = new JSONObject(cookies);
120
				for(String key:headers.keySet())
121
					get.addHeader(key, headers.get(key));
122
				get.addHeader("Cookie","SERVERID="+cookieObject.get("SERVERID")+";sfJSESSIONID="+cookieObject.get("sfJSESSIONID")+";");
14951 manish.sha 123
				HttpResponse response = httpClient.execute(get);
124
				BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
125
				String line = "";
126
				while ((line = rd.readLine()) != null) {
127
					logger.info(line);
128
					if(line.contains("\"pendingUpdate\":false")){
129
						retry =false;
130
					}
131
				}
132
			}
133
			GmailUtils mailer = new GmailUtils();
134
			String text = "Product : " +getProductName(this.item) +"\n"+ 
135
			"Item ID : " +this.item.getId() +"\n"+
136
			"SUPC : " +this.supc +"\n"+
137
			"Updated Price : " +this.price;
138
			try{
139
				mailer.sendSSLMessage(sendTo, "Price updated on Snapdeal  VOI( Item ID " + this.item.getId()+ " )",text, emailFromAddress , password,new ArrayList<File>());
140
			}
141
			catch(Exception e){
142
				logger.info("Exception"+e);
143
			}
144
			ArrayList<Long> updateList = new ArrayList<Long>();
145
			try {
146
				Client catalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
147
 
148
				updateList.add(this.item.getId());
149
				catalogClient.updateMarketPlacePriceUpdateStatus(updateList,this.timestamp,OrderSource.SNAPDEAL.getValue());
150
			} catch (TException e) {
151
				try {
152
					new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient().updateMarketPlacePriceUpdateStatus(updateList, timestamp,OrderSource.SNAPDEAL.getValue());
153
				} catch (TTransportException e1) {
154
					e1.printStackTrace();
155
					logger.info("Exception" + e1);
156
				} catch (TException e1) {
157
					e1.printStackTrace();
158
					logger.info("Exception" + e1);
159
				}
160
				logger.info("Exception" + e);
161
			}
162
		} catch (Exception e) {
163
			e.printStackTrace();
164
			//logger.error("Exception" + e);
165
			GmailUtils mailer = new GmailUtils();
166
			String text = "Product       : " +getProductName(this.item) +"\n"+ 
167
			"Item ID       : " +this.item.getId() +"\n"+
168
			"SUPC          : " +this.supc +"\n"+
169
			"Updated Price : " +this.price;
170
			try {
171
				mailer.sendSSLMessage(sendTo, "Failed to update Price on Snapdeal VOI ( Item ID " + this.item.getId()+" )",text, emailFromAddress , password,new ArrayList<File>());
172
			} catch (MessagingException e1) {
173
				e1.printStackTrace();
174
				logger.info("Exception" + e1);
175
			}
176
 
177
		} 
178
	}
179
	String getProductName(Item item){
180
		return getName(item.getBrand())+" " + getName(item.getModelName())+" " + getName(item.getModelNumber())+" " + getName(item.getColor()); 
181
 
182
	}
183
	String getName(String name){
184
		if(name==null || name.length()==0){
185
			return "";
186
		}
187
		else{
188
			return name;
189
		}
190
	}
191
 
192
 
193
}