Rev 8472 | Rev 8544 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package com.amazonaws.mws.samples;import in.shop2020.model.v1.inventory.AmazonFbaInventorySnapshot;import in.shop2020.thrift.clients.CatalogClient;import in.shop2020.thrift.clients.InventoryClient;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileReader;import java.io.IOException;import java.io.OutputStream;import java.util.Arrays;import java.util.GregorianCalendar;import java.util.List;import java.util.Map;import javax.xml.datatype.DatatypeConfigurationException;import javax.xml.datatype.DatatypeFactory;import javax.xml.datatype.XMLGregorianCalendar;import org.apache.thrift.TException;import org.apache.thrift.transport.TTransportException;import au.com.bytecode.opencsv.CSVReader;import com.amazonaws.mws.MarketplaceWebService;import com.amazonaws.mws.MarketplaceWebServiceClient;import com.amazonaws.mws.MarketplaceWebServiceConfig;import com.amazonaws.mws.MarketplaceWebServiceException;import com.amazonaws.mws.model.GetReportListRequest;import com.amazonaws.mws.model.GetReportRequest;import com.amazonaws.mws.model.IdList;import com.amazonaws.mws.model.RequestReportRequest;public class FetchAmazonInventory {public static void main(String... args){/************************************************************************* Access Key ID and Secret Access Key ID, obtained from:* http://aws.amazon.com***********************************************************************/final String accessKeyId = "AKIAII3SGRXBJDPCHSGQ";final String secretAccessKey = "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";final String appName = "Test";final String appVersion = "1.0";final String merchantId = "AF6E3O0VE0X4D";MarketplaceWebServiceConfig config = new MarketplaceWebServiceConfig();/************************************************************************* Uncomment to set the appropriate MWS endpoint.************************************************************************/// US// config.setServiceURL("https://mws.amazonservices.com");// UK// config.setServiceURL("https://mws.amazonservices.co.uk");// Germany// config.setServiceURL("https://mws.amazonservices.de");// France// config.setServiceURL("https://mws.amazonservices.fr");// Italy// config.setServiceURL("https://mws.amazonservices.it");// Japan// config.setServiceURL("https://mws.amazonservices.jp");// China// config.setServiceURL("https://mws.amazonservices.com.cn");// Canada// config.setServiceURL("https://mws.amazonservices.ca");// Indiaconfig.setServiceURL("https://mws.amazonservices.in");/************************************************************************* You can also try advanced configuration options. Available options are:** - Signature Version* - Proxy Host and Proxy Port* - User Agent String to be sent to Marketplace Web Service************************************************************************//************************************************************************* Instantiate Http Client Implementation of Marketplace Web Service***********************************************************************/MarketplaceWebService service = new MarketplaceWebServiceClient(accessKeyId, secretAccessKey, appName, appVersion, config);/************************************************************************* Uncomment to try out Mock Service that simulates Marketplace Web Service* responses without calling Marketplace Web Service service.** Responses are loaded from local XML files. You can tweak XML files to* experiment with various outputs during development** XML files available under com/amazonaws/mws/mock tree************************************************************************/// MarketplaceWebService service = new MarketplaceWebServiceMock();/************************************************************************* Setup request parameters and uncomment invoke to try out* sample for Request Report***********************************************************************//************************************************************************* Marketplace and Merchant IDs are required parameters for all* Marketplace Web Service calls.***********************************************************************/// marketplaces from which data should be included in the report; look at the// API reference document on the MWS website to see which marketplaces are// included if you do not specify the list yourselffinal IdList marketplaces = new IdList(Arrays.asList("A21TJRUUN4KGV"));RequestReportRequest request = new RequestReportRequest().withMerchant(merchantId).withMarketplaceIdList(marketplaces).withReportType("_GET_AFN_INVENTORY_DATA_").withReportOptions("ShowSalesChannel=true");// demonstrates how to set the date rangeDatatypeFactory df = null;try {df = DatatypeFactory.newInstance();} catch (DatatypeConfigurationException e) {e.printStackTrace();throw new RuntimeException(e);}//XMLGregorianCalendar startDate = df.newXMLGregorianCalendar(new GregorianCalendar());//XMLGregorianCalendar endDate = df.newXMLGregorianCalendar(new GregorianCalendar());//request.setStartDate(startDate);//request.setEndDate(endDate);// @TODO: set additional request parameters hereMap<String,String> requestIdreportIdmap;///Request reportwhile(true){String requestId = null;try {requestId = RequestReportSample.invokeRequestReport(service, request);} catch (MarketplaceWebServiceException e1) {// TODO Auto-generated catch blocke1.printStackTrace();continue;}while(true){GetReportListRequest requestreportlist = new GetReportListRequest();requestreportlist.setMerchant( merchantId );final IdList requestIdList = new IdList(Arrays.asList(requestId));requestreportlist.setReportRequestIdList(requestIdList);///Request report statusrequestIdreportIdmap = GetReportListSample.invokeGetReportList(service, requestreportlist);GetReportRequest requestreport = new GetReportRequest();requestreport.setMerchant( merchantId );///Fetch report only if it is readyif(requestIdreportIdmap.get(requestId)!=null){requestreport.setReportId( requestIdreportIdmap.get(requestId) );OutputStream report=null;try {report = new FileOutputStream( "/home/amazoninventoryreport.csv" );} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}requestreport.setReportOutputStream( report );GetReportSample.invokeGetReport(service, requestreport);//System.out.println("Report ready please check");CSVReader reader = null;try {//reader = new CSVReader(new FileReader("/home/vikram/Desktop/amazoninventoryreport.txt"),'\t');reader = new CSVReader(new FileReader("/home/amazoninventoryreport.csv"),'\t');} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}String [] nextLine;CatalogClient catalogServiceClient = null;InventoryClient inventoryServiceClient = null;try {inventoryServiceClient = new InventoryClient();//catalogServiceClient = new CatalogClient();catalogServiceClient = new CatalogClient("catalog_service_server_host_amazon","catalog_service_server_port");} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}in.shop2020.model.v1.catalog.CatalogService.Client catalogClient = catalogServiceClient.getClient();in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();try {while ((nextLine = reader.readNext()) != null) {// nextLine[] is an array of values from the line//System.out.println(nextLine[0] +" "+ nextLine[1]+" " + nextLine[2]+" " + nextLine[3] +" "+ nextLine[4] +" " + nextLine[5]);if(nextLine[0].startsWith("FBA") && nextLine[4].equalsIgnoreCase("SELLABLE") ){//System.out.println("Item ID" + nextLine[0].replaceAll("FBA","") + "---"+"Inventory" + nextLine[5]);if(catalogClient.getAmazonItemDetails(Long.parseLong(nextLine[0].replaceAll("FBA",""))).getItemid()!=0){AmazonFbaInventorySnapshot amazonfbainventorysnapshot = new AmazonFbaInventorySnapshot() ;amazonfbainventorysnapshot.setAvailability(Long.parseLong(nextLine[5]));amazonfbainventorysnapshot.setItem_id(Long.parseLong(nextLine[0].replaceAll("FBA","")));inventoryClient.addOrUpdateAmazonFbaInventory(amazonfbainventorysnapshot);}}}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (TException e) {// TODO Auto-generated catch blocke.printStackTrace();}break;}else{//System.out.println("Report not ready");try {Thread.sleep(5*60*1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}try {Thread.sleep(30*60*1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}// Note that depending on the type of report being downloaded, a report can reach// sizes greater than 1GB. For this reason we recommend that you _always_ program to// MWS in a streaming fashion. Otherwise, as your business grows you may silently reach// the in-memory size limit and have to re-work your solution.//}}