| 10268 |
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.CatalogServiceException;
|
|
|
5 |
import in.shop2020.model.v1.catalog.Item;
|
|
|
6 |
import in.shop2020.thrift.clients.CatalogClient;
|
|
|
7 |
import in.shop2020.utils.GmailUtils;
|
| 12827 |
kshitij.so |
8 |
import in.shop2020.serving.services.FlipkartPricingPannel;
|
| 10268 |
vikram.rag |
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;
|
| 11445 |
vikram.rag |
15 |
import java.nio.charset.Charset;
|
| 10268 |
vikram.rag |
16 |
import java.util.ArrayList;
|
| 11445 |
vikram.rag |
17 |
|
|
|
18 |
import org.apache.commons.codec.binary.Base64;
|
|
|
19 |
import org.apache.http.HttpHeaders;
|
| 10268 |
vikram.rag |
20 |
import org.apache.http.HttpResponse;
|
|
|
21 |
import org.apache.http.auth.AuthScope;
|
|
|
22 |
import org.apache.http.auth.UsernamePasswordCredentials;
|
|
|
23 |
import org.apache.http.client.ClientProtocolException;
|
|
|
24 |
import org.apache.http.client.methods.HttpPost;
|
|
|
25 |
import org.apache.http.conn.ClientConnectionManager;
|
|
|
26 |
import org.apache.http.entity.StringEntity;
|
|
|
27 |
import org.apache.http.impl.client.DefaultHttpClient;
|
|
|
28 |
import org.apache.http.impl.conn.PoolingClientConnectionManager;
|
|
|
29 |
import org.apache.thrift.TException;
|
|
|
30 |
import org.apache.thrift.transport.TTransportException;
|
| 10316 |
kshitij.so |
31 |
import org.json.JSONException;
|
|
|
32 |
import org.json.JSONObject;
|
| 10268 |
vikram.rag |
33 |
import org.slf4j.Logger;
|
|
|
34 |
import org.slf4j.LoggerFactory;
|
|
|
35 |
|
|
|
36 |
public class UpdateFlipkartPricing extends Thread{
|
|
|
37 |
private Float price;
|
|
|
38 |
private String fksku;
|
|
|
39 |
private Item item;
|
|
|
40 |
private Long timestamp;
|
| 10708 |
kshitij.so |
41 |
//public String[] sendTo = new String[]{"vikram.raghav@shop2020.in"};
|
| 10268 |
vikram.rag |
42 |
private static Logger logger = LoggerFactory.getLogger(UpdateSDPricingUsingPanel.class);
|
| 20247 |
amit.gupta |
43 |
public String[] sendTo = new String[]{"rajneesh.arora@shop2020.in", "khushal.bhatia@shop2020.in","manoj.kumar@saholic.com","kshitij.sood@shop2020.in","amit.gupta@shop2020.in"};
|
| 11445 |
vikram.rag |
44 |
public String emailFromAddress = "build@shop2020.in";
|
|
|
45 |
public String password = "cafe@nes";
|
|
|
46 |
public UpdateFlipkartPricing(Float price,String fksku,Item item,Long timestamp){
|
|
|
47 |
logger.info("Calling Update Snapdeal Price Constructor --" + " Price :" +price + " SKU at flipkart :"+fksku +" Item ID:" +item.getId());
|
|
|
48 |
this.price = price;
|
|
|
49 |
this.fksku = fksku;
|
|
|
50 |
this.item = item;
|
|
|
51 |
this.timestamp = timestamp;
|
|
|
52 |
}
|
|
|
53 |
public UpdateFlipkartPricing() {
|
|
|
54 |
}
|
|
|
55 |
public static void main(String... args) throws ClientProtocolException, IOException, TTransportException, CatalogServiceException, TException{
|
|
|
56 |
Item item = new CatalogClient().getClient().getItem(2231);
|
|
|
57 |
UpdateFlipkartPricing updateFlipkartPricing = new UpdateFlipkartPricing(22200f,"1108903",item,System.currentTimeMillis());
|
|
|
58 |
logger.info("Calling Thread to update price at snapdeal");
|
|
|
59 |
updateFlipkartPricing.start();
|
| 10268 |
vikram.rag |
60 |
|
| 11445 |
vikram.rag |
61 |
}
|
|
|
62 |
int updatePricing(Float price,String fksku,Item item,Long timestamp) throws JSONException{
|
|
|
63 |
logger.info("Calling Update Flipkart Price Method --" + " Price :" +price + " Supc :"+fksku +" Item ID:" +item.getId());
|
|
|
64 |
ClientConnectionManager connManager = new PoolingClientConnectionManager();
|
|
|
65 |
DefaultHttpClient httpclient = new DefaultHttpClient(connManager);
|
|
|
66 |
//httpclient = (DefaultHttpClient) WebClientWrapper.wrapClient(httpclient);
|
|
|
67 |
//httpclient.getCredentialsProvider().setCredentials(
|
|
|
68 |
// new AuthScope("api.flipkart.net", 443),
|
|
|
69 |
// new UsernamePasswordCredentials("m2z93iskuj81qiid","0c7ab6a5-98c0-4cdc-8be3-72c591e0add4"));
|
|
|
70 |
HttpPost httppost = new HttpPost("https://api.flipkart.net/sellers/skus/"+fksku+"/listings");
|
|
|
71 |
String auth = "m2z93iskuj81qiid"+":"+"0c7ab6a5-98c0-4cdc-8be3-72c591e0add4";
|
|
|
72 |
byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("US-ASCII")));
|
|
|
73 |
String authHeader = "Basic " + new String(encodedAuth);
|
|
|
74 |
httppost.setHeader(HttpHeaders.AUTHORIZATION, authHeader);
|
|
|
75 |
//StringBuffer jsonRequest = new StringBuffer();
|
|
|
76 |
JSONObject outer = new JSONObject();
|
|
|
77 |
JSONObject inner = new JSONObject();
|
|
|
78 |
outer.put("skuId",fksku);
|
|
|
79 |
inner.put("selling_price", String.valueOf(new Double(price).intValue()));
|
|
|
80 |
outer.put("attributeValues", inner);
|
|
|
81 |
/*jsonRequest.append("{\"skuId\":"+"\""+fksku+
|
| 10268 |
vikram.rag |
82 |
"\","+"\"attributeValues\""+":"+
|
|
|
83 |
"{\"selling_price\""+":"+"\""+price+
|
| 10316 |
kshitij.so |
84 |
"\"}");*/
|
| 11445 |
vikram.rag |
85 |
StringEntity input = null;
|
|
|
86 |
try {
|
|
|
87 |
input = new StringEntity(outer.toString());
|
|
|
88 |
logger.info("Json input " + outer.toString());
|
|
|
89 |
} catch (UnsupportedEncodingException e) {
|
|
|
90 |
logger.error("Unable to create request",e);
|
|
|
91 |
}
|
|
|
92 |
input.setContentType("application/json");
|
|
|
93 |
httppost.setEntity(input);
|
|
|
94 |
HttpResponse response = null;
|
|
|
95 |
try {
|
|
|
96 |
logger.info("Trying to post");
|
|
|
97 |
response = httpclient.execute(httppost);
|
|
|
98 |
} catch (IOException e) {
|
|
|
99 |
logger.error("Unable to post request",e);
|
|
|
100 |
}
|
|
|
101 |
BufferedReader rd = null;
|
|
|
102 |
try {
|
|
|
103 |
rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
|
|
|
104 |
} catch (IllegalStateException e) {
|
|
|
105 |
e.printStackTrace();
|
|
|
106 |
} catch (IOException e) {
|
|
|
107 |
e.printStackTrace();
|
|
|
108 |
}
|
|
|
109 |
String line;
|
|
|
110 |
boolean updated = false;
|
|
|
111 |
try {
|
| 10268 |
vikram.rag |
112 |
while ((line = rd.readLine()) != null) {
|
| 11445 |
vikram.rag |
113 |
logger.info("Response " + line);
|
| 12836 |
kshitij.so |
114 |
if(line.equalsIgnoreCase("{\"status\":\"success\"}")){
|
| 11445 |
vikram.rag |
115 |
updated = true;
|
|
|
116 |
}
|
|
|
117 |
}
|
| 10268 |
vikram.rag |
118 |
} catch (IOException e2) {
|
|
|
119 |
e2.printStackTrace();
|
|
|
120 |
}
|
| 11445 |
vikram.rag |
121 |
GmailUtils mailer = new GmailUtils();
|
|
|
122 |
String text = "Product : " +getProductName(this.item) +"\n"+
|
|
|
123 |
"Item ID : " +this.item.getId() +"\n"+
|
|
|
124 |
"SKU at Flipkart : " +this.fksku +"\n"+
|
|
|
125 |
"Updated Price : " +this.price;
|
| 12827 |
kshitij.so |
126 |
if(!updated){
|
|
|
127 |
FlipkartPricingPannel fkPricingPannel = new FlipkartPricingPannel();
|
|
|
128 |
try {
|
| 12832 |
kshitij.so |
129 |
updated = fkPricingPannel.updatePrice(this.fksku, String.valueOf(this.price));
|
| 12830 |
kshitij.so |
130 |
logger.info("Value of updated" +updated);
|
| 12827 |
kshitij.so |
131 |
} catch (Exception e) {
|
|
|
132 |
logger.info("Exception" + e);
|
|
|
133 |
}
|
|
|
134 |
}
|
| 11445 |
vikram.rag |
135 |
if(updated){
|
|
|
136 |
ArrayList<Long> updateList = new ArrayList<Long>();
|
|
|
137 |
try {
|
|
|
138 |
Client catalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
|
| 10268 |
vikram.rag |
139 |
|
| 11445 |
vikram.rag |
140 |
updateList.add(this.item.getId());
|
|
|
141 |
catalogClient.updateMarketPlacePriceUpdateStatus(updateList,this.timestamp,8);
|
|
|
142 |
} catch (TException e) {
|
|
|
143 |
try {
|
|
|
144 |
new CatalogClient().getClient().updateMarketPlacePriceUpdateStatus(updateList, timestamp,8);
|
|
|
145 |
} catch (TTransportException e1) {
|
|
|
146 |
e1.printStackTrace();
|
|
|
147 |
logger.info("Exception" + e1);
|
|
|
148 |
} catch (TException e1) {
|
|
|
149 |
e1.printStackTrace();
|
|
|
150 |
logger.info("Exception" + e1);
|
|
|
151 |
}
|
|
|
152 |
logger.info("Exception" + e);
|
|
|
153 |
}
|
|
|
154 |
try{
|
|
|
155 |
mailer.sendSSLMessage(sendTo, "Price updated on Flipkart ( Item ID " + this.item.getId() + " )",text, emailFromAddress , password,new ArrayList<File>());
|
|
|
156 |
}
|
|
|
157 |
catch(Exception e){
|
|
|
158 |
logger.info("Exception"+e);
|
|
|
159 |
}
|
|
|
160 |
}
|
|
|
161 |
else{
|
|
|
162 |
try{
|
|
|
163 |
mailer.sendSSLMessage(sendTo, "Failed to update Price on Flipkart ( Item ID " + this.item.getId() + " )",text, emailFromAddress , password,new ArrayList<File>());
|
|
|
164 |
}
|
|
|
165 |
catch(Exception e){
|
|
|
166 |
logger.info("Exception"+e);
|
|
|
167 |
}
|
| 10268 |
vikram.rag |
168 |
|
| 11445 |
vikram.rag |
169 |
}
|
|
|
170 |
return 1;
|
|
|
171 |
}
|
| 10268 |
vikram.rag |
172 |
|
| 11445 |
vikram.rag |
173 |
public void run()
|
|
|
174 |
{
|
|
|
175 |
try {
|
| 10316 |
kshitij.so |
176 |
this.updatePricing(this.price,this.fksku,this.item,timestamp);
|
|
|
177 |
} catch (JSONException e) {
|
|
|
178 |
// TODO Auto-generated catch block
|
|
|
179 |
logger.error("Exception while updating prices",e);
|
|
|
180 |
}
|
| 10268 |
vikram.rag |
181 |
|
| 11445 |
vikram.rag |
182 |
}
|
|
|
183 |
String getProductName(Item item){
|
|
|
184 |
return getName(item.getBrand())+" " + getName(item.getModelName())+" " + getName(item.getModelNumber())+" " + getName(item.getColor());
|
| 10268 |
vikram.rag |
185 |
|
| 11445 |
vikram.rag |
186 |
}
|
|
|
187 |
String getName(String name){
|
|
|
188 |
if(name==null || name.length()==0){
|
|
|
189 |
return "";
|
|
|
190 |
}
|
|
|
191 |
else{
|
|
|
192 |
return name;
|
|
|
193 |
}
|
|
|
194 |
}
|
| 10268 |
vikram.rag |
195 |
|
|
|
196 |
|
|
|
197 |
}
|