Subversion Repositories SmartDukaan

Rev

Rev 15625 | Rev 15899 | 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;
13784 manish.sha 5
import in.shop2020.utils.GmailUtils;
12826 kshitij.so 6
 
7
import java.io.BufferedReader;
13784 manish.sha 8
import java.io.File;
12826 kshitij.so 9
import java.io.IOException;
10
import java.io.InputStreamReader;
11
import java.io.UnsupportedEncodingException;
12
import java.util.ArrayList;
13
import java.util.List;
14
 
13784 manish.sha 15
import javax.mail.MessagingException;
16
 
12826 kshitij.so 17
import org.apache.http.HttpResponse;
18
import org.apache.http.NameValuePair;
19
import org.apache.http.client.ClientProtocolException;
20
import org.apache.http.client.HttpClient;
21
import org.apache.http.client.entity.UrlEncodedFormEntity;
22
import org.apache.http.client.methods.HttpGet;
23
import org.apache.http.client.methods.HttpPost;
24
import org.apache.http.entity.StringEntity;
25
import org.apache.http.impl.client.DefaultHttpClient;
26
import org.apache.http.message.BasicNameValuePair;
27
import org.json.JSONArray;
28
import org.json.JSONException;
29
import org.json.JSONObject;
30
import org.slf4j.Logger;
31
import org.slf4j.LoggerFactory;
32
 
