Subversion Repositories SmartDukaan

Rev

Rev 11267 | Rev 13855 | Go to most recent revision | 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.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.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.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.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", "vikram.raghav@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";
        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(22200f,"1108903",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 ClientProtocolException, IOException{
                logger.info("Calling Update Snapdeal Price Constructor --" + " Price :" +price + " Supc :"+supc +" Item ID:" +item.getId());
                HttpPost post = new HttpPost("http://seller.snapdeal.com/pricing/update");
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("changedField",
                "sellingPrice"));
                nameValuePairs.add(new BasicNameValuePair("changedValue",
                                String.valueOf(price.intValue())));
                nameValuePairs.add(new BasicNameValuePair("id",
                                supc));
                nameValuePairs.add(new BasicNameValuePair("oper","edit"));
                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) {
                        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/keymaker/login");
                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",
                "snapsaholic"));
                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()
        {
                try {
                        this.handleLogin();
                        this.updatePricing(this.price,this.supc,this.item,timestamp);
                        boolean retry =true;
                        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");
                                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>());
                        }
                        catch(Exception e){
                                logger.info("Exception"+e);
                        }
                        ArrayList<Long> updateList = new ArrayList<Long>();
                        try {
                                Client catalogClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();

                                updateList.add(this.item.getId());
                                catalogClient.updateMarketPlacePriceUpdateStatus(updateList,this.timestamp,OrderSource.SNAPDEAL.getValue());
                        } 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;
                }
        }


}