Subversion Repositories SmartDukaan

Rev

Rev 13756 | Rev 13761 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
10049 vikram.rag 1
package in.shop2020;
2
 
3
import in.shop2020.model.v1.catalog.CatalogService.Client;
4
import in.shop2020.model.v1.catalog.FlipkartItem;
5
import in.shop2020.model.v1.catalog.Item;
6
import in.shop2020.model.v1.catalog.MarketPlaceItemPrice;
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.io.UnsupportedEncodingException;
11444 vikram.rag 15
import java.nio.charset.Charset;
10049 vikram.rag 16
import java.util.ArrayList;
17
import java.util.HashMap;
18
import java.util.List;
19
import java.util.Map;
20
import javax.mail.MessagingException;
11444 vikram.rag 21
 
22
import org.apache.commons.codec.binary.Base64;
23
import org.apache.http.HttpHeaders;
10049 vikram.rag 24
import org.apache.http.HttpResponse;
25
import org.apache.http.auth.AuthScope;
26
import org.apache.http.auth.UsernamePasswordCredentials;
27
import org.apache.http.client.ClientProtocolException;
28
import org.apache.http.client.methods.HttpPost;
29
import org.apache.http.entity.StringEntity;
30
import org.apache.http.impl.client.DefaultHttpClient;
31
import org.apache.thrift.TException;
32
import org.apache.thrift.transport.TTransportException;
33
 
34
 
35
 
36
public class CreateSubmitFlipkartPricingFeed {
10196 vikram.rag 37
	private static long time = System.currentTimeMillis();
10202 vikram.rag 38
	private static List<MarketPlaceItemPrice> flipkartPricingItems = new ArrayList<MarketPlaceItemPrice>();
10049 vikram.rag 39
	private static Map<Long,Item> aliveItemsMap = new HashMap<Long,Item>();
40
	private static Map<Long,FlipkartItem> flipkartItemsMap = new HashMap<Long,FlipkartItem>();
10196 vikram.rag 41
	static List<Long> updatedItems = new ArrayList<Long>();
10049 vikram.rag 42
	static java.text.SimpleDateFormat sdf;
43
	static String emailFromAddress;
44
	static String password;
45
	static GmailUtils mailer;
46
	static String sendTo[];
10202 vikram.rag 47
	private static List<MarketPlaceItemPrice> flipkartPricingUpdateItems;
10049 vikram.rag 48
	static
49
	{
50
		sdf = new java.text.SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
51
		emailFromAddress = "build@shop2020.in";
52
		password = "cafe@nes";
53
		mailer = new GmailUtils();
10213 vikram.rag 54
		//sendTo = new String[]{"vikram.raghav@shop2020.in"};
13757 manish.sha 55
		sendTo = new String[]{ "sandeep.sachdeva@shop2020.in", "manish.sharma@shop2020.in", "rajneesh.arora@shop2020.in",
10049 vikram.rag 56
				"khushal.bhatia@shop2020.in","manoj.kumar@saholic.com","chaitnaya.vats@saholic.com",
11447 vikram.rag 57
				"yukti.jain@shop2020.in","manisha.sharma@shop2020.in","chandan.kumar@shop2020.in","ankush.dhingra@shop2020.in","anikendra.das@shop2020.in"};
10197 vikram.rag 58
		Client catalogServiceClient = null;
59
		try {
60
			catalogServiceClient = new CatalogClient().getClient();
61
		} catch (TTransportException e) {
62
			e.printStackTrace();
63
		}
64
		try {
65
			time = System.currentTimeMillis();
10202 vikram.rag 66
			flipkartPricingUpdateItems = catalogServiceClient.getMarketPlaceItemsForPriceUpdate(8);
67
			for(MarketPlaceItemPrice flipkartPricingUpdateItem:flipkartPricingUpdateItems){
68
				if(flipkartPricingUpdateItem.getLastUpdatedOn() > flipkartPricingUpdateItem.getLastUpdatedOnMarketplace() && !flipkartPricingUpdateItem.isIsPriceOverride() && flipkartPricingUpdateItem.isIsListedOnSource() && flipkartPricingUpdateItem.getSellingPrice()!=0){
69
					flipkartPricingItems.add(flipkartPricingUpdateItem);
70
				}
71
			}
10199 vikram.rag 72
			List<FlipkartItem> flipkartItems = null;
73
			boolean retry = true;
74
			while(retry){
75
				try{
76
					flipkartItems = catalogServiceClient.getAllFlipkartItems();
77
					retry = false;
78
				}
79
				catch(Exception e){
80
					catalogServiceClient = new CatalogClient().getClient();
81
				}
82
			}
10049 vikram.rag 83
			for(FlipkartItem flipkartItem:flipkartItems){
84
				flipkartItemsMap.put(flipkartItem.getItem_id(),flipkartItem);
85
			}
10200 vikram.rag 86
			retry = true;
87
			List<Item> aliveItems = null;
88
			while(retry){
89
				try{
90
					aliveItems = catalogServiceClient.getAllAliveItems();
91
					retry = false;
92
				}
93
				catch(Exception e){
94
					catalogServiceClient = new CatalogClient().getClient();
95
				}
96
			}
10197 vikram.rag 97
			for(in.shop2020.model.v1.catalog.Item thriftItem:aliveItems){
98
				aliveItemsMap.put(thriftItem.getId(), thriftItem);
99
			}
10049 vikram.rag 100
 
10197 vikram.rag 101
		} catch (TException e) {
102
			e.printStackTrace();
103
		}
104
		if(flipkartPricingItems.size()==0){
105
			String text = "";
106
			try {
107
				mailer.sendSSLMessage(sendTo,"No Change in Flipkart item Prices ", emailFromAddress, password, text);
108
				System.exit(0);
109
			} catch (MessagingException e) {
110
				e.printStackTrace();
111
			}
112
		}
113
 
10049 vikram.rag 114
	}
115
 
116
	public static void main(String[] args) throws ClientProtocolException, TException{
117
		DefaultHttpClient httpclient = new DefaultHttpClient();
11444 vikram.rag 118
		//httpclient = (DefaultHttpClient) WebClientWrapper.wrapClient(httpclient);
10206 vikram.rag 119
		//httpclient.getCredentialsProvider().setCredentials(
120
		//		new AuthScope("sandbox-api.flipkart.net", 443),
121
		//		new UsernamePasswordCredentials("og3yg5994bxbxxc1", "8a137068-417c-4e4f-ae95-9295ab938d88"));/// Test Server Access Keys
11444 vikram.rag 122
		//httpclient.getCredentialsProvider().setCredentials(
123
		//		new AuthScope("api.flipkart.net", 443),
124
		//		new UsernamePasswordCredentials("m2z93iskuj81qiid","0c7ab6a5-98c0-4cdc-8be3-72c591e0add4")); //Prod Server Access Keys
125
 
10206 vikram.rag 126
		HttpPost httppost = new HttpPost("https://api.flipkart.net/sellers/skus/listings/bulk"); // PROD
11444 vikram.rag 127
 
128
		String auth = "m2z93iskuj81qiid"+":"+"0c7ab6a5-98c0-4cdc-8be3-72c591e0add4";
129
		byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("US-ASCII")));
