Rev 15625 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.serving.services;import in.shop2020.config.ConfigException;import in.shop2020.thrift.clients.config.ConfigClient;import in.shop2020.utils.GmailUtils;import java.io.BufferedReader;import java.io.File;import java.io.IOException;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import java.util.ArrayList;import java.util.List;import javax.mail.MessagingException;import org.apache.http.HttpResponse;import org.apache.http.NameValuePair;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.HttpClient;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.StringEntity;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.message.BasicNameValuePair;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class FlipkartPricingPannel{private static Logger logger = LoggerFactory.getLogger(FlipkartPricingPannel.class);public static final String AUTH_NAME = "flipkart";public static final String USER_NAME = "flipkart-support@saholic.com";public static final String PASSWORD = "bestmobiledeals2010";public static final String SELLER_ID = "m2z93iskuj81qiid";public static String COOKIE ;static String emailFromAddress;static String password;static GmailUtils mailer;static String sendTo[];public static final String SELLER_HOME = "seller.flipkart.com";public static final String USER_AGENT = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.63";public static final HttpClient client = new DefaultHttpClient();private static String UPDATE_PRICE_ON_FK;static{try {UPDATE_PRICE_ON_FK = ConfigClient.getClient().get("sync_price_on_marketplace");} catch (ConfigException e) {logger.error("Unable to get sync prices on marketplace", e);UPDATE_PRICE_ON_FK = "false";}logger.info(UPDATE_PRICE_ON_FK);emailFromAddress = "build@shop2020.in";password = "cafe@nes";mailer = new GmailUtils();//sendTo = new String[]{"manish.sharma@shop2020.in"};sendTo = new String[]{ "sandeep.sachdeva@shop2020.in", "manish.sharma@shop2020.in", "rajneesh.arora@shop2020.in","khushal.bhatia@shop2020.in","manoj.kumar@saholic.com","chaitnaya.vats@saholic.com","yukti.jain@shop2020.in","yatin.singh@shop2020.in","chandan.kumar@shop2020.in","ankush.dhingra@shop2020.in","anikendra.das@shop2020.in"};}public static String getCsrfValue() throws ClientProtocolException, IOException, JSONException{HttpResponse response;BufferedReader rd= null;HttpGet get_new;String line = "";get_new = new HttpGet("https://seller.flipkart.com/sfx");get_new.addHeader("Cookie",COOKIE+"; __utmt=1; __utma=143439159.1675929865.1422546011.1422546011.1422546011.1; __utmb=143439159.1.10.1422546011; __utmc=143439159; __utmz=143439159.1422546011.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); is_login=true; sellerId=m2z93iskuj81qiid");get_new.addHeader("Host","seller.flipkart.com");get_new.addHeader("User-agent", "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.63 Safari/537.31");get_new.addHeader("Connection","keep-alive");response = client.execute(get_new);rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));String csrfVal = "";while ((line = rd.readLine()) != null) {if(line.contains("var seller = ")){String desiredLine = line;desiredLine = desiredLine.split("var seller = ")[1].trim();desiredLine = desiredLine.substring(0, desiredLine.length()-1);System.out.println("desiredLine... "+desiredLine);JSONObject jsonObj = new JSONObject(desiredLine);csrfVal=(String)jsonObj.get("csrf");System.out.println(csrfVal);}}return csrfVal;}public boolean updatePrice(String skuAtFlipkart, String sellingPrice) throws ClientProtocolException, IOException, JSONException{if (!Boolean.valueOf(UPDATE_PRICE_ON_FK)){return true;}HttpGet get_new;BufferedReader rd= null;get_new = new HttpGet("https://seller.flipkart.com/");get_new.addHeader("Host","seller.flipkart.com");get_new.addHeader("User-agent", "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.63 Safari/537.31");get_new.addHeader("Connection","keep-alive");HttpResponse response = client.execute(get_new);rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));String line = "";while ((line = rd.readLine()) != null) {System.out.println(line);}COOKIE = response.getFirstHeader("Set-Cookie") == null ? "" :response.getFirstHeader("Set-Cookie").getValue();System.out.println("Cookies Before Login "+ COOKIE);HttpPost post = new HttpPost("https://seller.flipkart.com/login");List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();nameValuePairs.add(new BasicNameValuePair("authName",AUTH_NAME));nameValuePairs.add(new BasicNameValuePair("username",USER_NAME));nameValuePairs.add(new BasicNameValuePair("password",PASSWORD));post.addHeader("Cookie",COOKIE);post.addHeader("User-agent", USER_AGENT);post.addHeader("Referer", SELLER_HOME);post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));response = client.execute(post);COOKIE = response.getFirstHeader("Set-Cookie") == null ? "" :response.getFirstHeader("Set-Cookie").getValue();System.out.println("cookies "+COOKIE);rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));line="";while ((line = rd.readLine()) != null) {System.out.println(line);}String csrfVal= getCsrfValue();String data = "{\"refiners\":{\"sku_id\":[\""+skuAtFlipkart+"\"]},\"verticalGroup\":{},\"sellerId\":\""+SELLER_ID+"\",\"pageSize\":10,\"pageNumber\":1,\"state\":\"LIVE\",\"_csrf\":\""+csrfVal+"\"}";//String data = "{\"refiners\":{\"sku_id\":[\"12442\"]},\"verticalGroup\":{},\"sellerId\":\"m2z93iskuj81qiid\",\"pageSize\":10,\"pageNumblogger.info(data);//HttpGet get_new = new HttpGet("https://seller.flipkart.com/dashboard/som/new_order_items?status=on_hold%2Capproved.payment_approved&sort=confirm_by_date&page="+i+"&page_size=100&_="+time);HttpPost httppost = new HttpPost("https://seller.flipkart.com/createproduct/getproductrowsforstate?sellerId=m2z93iskuj81qiid");httppost.addHeader("Cookie",COOKIE+"; __utmt=1; __utma=143439159.1675929865.1422546011.1422546011.1422546011.1; __utmb=143439159.1.10.1422546011; __utmc=143439159; __utmz=143439159.1422546011.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); is_login=true; sellerId=m2z93iskuj81qiid");httppost.addHeader("User-agent", USER_AGENT);httppost.addHeader("Referer", "https://seller.flipkart.com/appV2");StringEntity input = null;input = new StringEntity(data);input.setContentType("application/json");httppost.setEntity(input);HttpResponse response1 = null;response1 = client.execute(httppost);BufferedReader rd1 = null;rd1 = new BufferedReader(new InputStreamReader(response1.getEntity().getContent()));line= "";StringBuilder sb = new StringBuilder();while ((line = rd1.readLine()) != null) {sb.append(line);}response1.getEntity().consumeContent();System.out.println(sb.toString());JSONObject jsonResult = new JSONObject(sb.toString());JSONArray jsonResultArr = (JSONArray)(JSONArray)jsonResult.get("data");String listingId = ((JSONObject)jsonResultArr.get(0)).get("listing").toString();data = getPostForSku(skuAtFlipkart, sellingPrice, listingId, csrfVal);logger.info(listingId);httppost = new HttpPost("https://seller.flipkart.com/sellerListing/listing/updateListings?sellerId=m2z93iskuj81qiid");httppost.addHeader("Cookie",COOKIE+"; __utmt=1; __utma=143439159.1675929865.1422546011.1422546011.1422546011.1; __utmb=143439159.1.10.1422546011; __utmc=143439159; __utmz=143439159.1422546011.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); is_login=true; sellerId=m2z93iskuj81qiid");httppost.addHeader("User-agent", USER_AGENT);httppost.addHeader("Referer", "https://seller.flipkart.com/appV2");httppost.addHeader("fk-csrf-token",csrfVal);input = null;input = new StringEntity(data);input.setContentType("application/json");httppost.setEntity(input);response1 = null;response1 = client.execute(httppost);rd1 = null;rd1 = new BufferedReader(new InputStreamReader(response1.getEntity().getContent()));line= "";sb = new StringBuilder();while ((line = rd1.readLine()) != null) {sb.append(line);}System.out.println(sb.toString());try {mailer.sendSSLMessage(sendTo, "Flipkart Pricing Sent Response ", sb.toString(), emailFromAddress, password,new ArrayList<File>());} catch (MessagingException e) {e.printStackTrace();}response1.getEntity().consumeContent();JSONArray jsonArray = new JSONArray(sb.toString());String listingPrice = "";for (int i = 0, size = jsonArray.length(); i < size; i++){JSONObject x = jsonArray.getJSONObject(i);listingPrice = x.get("listingPrice").toString();}logger.info("New listing price "+listingPrice);if (Double.valueOf(sellingPrice).equals(Double.valueOf(listingPrice))){return true;}return false;}public static String getPostForSku(String sku, String sellingPrice, String listingId, String csrf){return "{\"sellerId\":\""+SELLER_ID+"\"" +",\"bulkListingRequest\":{\"listings\":" +"[{\"listingId\":\""+listingId+"\",\"attributeValues\":" +"{\"selling_price\":\""+sellingPrice+"\"},\"" +"listingValidations\":" +"{\"PRICE_ERROR_CHECK\":" +"\"disable\"}}]}," +"\"_csrf\":\""+csrf+"\"}";//return "{\"sellerId\":\"m2z93iskuj81qiid\",\"bulkListingRequest\":{\"listings\":[{\"listingId\":\"LSTACCDSN8FSJTTYPCQZWWGJL\",\"attributeValues\":{\"selling_price\":\"4901\"},\"listingValidations\":{\"PRICE_ERROR_CHECK\":\"enable\"}}]},\"_csrf\":\"wuru84MMNRMhRCWtlhydVdN9\"}";}public static void main(String[] args) throws ClientProtocolException, IOException, JSONException{FlipkartPricingPannel fkPricing = new FlipkartPricingPannel();System.out.println(fkPricing.updatePrice("169","650"));}}