33
public class FlipkartPricingPannel{
34
 
35
    private static Logger logger = LoggerFactory.getLogger(FlipkartPricingPannel.class);
36
 
37
    public static final String AUTH_NAME = "flipkart";
38
    public static final String USER_NAME = "flipkart-support@saholic.com";
13765 manish.sha 39
    public static final String PASSWORD = "bestmobiledeals2010";
12826 kshitij.so 40
    public static final String SELLER_ID = "m2z93iskuj81qiid";
13668 manish.sha 41
    public static String COOKIE ;
13784 manish.sha 42
 
43
    static String emailFromAddress;
44
	static String password;
45
	static GmailUtils mailer;
46
	static String sendTo[];
47
 
48
 
12826 kshitij.so 49
 
50
    public static final String SELLER_HOME = "seller.flipkart.com";
51
    public static final String USER_AGENT = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.63";
52
    public static final HttpClient client = new DefaultHttpClient();
53
 
54
    private static String UPDATE_PRICE_ON_FK;
55
 
56
    static{
57
        try {
58
            UPDATE_PRICE_ON_FK = ConfigClient.getClient().get("sync_price_on_marketplace");
59
        } catch (ConfigException e) {
60
            logger.error("Unable to get sync prices on marketplace", e);
61
            UPDATE_PRICE_ON_FK = "false";
62
        }
12828 kshitij.so 63
        logger.info(UPDATE_PRICE_ON_FK);
13784 manish.sha 64
 
65
        emailFromAddress = "build@shop2020.in";
66
		password = "cafe@nes";
67
		mailer = new GmailUtils();
68
		//sendTo = new String[]{"manish.sharma@shop2020.in"};
69
		sendTo = new String[]{ "sandeep.sachdeva@shop2020.in", "manish.sharma@shop2020.in", "rajneesh.arora@shop2020.in",
70
				"khushal.bhatia@shop2020.in","manoj.kumar@saholic.com","chaitnaya.vats@saholic.com",
13792 manish.sha 71
				"yukti.jain@shop2020.in","yatin.singh@shop2020.in","chandan.kumar@shop2020.in","ankush.dhingra@shop2020.in","anikendra.das@shop2020.in"};
12826 kshitij.so 72
    }
15636 manish.sha 73
 
74
    public static String getCsrfValue() throws ClientProtocolException, IOException, JSONException{
75
		HttpResponse response;
76
 
77
		BufferedReader rd= null;
78
		HttpGet get_new;
79
 
80
		String line = "";
81
 
82
		get_new = new HttpGet("https://seller.flipkart.com/sfx");
83
		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");
84
		get_new.addHeader("Host","seller.flipkart.com");
85
        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");
86
        get_new.addHeader("Connection","keep-alive");
87
 
88
		response = client.execute(get_new);
12826 kshitij.so 89
 
15636 manish.sha 90
		rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
12826 kshitij.so 91
 
15636 manish.sha 92
		String csrfVal = "";
93
		while ((line = rd.readLine()) != null) {
94
			if(line.contains("var seller = ")){
95
				String desiredLine = line;
96
				desiredLine = desiredLine.split("var seller = ")[1].trim();
97
				desiredLine = desiredLine.substring(0, desiredLine.length()-1);
98
				System.out.println("desiredLine... "+desiredLine);
99
				JSONObject jsonObj = new JSONObject(desiredLine);
100
 
101
				csrfVal=(String)jsonObj.get("csrf");
102
				System.out.println(csrfVal);
103
			}
104
		}
105
		return csrfVal;
106
	}
107
 
108
 
12826 kshitij.so 109
    public boolean updatePrice(String skuAtFlipkart, String sellingPrice) throws ClientProtocolException, IOException, JSONException{
110
 
12835 kshitij.so 111
        if (!Boolean.valueOf(UPDATE_PRICE_ON_FK)){
12826 kshitij.so 112
            return true;
113
        }
114
 
13668 manish.sha 115
        HttpGet get_new;
116
        BufferedReader rd= null;
117
		get_new = new HttpGet("https://seller.flipkart.com/");
118
		get_new.addHeader("Host","seller.flipkart.com");
119
        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");
120
        get_new.addHeader("Connection","keep-alive");
121
 
122
        HttpResponse response = client.execute(get_new);
123
        rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
124
        String line = "";
125
        while ((line = rd.readLine()) != null) {
126
            System.out.println(line);
127
        }
128
        COOKIE = response.getFirstHeader("Set-Cookie") == null ? "" : 
129
            response.getFirstHeader("Set-Cookie").getValue();
130
 
131
        System.out.println("Cookies Before Login "+ COOKIE);
132
 
12826 kshitij.so 133
        HttpPost post = new HttpPost("https://seller.flipkart.com/login");
13668 manish.sha 134
 
12826 kshitij.so 135
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
136
        nameValuePairs.add(new BasicNameValuePair("authName",
137
                AUTH_NAME));
138
        nameValuePairs.add(new BasicNameValuePair("username",
139
                USER_NAME));
140
        nameValuePairs.add(new BasicNameValuePair("password",
141
                PASSWORD));
142
        post.addHeader("Cookie",COOKIE);
143
        post.addHeader("User-agent", USER_AGENT);
144
        post.addHeader("Referer", SELLER_HOME);
145
        post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
13668 manish.sha 146
        response = client.execute(post);
147
        COOKIE = response.getFirstHeader("Set-Cookie") == null ? "" : 
148
            response.getFirstHeader("Set-Cookie").getValue();
149
		System.out.println("cookies "+COOKIE);
12826 kshitij.so 150
        rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
13668 manish.sha 151
        line="";
12826 kshitij.so 152
        while ((line = rd.readLine()) != null) {
153
            System.out.println(line);
154
        }
15636 manish.sha 155
 
156
        String csrfVal= getCsrfValue();
157
 
158
        String data = "{\"refiners\":{\"sku_id\":[\""+skuAtFlipkart+"\"]},\"verticalGroup\":{},\"sellerId\":\""+SELLER_ID+"\",\"pageSize\":10,\"pageNumber\":1,\"state\":\"LIVE\",\"_csrf\":\""+csrfVal+"\"}";
12826 kshitij.so 159
        //String data = "{\"refiners\":{\"sku_id\":[\"12442\"]},\"verticalGroup\":{},\"sellerId\":\"m2z93iskuj81qiid\",\"pageSize\":10,\"pageNumb
12828 kshitij.so 160
        logger.info(data);
12826 kshitij.so 161
        //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);
15625 manish.sha 162
        HttpPost httppost = new HttpPost("https://seller.flipkart.com/createproduct/getproductrowsforstate?sellerId=m2z93iskuj81qiid");
13668 manish.sha 163
        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 164
        httppost.addHeader("User-agent", USER_AGENT);
165
        httppost.addHeader("Referer", "https://seller.flipkart.com/appV2");
166
 
167
        StringEntity input = null;
168
        input = new StringEntity(data);
169
        input.setContentType("application/json");
170
        httppost.setEntity(input);
171
        HttpResponse response1 = null;
172
        response1 = client.execute(httppost);
173
        BufferedReader rd1 = null;
174
        rd1 = new BufferedReader(new InputStreamReader(response1.getEntity().getContent()));
175
        line= "";
176
        StringBuilder sb = new StringBuilder();
177
        while ((line = rd1.readLine()) != null) {
178
            sb.append(line);
179
        }
180
        response1.getEntity().consumeContent();
15636 manish.sha 181
        System.out.println(sb.toString());
182
        JSONObject jsonResult = new JSONObject(sb.toString());
183
        JSONArray jsonResultArr = (JSONArray)(JSONArray)jsonResult.get("data");
184
        String listingId = ((JSONObject)jsonResultArr.get(0)).get("listing").toString();
12826 kshitij.so 185
 
15636 manish.sha 186
        data = getPostForSku(skuAtFlipkart, sellingPrice, listingId, csrfVal);
12828 kshitij.so 187
        logger.info(listingId);
12826 kshitij.so 188
        httppost = new HttpPost("https://seller.flipkart.com/sellerListing/listing/updateListings?sellerId=m2z93iskuj81qiid");
13668 manish.sha 189
        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 190
        httppost.addHeader("User-agent", USER_AGENT);
191
        httppost.addHeader("Referer", "https://seller.flipkart.com/appV2");
15636 manish.sha 192
        httppost.addHeader("fk-csrf-token",csrfVal);
12826 kshitij.so 193
 
194
        input = null;
195
        input = new StringEntity(data);
196
        input.setContentType("application/json");
197
        httppost.setEntity(input);
198
        response1 = null;
199
        response1 = client.execute(httppost);
200
        rd1 = null;
201
        rd1 = new BufferedReader(new InputStreamReader(response1.getEntity().getContent()));
202
        line= "";
203
        sb = new StringBuilder();
204
        while ((line = rd1.readLine()) != null) {
205
            sb.append(line);
206
        }
207
        System.out.println(sb.toString());
13784 manish.sha 208
 
209
        try {
210
        	mailer.sendSSLMessage(sendTo, "Flipkart Pricing Sent Response ", sb.toString(), emailFromAddress, password,new ArrayList<File>());
211
		} catch (MessagingException e) {
212
			e.printStackTrace();
213
		}
12826 kshitij.so 214
        response1.getEntity().consumeContent();
15636 manish.sha 215
        JSONArray jsonArray = new JSONArray(sb.toString());
12826 kshitij.so 216
        String listingPrice = "";
217
        for (int i = 0, size = jsonArray.length(); i < size; i++){
218
            JSONObject x = jsonArray.getJSONObject(i);
219
            listingPrice = x.get("listingPrice").toString();
220
        }
12828 kshitij.so 221
        logger.info("New listing price "+listingPrice);
12834 kshitij.so 222
        if (Double.valueOf(sellingPrice).equals(Double.valueOf(listingPrice))){
12826 kshitij.so 223
            return true;
224
        }
225
        return false;
226
    }
227
 
228
 
15636 manish.sha 229
    public static String getPostForSku(String sku, String sellingPrice, String listingId, String csrf){
12826 kshitij.so 230
        return "{\"sellerId\":\""+SELLER_ID+"\"" +
231
        ",\"bulkListingRequest\":{\"listings\":" +
232
        "[{\"listingId\":\""+listingId+"\",\"attributeValues\":" +
233
        "{\"selling_price\":\""+sellingPrice+"\"},\"" +
234
        "listingValidations\":" +
235
        "{\"PRICE_ERROR_CHECK\":" +
236
        "\"disable\"}}]}," +
15636 manish.sha 237
        "\"_csrf\":\""+csrf+"\"}";
12826 kshitij.so 238
           //return "{\"sellerId\":\"m2z93iskuj81qiid\",\"bulkListingRequest\":{\"listings\":[{\"listingId\":\"LSTACCDSN8FSJTTYPCQZWWGJL\",\"attributeValues\":{\"selling_price\":\"4901\"},\"listingValidations\":{\"PRICE_ERROR_CHECK\":\"enable\"}}]},\"_csrf\":\"wuru84MMNRMhRCWtlhydVdN9\"}";
239
    }
240
 
241
    public static void main(String[] args) throws ClientProtocolException, IOException, JSONException{
242
        FlipkartPricingPannel fkPricing = new FlipkartPricingPannel();
13765 manish.sha 243
        System.out.println(fkPricing.updatePrice("169","650"));
12826 kshitij.so 244
    }
245
 
246
 
247
 
248
}