130
		String authHeader = "Basic " + new String(encodedAuth);
131
		httppost.setHeader(HttpHeaders.AUTHORIZATION, authHeader);
10206 vikram.rag 132
		//HttpPost httppost = new HttpPost("https://sandbox-api.flipkart.net/sellers/skus/listings/bulk"); // TEST
10049 vikram.rag 133
		StringBuffer jsonRequest = new StringBuffer();
134
		StringBuffer jsonStart = new StringBuffer();
135
		jsonStart = jsonStart.append("{\"listings\":[");
136
		StringBuffer jsonEnd = new StringBuffer();
137
		jsonEnd.append("]}");
138
		//System.out.println("JSON request  " + jsonRequest);
139
		int i=0;
140
		StringBuffer pricingItems =new StringBuffer();
141
		String tableHeader = "<html><table border=\"1\" align=\"center\"><tr>" 
142
			+ "<td><b>" + "Product Name" +"</b></td>"
143
			+"<td><b>" + "Item ID" + "</b></td>"
144
			+"<td><b>" + "Selling Price" + "</b></td>"
145
			+"</tr>";
146
		String tableFooter = "</table></html>";
147
		Item item;
148
		StringBuffer feedResponse = null;
149
		for(MarketPlaceItemPrice entry:flipkartPricingItems){
10196 vikram.rag 150
			updatedItems.add(entry.getItem_id());
10049 vikram.rag 151
			feedResponse=  new StringBuffer();
11152 vikram.rag 152
			if(aliveItemsMap.containsKey(entry.getItem_id())){
153
				item = aliveItemsMap.get(entry.getItem_id());	
154
			}
155
			else{
156
				continue;
157
			}
10049 vikram.rag 158
			System.out.println("Item ID  " + entry.getItem_id());
159
			System.out.println("Product Name " + getProductName(item));
160
			System.out.println("Selling Price " + entry.getSellingPrice());
161
			jsonRequest.append("{\"skuId\":"+"\""+flipkartItemsMap.get(entry.getItem_id()).getSkuAtFlipkart()+
162
					"\","+"\"attributeValues\""+":"+
13757 manish.sha 163
					"{\"selling_price\""+":"+"\""+ new Double(entry.getSellingPrice()).intValue()+"\""+"},"+"\"listingValidations\":" +
164
			        "{\"PRICE_ERROR_CHECK\":" +
165
			        "\"disable\"}"+"}");
10197 vikram.rag 166
 
10049 vikram.rag 167
			pricingItems.append("<tr>" 
168
					+ "<td>" +getProductName(item)+"</td>"
169
					+"<td>" + item.getId() + "</td>"
170
					+"<td>" + entry.getSellingPrice() + "</td>"
171
					+"</tr>");
10211 vikram.rag 172
			i++;
10212 vikram.rag 173
			if(flipkartPricingItems.size()!=1 && (i!=flipkartPricingItems.size()) ){
174
				if(i%10!=0){
175
					jsonRequest.append(",");
176
				}
177
 
10049 vikram.rag 178
			}
10201 vikram.rag 179
			System.out.println("Value of i " + i + " FlipkartPricing Items List size " + flipkartPricingItems.size());
10049 vikram.rag 180
			if(i%10==0 || i==flipkartPricingItems.size()){
10201 vikram.rag 181
				System.out.println("Inside send code block");
10049 vikram.rag 182
				StringEntity input = null;
183
				try {
184
					input = new StringEntity(jsonStart+jsonRequest.toString()+jsonEnd);
185
					System.out.println("Json input " + jsonStart+jsonRequest.toString()+jsonEnd);
186
					jsonRequest = new StringBuffer();
187
				} catch (UnsupportedEncodingException e) {
188
					e.printStackTrace();
189
				}
190
				input.setContentType("application/json");
191
 
192
				httppost.setEntity(input);
193
				HttpResponse response = null;
194
				try {
195
					System.out.println("Before send " );
196
					response = httpclient.execute(httppost);
197
					System.out.println("After send " );
198
				} catch (IOException e) {
199
					e.printStackTrace();
200
				}
201
				BufferedReader rd = null;
202
				try {
203
					rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
204
				} catch (IllegalStateException e) {
205
					e.printStackTrace();
206
				} catch (IOException e) {
207
					e.printStackTrace();
208
				}
13756 manish.sha 209
				feedResponse.append("");
10049 vikram.rag 210
				String line;
211
				try {
212
					while ((line = rd.readLine()) != null) {
213
						System.out.println("Response " + line);
214
						if(line.equalsIgnoreCase("{\"status\":\"success\"}")){
215
						}
216
						else{
217
							feedResponse.append(line);
218
						}
219
 
220
					}
221
				} catch (IOException e) {
222
					e.printStackTrace();
223
				}
224
			}
225
		}
