Subversion Repositories SmartDukaan

Rev

Rev 11266 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020;

import in.shop2020.model.v1.catalog.SnapdealItemDetails;
import in.shop2020.model.v1.inventory.SnapdealStockAtEOD;
import in.shop2020.thrift.clients.CatalogClient;
import in.shop2020.thrift.clients.InventoryClient;
import inventory.Inventory;
import inventory.InventoryItems;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.thrift.TException;
import org.apache.thrift.transport.TTransportException;

import com.google.gson.Gson;

public class SnapdealInventoryAtEOD {

        private static DefaultHttpClient client = new DefaultHttpClient();
        private static Map<Long, SnapdealItemDetails> itemSnapdealMap = new HashMap<Long, SnapdealItemDetails>();;
        private static Map<String, SnapdealItemDetails> snapdealItemMap = new HashMap<String, SnapdealItemDetails>();

        static{
                CatalogClient catalogServiceClient = null;
                try {
                        catalogServiceClient = new CatalogClient();
                } catch (TTransportException e1) {
                        e1.printStackTrace();
                }
                in.shop2020.model.v1.catalog.CatalogService.Client catalogClient = catalogServiceClient.getClient();
                List<SnapdealItemDetails> allSnapdealItems = null;

                try {
                        allSnapdealItems = catalogClient.getAllSnapdealItems();
                } catch (TException e) {

                        e.printStackTrace();
                }
                for(SnapdealItemDetails snapdealItem:allSnapdealItems){
                        snapdealItemMap.put(snapdealItem.getSkuAtSnapdeal(),snapdealItem);
                        itemSnapdealMap.put(snapdealItem.getItem_id(), snapdealItem);

                }
                System.out.println("++++++++++++++++++++++++++++++");
                System.out.println("Created Item and Snapdeal Map ");
                System.out.println("++++++++++++++++++++++++++++++");

        }


        private static void fetchcurrentinventoryfromsnapdeal() throws UnsupportedEncodingException, TException{
                int i = 1;
                int items=0;
                Gson gson = new Gson();
                ArrayList<Inventory> currentInventoryItemList = new ArrayList<Inventory>();
                HttpGet get;
                HttpResponse response = null;
                BufferedReader rd= null;
                System.out.println("Fetching current inventory ");
                while(true){
                        Long time = System.currentTimeMillis();
                        //System.out.println("Fetching current inventory page " +i);
                        get = new HttpGet("http://seller.snapdeal.com/inventory/search?gridType=normal&_search=false&nd="+time+"&rows="+30+"&page="+i+"&sidx=&sord=asc");
                        try {
                                response = client.execute(get);
                        } catch (ClientProtocolException e) {

                                e.printStackTrace();
                        } catch (IOException e) {

                                e.printStackTrace();
                        }
                        try {
                                rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                        } catch (IllegalStateException e) {

                                e.printStackTrace();
                        } catch (IOException e) {

                                e.printStackTrace();
                        }
                        InventoryItems inventoryitems = (InventoryItems) gson.fromJson(rd, InventoryItems.class);
                        if(inventoryitems.getRows().size()!=0){
                                for(Inventory inventory : inventoryitems.getRows()){
                                        if(snapdealItemMap.containsKey(inventory.getSellerSku())){
                                                currentInventoryItemList.add(inventory);
                                        }

                                        else{
                                        }
                                        items++;
                                }
                        }
                        else{
                                System.out.println("Common items  " + items);
                                System.out.println("Total Pages Fetched  " + i);
                                break;
                        }
                        i++;
                }
                int total =0;
                List<SnapdealStockAtEOD> allsnapdealstock = new ArrayList<SnapdealStockAtEOD>();
                long time = System.currentTimeMillis();
                for(Inventory inventory:currentInventoryItemList){
                        SnapdealStockAtEOD snapdealStockAtEOD = new SnapdealStockAtEOD();
                        System.out.println("Item Id " + snapdealItemMap.get(inventory.getSellerSku()).getItem_id());
                        snapdealStockAtEOD.setItem_id(snapdealItemMap.get(inventory.getSellerSku()).getItem_id());
                        System.out.println("Sku at SD " + snapdealItemMap.get(inventory.getSellerSku()).getSkuAtSnapdeal());
                        System.out.println("Inventory " + inventory.getAvailableInventory());
                        snapdealStockAtEOD.setAvailability(inventory.getAvailableInventory());
                        snapdealStockAtEOD.setDate(time);
                        allsnapdealstock.add(snapdealStockAtEOD);
                        total++; 
                }
                System.out.println("Inventory found for " + total);
                try{
                        new InventoryClient().getClient().updateSnapdealStockAtEOD(allsnapdealstock);
                }
                catch(Exception e){
                        new InventoryClient().getClient().updateSnapdealStockAtEOD(allsnapdealstock);
                }

        }


        public static void handleLogin() throws ClientProtocolException, IOException{
                HttpGet get = new HttpGet("http://selleraccounts.snapdeal.com/keymaker/login");
                HttpResponse response = null;
                try {
                        response = client.execute(get);
                } catch (ClientProtocolException e) {

                        e.printStackTrace();
                } catch (IOException e) {

                        e.printStackTrace();
                }
                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);
                                //System.out.println(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",
                //"khushal.bhatia@saholic.com"));
                nameValuePairs.add(new BasicNameValuePair("username",
                "saholic-snapdeal@saholic.com"));
                //nameValuePairs.add(new BasicNameValuePair("password",
                //"sonline"));
                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 = client.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/inventory/");
                response = client.execute(get);
                rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                while ((line = rd.readLine()) != null) {
                        //System.out.println(line);
                }
        }

        public static void main(String... args) throws ClientProtocolException, IOException, TException{
                handleLogin();
                fetchcurrentinventoryfromsnapdeal();
        }


}