Rev 15599 | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.support.utils;import in.shop2020.model.v1.catalog.CatalogService.Client;import in.shop2020.model.v1.catalog.CatalogServiceException;import in.shop2020.model.v1.catalog.Item;import in.shop2020.model.v1.order.OrderSource;import in.shop2020.serving.services.SnapdealSessionCookie;import in.shop2020.thrift.clients.CatalogClient;import in.shop2020.utils.GmailUtils;import java.io.BufferedReader;import java.io.File;import java.io.IOException;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;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.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.conn.ClientConnectionManager;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.impl.conn.PoolingClientConnectionManager;import org.apache.http.message.BasicNameValuePair;import org.apache.thrift.TException;import org.apache.thrift.transport.TTransportException;import org.json.JSONObject;import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class UpdateSDPricingUsingPanel extends Thread{static ClientConnectionManager connManager = new PoolingClientConnectionManager();private static DefaultHttpClient httpClient = new DefaultHttpClient(connManager);//private static DefaultHttpClient httpClient = new DefaultHttpClient();private Float price;private String supc;private Item item;private Long timestamp;//public String[] sendTo = new String[]{"vikram.raghav@shop2020.in"};private static Logger logger = LoggerFactory.getLogger(UpdateSDPricingUsingPanel.class);public String[] 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","chandan.kumar@shop2020.in","ankush.dhingra@shop2020.in","kshitij.sood@shop2020.in"};public String emailFromAddress = "build@shop2020.in";public String password = "cafe@nes";private static Map<String, String> headers = new HashMap<String, String>();static {headers.put("User-agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11");headers.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");headers.put("Accept-Language", "en-US,en;q=0.8");headers.put("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.3");}public UpdateSDPricingUsingPanel(Float price,String supc,Item item,Long timestamp){logger.info("Calling Update Snapdeal Price Constructor --" + " Price :" +price + " Supc :"+supc +" Item ID:" +item.getId());this.price = price;this.supc = supc;this.item = item;this.timestamp = timestamp;}public UpdateSDPricingUsingPanel() {}public static void main(String... args) throws ClientProtocolException, IOException, TTransportException, CatalogServiceException, TException{Item item = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient().getItem(2231);UpdateSDPricingUsingPanel updatePriceOnSnapdeal = new UpdateSDPricingUsingPanel(49000f,"SDL850558681",item,System.currentTimeMillis());logger.info("Calling Thread to update price at snapdeal");updatePriceOnSnapdeal.start();}int updatePricing(Float price,String supc,Item item,Long timestamp) throws Exception{logger.info("Calling Update Snapdeal Price Constructor --" + " Price :" +price + " Supc :"+supc +" Item ID:" +item.getId());SnapdealSessionCookie sdSessionCookie = new SnapdealSessionCookie();String cookies = "";JSONObject cookieObject = null;long priceVal = (long)price.floatValue();HttpPost post = new HttpPost("http://seller.snapdeal.com/pricing/update");cookies = sdSessionCookie.getCookies();cookieObject = new JSONObject(cookies);for(String key:headers.keySet())post.addHeader(key, headers.get(key));post.addHeader("Cookie","SERVERID="+cookieObject.get("SERVERID")+";sfJSESSIONID="+cookieObject.get("sfJSESSIONID")+";");List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();nameValuePairs.add(new BasicNameValuePair("changedField","sellingPrice"));nameValuePairs.add(new BasicNameValuePair("changedValue",priceVal+""));nameValuePairs.add(new BasicNameValuePair("supc",supc));post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));HttpResponse response = httpClient.execute(post);BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));String line = "";while ((line = rd.readLine()) != null) {logger.info(line);System.out.println(line);}return 1;}public void handleLogin() throws ClientProtocolException, IOException{logger.info("----Inside Handle Login ----" );HttpGet get = new HttpGet("http://selleraccounts.snapdeal.com/login?service=http%3A%2F%2Fseller.snapdeal.com%2Fj_spring_cas_security_check");HttpResponse response = null;boolean retry = true;while(retry){try {response = httpClient.execute(get);retry = false;} catch (ClientProtocolException e) {e.printStackTrace();logger.error("Exception ",e);} catch (IOException e) {e.printStackTrace();logger.info("Exception " + e);}}BufferedReader rd = null;try {rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));} catch (IllegalStateException e1) {e1.printStackTrace();} catch (IOException e1) {e1.printStackTrace();}String line = "";StringBuffer sb = new StringBuffer();try {while ((line = rd.readLine()) != null) {sb.append(line);}} catch (IOException e) {e.printStackTrace();}int i= sb.toString().indexOf("name=\"lt\" value=");char[] charArray = sb.toString().toCharArray();String lt = "";int j=0;for(j=i+16;j<=charArray.length;j++){if(charArray[j]==' '){break;}}lt = sb.substring(i+17,j-1);System.out.println("LT VALUE " + lt);i= sb.toString().indexOf("name=\"execution\" value=");charArray = sb.toString().toCharArray();String ex = "";j=0;for(j=i+24;j<=charArray.length;j++){if(charArray[j]==' '){break;}}ex = sb.substring(i+24,j-1);System.out.println("EXECUTION VALUE " + ex);HttpPost post = new HttpPost("http://selleraccounts.snapdeal.com/login?service=http%3A%2F%2Fseller.snapdeal.com%2Fj_spring_cas_security_check");List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();nameValuePairs.add(new BasicNameValuePair("username","saholic-snapdeal@saholic.com"));nameValuePairs.add(new BasicNameValuePair("password","saholic15"));nameValuePairs.add(new BasicNameValuePair("_eventId","submit"));nameValuePairs.add(new BasicNameValuePair("execution",ex));nameValuePairs.add(new BasicNameValuePair("lt",lt));nameValuePairs.add(new BasicNameValuePair("submit","LOGIN"));post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));response = httpClient.execute(post);rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));line = "";while ((line = rd.readLine()) != null) {System.out.println(line);}get = new HttpGet("http://seller.snapdeal.com/pricing");response = httpClient.execute(get);rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));while ((line = rd.readLine()) != null) {//System.out.println(line);}logger.info("----Login Successful----" );}public void run(){logger.info("inside run");try {this.updatePricing(this.price,this.supc,this.item,timestamp);boolean retry =true;SnapdealSessionCookie sdSessionCookie = new SnapdealSessionCookie();String cookies = "";JSONObject cookieObject = null;while(retry){Thread.sleep(2*60*1000);HttpGet get = new HttpGet("http://seller.snapdeal.com/pricing/search?searchType=SUPC&searchValue="+this.supc+"&gridType=normal&_search=false&nd="+System.currentTimeMillis()+"&rows=30&page=1&sidx=&sord=asc");cookies = sdSessionCookie.getCookies();cookieObject = new JSONObject(cookies);for(String key:headers.keySet())get.addHeader(key, headers.get(key));get.addHeader("Cookie","SERVERID="+cookieObject.get("SERVERID")+";sfJSESSIONID="+cookieObject.get("sfJSESSIONID")+";");HttpResponse response = httpClient.execute(get);BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));String line = "";while ((line = rd.readLine()) != null) {logger.info(line);if(line.contains("\"pendingUpdate\":false")){retry =false;}}}GmailUtils mailer = new GmailUtils();String text = "Product : " +getProductName(this.item) +"\n"+"Item ID : " +this.item.getId() +"\n"+"SUPC : " +this.supc +"\n"+"Updated Price : " +this.price;try{mailer.sendSSLMessage(sendTo, "Price updated on Snapdeal ( Item ID " + this.item.getId()+ " )",text, emailFromAddress , password,new ArrayList<File>());logger.info("mail sent");}catch(Exception e){logger.info("Exception"+e);}ArrayList<Long> updateList = new ArrayList<Long>();try {logger.info("creating client");Client catalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();updateList.add(this.item.getId());logger.info(updateList.toString());catalogClient.updateMarketPlacePriceUpdateStatus(updateList,this.timestamp,OrderSource.SNAPDEAL.getValue());logger.info("client call done");} catch (TException e) {try {new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient().updateMarketPlacePriceUpdateStatus(updateList, timestamp,OrderSource.SNAPDEAL.getValue());} catch (TTransportException e1) {e1.printStackTrace();logger.info("Exception" + e1);} catch (TException e1) {e1.printStackTrace();logger.info("Exception" + e1);}logger.info("Exception" + e);}} catch (Exception e) {e.printStackTrace();//logger.error("Exception" + e);GmailUtils mailer = new GmailUtils();String text = "Product : " +getProductName(this.item) +"\n"+"Item ID : " +this.item.getId() +"\n"+"SUPC : " +this.supc +"\n"+"Updated Price : " +this.price;try {mailer.sendSSLMessage(sendTo, "Failed to update Price on Snapdeal ( Item ID " + this.item.getId()+" )",text, emailFromAddress , password,new ArrayList<File>());} catch (MessagingException e1) {e1.printStackTrace();logger.info("Exception" + e1);}}}String getProductName(Item item){return getName(item.getBrand())+" " + getName(item.getModelName())+" " + getName(item.getModelNumber())+" " + getName(item.getColor());}String getName(String name){if(name==null || name.length()==0){return "";}else{return name;}}}