226
		System.out.println("Before sending email");
227
		String text = tableHeader+pricingItems.toString()+tableFooter;
228
		try {
10196 vikram.rag 229
			mailer.sendSSLMessage(sendTo,"Flipkart Pricing Sent Details ", emailFromAddress, password, text);
10049 vikram.rag 230
		} catch (MessagingException e) {
231
			e.printStackTrace();
232
		}
10196 vikram.rag 233
		Client catalogClient = null;
10049 vikram.rag 234
		if(feedResponse.toString().length()>0){
235
			try {
10196 vikram.rag 236
				mailer.sendSSLMessage(sendTo, "Flipkart Pricing could not be updated ", feedResponse.toString(), emailFromAddress, password,new ArrayList<File>());
10204 vikram.rag 237
			} catch (MessagingException e) {
238
				e.printStackTrace();
239
			}
240
		}
241
		else{
242
			try {
243
				mailer.sendSSLMessage(sendTo, "Flipkart Pricing updated successfully ", feedResponse.toString(), emailFromAddress, password,new ArrayList<File>());
10196 vikram.rag 244
				try {
10197 vikram.rag 245
					catalogClient = new CatalogClient().getClient();
246
					catalogClient.updateMarketPlacePriceUpdateStatus(updatedItems,time,8);
10196 vikram.rag 247
				} catch (TException e) {
248
					try {
249
						new CatalogClient().getClient().updateMarketPlacePriceUpdateStatus(updatedItems, time,8);
250
					} catch (TTransportException e1) {
251
						e1.printStackTrace();
252
					} catch (TException e1) {
253
						e1.printStackTrace();
254
					}
255
					e.printStackTrace();
256
				}
10197 vikram.rag 257
 
10049 vikram.rag 258
			} catch (MessagingException e) {
10196 vikram.rag 259
				try {
10197 vikram.rag 260
					catalogClient = new CatalogClient().getClient();
261
					catalogClient.updateMarketPlacePriceUpdateStatus(updatedItems,time,8);
10196 vikram.rag 262
				} catch (TException ex) {
263
					try {
264
						new CatalogClient().getClient().updateMarketPlacePriceUpdateStatus(updatedItems, time,8);
265
					} catch (TTransportException e1) {
266
						e1.printStackTrace();
267
					} catch (TException e1) {
268
						e1.printStackTrace();
269
					}
270
					ex.printStackTrace();
271
				}
10049 vikram.rag 272
				e.printStackTrace();
273
			}
274
		}
275
	}
276
 
277
	static String getProductName(Item item){
278
		return getName(item.getBrand())+" " + getName(item.getModelName())+" " + getName(item.getModelNumber())+" " + getName(item.getColor()); 
279
 
280
	}
281
	static String getName(String name){
282
		if(name==null || name.length()==0){
283
			return "";
284
		}
285
		else{
286
			return name;
287
		}
288
	}
289
}