Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

package in.shop2020;

import in.shop2020.model.v1.catalog.CatalogService.Client;
import in.shop2020.model.v1.catalog.FlipkartItem;
import in.shop2020.model.v1.inventory.FlipkartStockAtEOD;
import in.shop2020.model.v1.inventory.SnapdealStockAtEOD;
import in.shop2020.thrift.clients.CatalogClient;
import in.shop2020.thrift.clients.InventoryClient;
import in.shop2020.utils.GmailUtils;
import java.io.BufferedReader;
import java.io.File;
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 java.util.Map.Entry;
import in.shop2020.WebClientWrapper;
import javax.mail.MessagingException;
import inventory.FlipkartJSONreponse;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
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.entity.StringEntity;
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 FlipkartInventoryAtEOD {
        private static List<FlipkartItem> flipkartItems;
        static java.text.SimpleDateFormat sdf;
        static String emailFromAddress;
        static String password;
        static GmailUtils mailer;
        static String sendTo[];
        static
        {
                sdf = new java.text.SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
                emailFromAddress = "build@shop2020.in";
                password = "cafe@nes";
                mailer = new GmailUtils();
                sendTo = new String[]{"vikram.raghav@shop2020.in"};
                /*              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","manoj.pal@shop2020.in","chandan.kumar@shop2020.in","ankush.dhingra@shop2020.in","amar.kumar@shop2020.in"};
                 */             List<String> flipkartorderids = null;
                 Client catalogServiceClient = null;
                 try {
                         catalogServiceClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port").getClient();
                 } catch (TTransportException e) {
                         e.printStackTrace();
                 }
                 try {
                         flipkartItems = catalogServiceClient.getAllFlipkartItems();
                 } catch (TException e) {
                         e.printStackTrace();
                 }
        }

        public static void main(String[] args) throws ClientProtocolException, TException{
                DefaultHttpClient httpclient = new DefaultHttpClient();
                httpclient = (DefaultHttpClient) WebClientWrapper.wrapClient(httpclient);
                //httpclient.getCredentialsProvider().setCredentials(
                //              new AuthScope("sandbox-api.flipkart.net", 443),
                //              new UsernamePasswordCredentials("og3yg5994bxbxxc1", "8a137068-417c-4e4f-ae95-9295ab938d88"));/// Test Server Access Keys
                httpclient.getCredentialsProvider().setCredentials(
                                new AuthScope("api.flipkart.net", 443),
                                new UsernamePasswordCredentials("m2z93iskuj81qiid","0c7ab6a5-98c0-4cdc-8be3-72c591e0add4")); //Prod Server Access Keys
                HttpGet httpget;
                BufferedReader rd = null;
                Gson gson = new Gson();
                List<FlipkartStockAtEOD> allflipkartstock = new ArrayList<FlipkartStockAtEOD>();
                FlipkartStockAtEOD stockItem;
                Long time = System.currentTimeMillis();
                for(FlipkartItem flipkartItem:flipkartItems){
                        httpget = new HttpGet("https://api.flipkart.net/sellers/skus/"+flipkartItem.getSkuAtFlipkart()+"/listings"); // PROD
                        HttpResponse response = null;
                        try {
                                response = httpclient.execute(httpget);
                        } catch (IOException e) {
                                e.printStackTrace();
                        }
                        try {
                                rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                        } catch (Exception e) {
                                e.printStackTrace();
                        }
                        /*try {
                                System.out.println("Response :" + rd.readLine());
                        } catch (IOException e) {
                                e.printStackTrace();
                        }*/
                        FlipkartJSONreponse flipkartJSONreponse = (FlipkartJSONreponse) gson.fromJson(rd, FlipkartJSONreponse.class);
                        if(flipkartJSONreponse!=null){
                                stockItem = new FlipkartStockAtEOD(); 
                                System.out.println("Item ID :" + flipkartItem.getSkuAtFlipkart());
                                stockItem.setItem_id(flipkartItem.getItem_id());
                                System.out.println("Item ID :" + flipkartItem.getItem_id());
                                System.out.println("Listing ID :" + flipkartJSONreponse.getListingId());
                                System.out.println("SKU at Flipkart :" + flipkartJSONreponse.getSkuId());
                                System.out.println("Stock Count :" + flipkartJSONreponse.getAttributes().getStock_count());
                                stockItem.setAvailability(Long.valueOf(flipkartJSONreponse.getAttributes().getStock_count()));
                                stockItem.setDate(time);
                                allflipkartstock.add(stockItem);
                        }
                }
                try{
                        new InventoryClient().getClient().updateFlipkartStockAtEOD(allflipkartstock);
                }
                catch(Exception e){
                        new InventoryClient().getClient().updateFlipkartStockAtEOD(allflipkartstock);
                }

        }

}