Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
12826 kshitij.so 1
package in.shop2020.serving.services;
2
 
3
import in.shop2020.config.ConfigException;
4
import in.shop2020.thrift.clients.config.ConfigClient;
5
 
6
import java.io.BufferedReader;
7
import java.io.IOException;
8
import java.io.InputStreamReader;
9
import java.io.UnsupportedEncodingException;
10
import java.util.ArrayList;
11
import java.util.List;
12
 
13
import org.apache.http.HttpResponse;
14
import org.apache.http.NameValuePair;
15
import org.apache.http.client.ClientProtocolException;
16
import org.apache.http.client.HttpClient;
17
import org.apache.http.client.entity.UrlEncodedFormEntity;
18
import org.apache.http.client.methods.HttpGet;
19
import org.apache.http.client.methods.HttpPost;
20
import org.apache.http.entity.StringEntity;
21
import org.apache.http.impl.client.DefaultHttpClient;
22
import org.apache.http.message.BasicNameValuePair;
23
import org.json.JSONArray;
24
import org.json.JSONException;
25
import org.json.JSONObject;
26
import org.slf4j.Logger;
27
import org.slf4j.LoggerFactory;
28
 
29
public class FlipkartPricingPannel{
30
 
31
    private static Logger logger = LoggerFactory.getLogger(FlipkartPricingPannel.class);
32
 
33
    public static final String AUTH_NAME = "flipkart";
34
    public static final String USER_NAME = "flipkart-support@saholic.com";
35
    public static final String PASSWORD = "076c27ee24d7596b06608a8ed2559f87";
36
    public static final String SELLER_ID = "m2z93iskuj81qiid";
37
    public static final String CSRF = "wuru84MMNRMhRCWtlhydVdN9";
13668 manish.sha 38
    public static String COOKIE ;
12826 kshitij.so 39
 
40
    public static final String SELLER_HOME = "seller.flipkart.com";
41
    public static final String USER_AGENT = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.63";
42
    public static final HttpClient client = new DefaultHttpClient();
43
 
44
    private static String UPDATE_PRICE_ON_FK;
45
 
46
    static{
47
        try {
48
            UPDATE_PRICE_ON_FK = ConfigClient.getClient().get("sync_price_on_marketplace");
49
        } catch (ConfigException e) {
50
            logger.error("Unable to get sync prices on marketplace", e);
51
            UPDATE_PRICE_ON_FK = "false";
52
        }
12828 kshitij.so 53
        logger.info(UPDATE_PRICE_ON_FK);
12826 kshitij.so 54
    }
55
 
56
 
57
    public boolean updatePrice(String skuAtFlipkart, String sellingPrice) throws ClientProtocolException, IOException, JSONException{
58
 
12835 kshitij.so 59
        if (!Boolean.valueOf(UPDATE_PRICE_ON_FK)){
12826 kshitij.so 60
            return true;
61
        }
62
 
13668 manish.sha 63
        HttpGet get_new;
64
        BufferedReader rd= null;
65
		get_new = new HttpGet("https://seller.flipkart.com/");
66
		get_new.addHeader("Host","seller.flipkart.com");
67
        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");
68
        get_new.addHeader("Connection","keep-alive");
69
 
70
        HttpResponse response = client.execute(get_new);
71
        rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
72
        String line = "";
73
        while ((line = rd.readLine()) != null) {
74
            System.out.println(line);
75
        }
76
        COOKIE = response.getFirstHeader("Set-Cookie") == null ? "" : 
77
            response.getFirstHeader("Set-Cookie").getValue();
78
 
79
        System.out.println("Cookies Before Login "+ COOKIE);
80
 
12826 kshitij.so 81
        HttpPost post = new HttpPost("https://seller.flipkart.com/login");
13668 manish.sha 82
 
12826 kshitij.so 83
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
84
        nameValuePairs.add(new BasicNameValuePair("authName",
85
                AUTH_NAME));
86
        nameValuePairs.add(new BasicNameValuePair("username",
87
                USER_NAME));
88
        nameValuePairs.add(new BasicNameValuePair("password",
89
                PASSWORD));
90
        post.addHeader("Cookie",COOKIE);
91
        post.addHeader("User-agent", USER_AGENT);
92
        post.addHeader("Referer", SELLER_HOME);
93
        post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
13668 manish.sha 94
        response = client.execute(post);
95
        COOKIE = response.getFirstHeader("Set-Cookie") == null ? "" : 
96
            response.getFirstHeader("Set-Cookie").getValue();
97
		System.out.println("cookies "+COOKIE);
12826 kshitij.so 98
        rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
13668 manish.sha 99
        line="";
12826 kshitij.so 100
        while ((line = rd.readLine()) != null) {
101
            System.out.println(line);
102
        }
103
        String data = "{\"refiners\":{\"sku_id\":[\""+skuAtFlipkart+"\"]},\"verticalGroup\":{},\"sellerId\":\""+SELLER_ID+"\",\"pageSize\":10,\"pageNumber\":1,\"state\":\"LIVE\"}";
104
        //String data = "{\"refiners\":{\"sku_id\":[\"12442\"]},\"verticalGroup\":{},\"sellerId\":\"m2z93iskuj81qiid\",\"pageSize\":10,\"pageNumb
12828 kshitij.so 105
        logger.info(data);
12826 kshitij.so 106
        //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);
107
        HttpPost httppost = new HttpPost("https://seller.flipkart.com/sellerListing/listing/listingsForContext?sellerId=m2z93iskuj81qiid");
13668 manish.sha 108
        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");
12826 kshitij.so 109
        httppost.addHeader("User-agent", USER_AGENT);
110
        httppost.addHeader("Referer", "https://seller.flipkart.com/appV2");
111
 
112
        StringEntity input = null;
113
        input = new StringEntity(data);
114
        input.setContentType("application/json");
115
        httppost.setEntity(input);
116
        HttpResponse response1 = null;
117
        response1 = client.execute(httppost);
118
        BufferedReader rd1 = null;
119
        rd1 = new BufferedReader(new InputStreamReader(response1.getEntity().getContent()));
120
        line= "";
121
        StringBuilder sb = new StringBuilder();
122
        while ((line = rd1.readLine()) != null) {
123
            sb.append(line);
124
        }
125
        response1.getEntity().consumeContent();
126
        JSONArray jsonArray = new JSONArray(sb.toString());
127
        String listingId = "";
128
        for (int i = 0, size = jsonArray.length(); i < size; i++){
129
            JSONObject x = jsonArray.getJSONObject(i);
130
            listingId = x.get("listing").toString();
131
        }
132
 
133
        data = getPostForSku(skuAtFlipkart, sellingPrice, listingId);
12828 kshitij.so 134
        logger.info(listingId);
12826 kshitij.so 135
        httppost = new HttpPost("https://seller.flipkart.com/sellerListing/listing/updateListings?sellerId=m2z93iskuj81qiid");
13668 manish.sha 136
        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");
12826 kshitij.so 137
        httppost.addHeader("User-agent", USER_AGENT);
138
        httppost.addHeader("Referer", "https://seller.flipkart.com/appV2");
139
 
140
        input = null;
141
        input = new StringEntity(data);
142
        input.setContentType("application/json");
143
        httppost.setEntity(input);
144
        response1 = null;
145
        response1 = client.execute(httppost);
146
        rd1 = null;
147
        rd1 = new BufferedReader(new InputStreamReader(response1.getEntity().getContent()));
148
        line= "";
149
        sb = new StringBuilder();
150
        while ((line = rd1.readLine()) != null) {
151
            sb.append(line);
152
        }
153
        System.out.println(sb.toString());
154
        response1.getEntity().consumeContent();
155
        jsonArray = new JSONArray(sb.toString());
156
        String listingPrice = "";
157
        for (int i = 0, size = jsonArray.length(); i < size; i++){
158
            JSONObject x = jsonArray.getJSONObject(i);
159
            listingPrice = x.get("listingPrice").toString();
160
        }
12828 kshitij.so 161
        logger.info("New listing price "+listingPrice);
12834 kshitij.so 162
        if (Double.valueOf(sellingPrice).equals(Double.valueOf(listingPrice))){
12826 kshitij.so 163
            return true;
164
        }
165
        return false;
166
    }
167
 
168
 
169
    public static String getPostForSku(String sku, String sellingPrice, String listingId){
170
        return "{\"sellerId\":\""+SELLER_ID+"\"" +
171
        ",\"bulkListingRequest\":{\"listings\":" +
172
        "[{\"listingId\":\""+listingId+"\",\"attributeValues\":" +
173
        "{\"selling_price\":\""+sellingPrice+"\"},\"" +
174
        "listingValidations\":" +
175
        "{\"PRICE_ERROR_CHECK\":" +
176
        "\"disable\"}}]}," +
177
        "\"_csrf\":\""+CSRF+"\"}";
178
           //return "{\"sellerId\":\"m2z93iskuj81qiid\",\"bulkListingRequest\":{\"listings\":[{\"listingId\":\"LSTACCDSN8FSJTTYPCQZWWGJL\",\"attributeValues\":{\"selling_price\":\"4901\"},\"listingValidations\":{\"PRICE_ERROR_CHECK\":\"enable\"}}]},\"_csrf\":\"wuru84MMNRMhRCWtlhydVdN9\"}";
179
    }
180
 
181
    public static void main(String[] args) throws ClientProtocolException, IOException, JSONException{
182
        FlipkartPricingPannel fkPricing = new FlipkartPricingPannel();
12834 kshitij.so 183
        System.out.println(fkPricing.updatePrice("169","629"));
12826 kshitij.so 184
    }
185
 
186
 
187
 
188
}