Subversion Repositories SmartDukaan

Rev

Rev 12835 | Rev 13765 | 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 java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

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 = "076c27ee24d7596b06608a8ed2559f87";
    public static final String SELLER_ID = "m2z93iskuj81qiid";
    public static final String CSRF = "wuru84MMNRMhRCWtlhydVdN9";
    public static String COOKIE ;

    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);
    }


    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 data = "{\"refiners\":{\"sku_id\":[\""+skuAtFlipkart+"\"]},\"verticalGroup\":{},\"sellerId\":\""+SELLER_ID+"\",\"pageSize\":10,\"pageNumber\":1,\"state\":\"LIVE\"}";
        //String data = "{\"refiners\":{\"sku_id\":[\"12442\"]},\"verticalGroup\":{},\"sellerId\":\"m2z93iskuj81qiid\",\"pageSize\":10,\"pageNumb
        logger.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/sellerListing/listing/listingsForContext?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();
        JSONArray jsonArray = new JSONArray(sb.toString());
        String listingId = "";
        for (int i = 0, size = jsonArray.length(); i < size; i++){
            JSONObject x = jsonArray.getJSONObject(i);
            listingId = x.get("listing").toString();
        }
        
        data = getPostForSku(skuAtFlipkart, sellingPrice, listingId);
        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");

        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());
        response1.getEntity().consumeContent();
        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){
        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","629"));
    }



}