Subversion Repositories SmartDukaan

Rev

Rev 15899 | 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+"; T=TI143676574953728283468652963707792772000507925594940705902272051229; s_ppv=20; __utma=19769839.1606715935.1436765752.1436776227.1436781309.4; __utmc=19769839; s_cc=true; s_ch_list=%5B%5B'Affiliates'%2C'1436765752631'%5D%2C%5B'Direct%2528No%2520referrer%2529'%2C'1436772820483'%5D%2C%5B'Affiliates'%2C'1436776225673'%5D%2C%5B'Affiliates'%2C'1436781310176'%5D%5D; s_sq=%5B%5BB%5D%5D; SN=2.VI867C5C7412964CF699334DBD9EF6DE52.SIA588536C11BE409DA6EDA6421E4C553A.VS143678130317178984193.1436781310; VID=2.VI867C5C7412964CF699334DBD9EF6DE52.1436781310.VS143678130317178984193; NSID=2.SIA588536C11BE409DA6EDA6421E4C553A.1436781310.VI867C5C7412964CF699334DBD9EF6DE52; is_login=true; sellerId=m2z93iskuj81qiid; __utma=143439159.1310095091.1436782302.1436782302.1436782302.1; __utmb=143439159.3.9.1436782338363; __utmc=143439159; __utmz=143439159.1436782302.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); km_lv=x; _ga=GA1.2.1310095091.1436782302; _mkto_trk=id:021-QVV-957&token:_mch-flipkart.com-1436782303258-56348; kvcd=1436783491524; km_vs=1; km_ai=m2z93iskuj81qiid; km_ni=m2z93iskuj81qiid; km_uq=");
                                                  
                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,\"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/createproduct/getproductrowsforstate?sellerId=m2z93iskuj81qiid");
        httppost.addHeader("Cookie",COOKIE+";  __gads=ID=dfe0374cf04d1576:T=1381304511:S=ALNI_Ma2TpDrlF9_amkoqp3MOYJBaFAOUg; km_lv=x; T=TI138130450866706710769873257251497444628820234222703981199700765991; __CG=u%3A6208250296506778000%2Cs%3A2098853214%2Ct%3A1419058011787%2Cc%3A1%2Ck%3Awww.flipkart.com/24/24/76%2Cf%3A0%2Ci%3A1; S=d1t13CXEVURPbRn%2FTAlMG9iwwwBWoS%2FUUixPxuJkY6W8pdfcWEzMG07x3EBPSRtlGHPdclpSYHdyFGacGJ0K01qpxtQ%3D%3D; __sonar=6631097455123118581; s_ch_list=%5B%5B'Affiliates'%2C'1436780663582'%5D%2C%5B'Direct%2528No%2520referrer%2529'%2C'1437154172975'%5D%5D; __utma=19769839.1156779613.1390806906.1436780662.1437154173.31; __utmz=19769839.1436780662.30.17.utmcsr=mobilehotindia.com|utmccn=(referral)|utmcmd=referral|utmcct=/r.html; VID=2.VIB4C4C187B63B4436AAE42944FD9BE1C9.1437389520.VS143738952083130426084; NSID=2.SI2B008083CC2145FFB103D6E76CDB343C.1437389520.VIB4C4C187B63B4436AAE42944FD9BE1C9;  SN=2.VIB4C4C187B63B4436AAE42944FD9BE1C9.SI2B008083CC2145FFB103D6E76CDB343C.VS143738952083130426084.1437389520; __utmt=1; _gat=1; is_login=true; sellerId=m2z93iskuj81qiid; __utma=143439159.1156779613.1390806906.1435740037.1437454247.95; __utmb=143439159.7.9.1437454350458; __utmc=143439159; __utmz=143439159.1390806906.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); _ga=GA1.2.1156779613.1390806906; _mkto_trk=id:021-QVV-957&token:_mch-flipkart.com-1434970574990-64308; kvcd=1437454400320; km_vs=1; km_ai=m2z93iskuj81qiid; km_ni=m2z93iskuj81qiid; km_uq=1437454437%20%2Fe%3Fcategory%3Dlistings%26label%3Dsearch%2520sku_id%2520in%2520my%2520listings%26_n%3Dsearch%2520listings%26_k%3D24a3ad2b74ad481059076b2c4d300b978b2c7adb%26_p%3Dm2z93iskuj81qiid%26_t%3D1437454437");
        httppost.addHeader("User-agent", USER_AGENT);
        httppost.addHeader("Referer", "https://seller.flipkart.com/appV2");
        httppost.addHeader("fk-csrf-token",csrfVal);

        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+"; T=TI143676574953728283468652963707792772000507925594940705902272051229; s_ppv=20; __utma=19769839.1606715935.1436765752.1436776227.1436781309.4; __utmc=19769839; s_cc=true; s_ch_list=%5B%5B'Affiliates'%2C'1436765752631'%5D%2C%5B'Direct%2528No%2520referrer%2529'%2C'1436772820483'%5D%2C%5B'Affiliates'%2C'1436776225673'%5D%2C%5B'Affiliates'%2C'1436781310176'%5D%5D; s_sq=%5B%5BB%5D%5D; SN=2.VI867C5C7412964CF699334DBD9EF6DE52.SIA588536C11BE409DA6EDA6421E4C553A.VS143678130317178984193.1436781310; VID=2.VI867C5C7412964CF699334DBD9EF6DE52.1436781310.VS143678130317178984193; NSID=2.SIA588536C11BE409DA6EDA6421E4C553A.1436781310.VI867C5C7412964CF699334DBD9EF6DE52; is_login=true; sellerId=m2z93iskuj81qiid; __utma=143439159.1310095091.1436782302.1436782302.1436782302.1; __utmb=143439159.3.9.1436782338363; __utmc=143439159; __utmz=143439159.1436782302.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); km_lv=x; _ga=GA1.2.1310095091.1436782302; _mkto_trk=id:021-QVV-957&token:_mch-flipkart.com-1436782303258-56348; kvcd=1436783491524; km_vs=1; km_ai=m2z93iskuj81qiid; km_ni=m2z93iskuj81qiid; km_uq=");
        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"));
    }



}