| 8285 |
kshitij.so |
1 |
package com.amazonaws.mws.samples;
|
|
|
2 |
|
|
|
3 |
import in.shop2020.model.v1.inventory.AmazonFbaInventorySnapshot;
|
|
|
4 |
import in.shop2020.thrift.clients.InventoryClient;
|
|
|
5 |
|
|
|
6 |
import java.io.FileNotFoundException;
|
|
|
7 |
import java.io.FileOutputStream;
|
|
|
8 |
import java.io.FileReader;
|
|
|
9 |
import java.io.IOException;
|
|
|
10 |
import java.io.OutputStream;
|
|
|
11 |
import java.util.Arrays;
|
|
|
12 |
import java.util.GregorianCalendar;
|
|
|
13 |
import java.util.List;
|
|
|
14 |
import java.util.Map;
|
|
|
15 |
|
|
|
16 |
import javax.xml.datatype.DatatypeConfigurationException;
|
|
|
17 |
import javax.xml.datatype.DatatypeFactory;
|
|
|
18 |
import javax.xml.datatype.XMLGregorianCalendar;
|
|
|
19 |
|
|
|
20 |
import org.apache.thrift.TException;
|
|
|
21 |
import org.apache.thrift.transport.TTransportException;
|
|
|
22 |
|
|
|
23 |
import au.com.bytecode.opencsv.CSVReader;
|
|
|
24 |
|
|
|
25 |
import com.amazonaws.mws.MarketplaceWebService;
|
|
|
26 |
import com.amazonaws.mws.MarketplaceWebServiceClient;
|
|
|
27 |
import com.amazonaws.mws.MarketplaceWebServiceConfig;
|
| 8363 |
vikram.rag |
28 |
import com.amazonaws.mws.MarketplaceWebServiceException;
|
| 8285 |
kshitij.so |
29 |
import com.amazonaws.mws.model.GetReportListRequest;
|
|
|
30 |
import com.amazonaws.mws.model.GetReportRequest;
|
|
|
31 |
import com.amazonaws.mws.model.IdList;
|
|
|
32 |
import com.amazonaws.mws.model.RequestReportRequest;
|
|
|
33 |
|
|
|
34 |
public class FetchAmazonInventory {
|
|
|
35 |
public static void main(String... args){
|
|
|
36 |
/************************************************************************
|
|
|
37 |
* Access Key ID and Secret Access Key ID, obtained from:
|
|
|
38 |
* http://aws.amazon.com
|
|
|
39 |
***********************************************************************/
|
|
|
40 |
final String accessKeyId = "AKIAII3SGRXBJDPCHSGQ";
|
|
|
41 |
final String secretAccessKey = "B92xTbNBTYygbGs98w01nFQUhbec1pNCkCsKVfpg";
|
|
|
42 |
|
|
|
43 |
final String appName = "Test";
|
|
|
44 |
final String appVersion = "1.0";
|
|
|
45 |
final String merchantId = "AF6E3O0VE0X4D";
|
|
|
46 |
|
|
|
47 |
MarketplaceWebServiceConfig config = new MarketplaceWebServiceConfig();
|
|
|
48 |
|
|
|
49 |
/************************************************************************
|
|
|
50 |
* Uncomment to set the appropriate MWS endpoint.
|
|
|
51 |
************************************************************************/
|
|
|
52 |
// US
|
|
|
53 |
// config.setServiceURL("https://mws.amazonservices.com");
|
|
|
54 |
// UK
|
|
|
55 |
// config.setServiceURL("https://mws.amazonservices.co.uk");
|
|
|
56 |
// Germany
|
|
|
57 |
// config.setServiceURL("https://mws.amazonservices.de");
|
|
|
58 |
// France
|
|
|
59 |
// config.setServiceURL("https://mws.amazonservices.fr");
|
|
|
60 |
// Italy
|
|
|
61 |
// config.setServiceURL("https://mws.amazonservices.it");
|
|
|
62 |
// Japan
|
|
|
63 |
// config.setServiceURL("https://mws.amazonservices.jp");
|
|
|
64 |
// China
|
|
|
65 |
// config.setServiceURL("https://mws.amazonservices.com.cn");
|
|
|
66 |
// Canada
|
|
|
67 |
// config.setServiceURL("https://mws.amazonservices.ca");
|
|
|
68 |
// India
|
|
|
69 |
config.setServiceURL("https://mws.amazonservices.in");
|
|
|
70 |
|
|
|
71 |
/************************************************************************
|
|
|
72 |
* You can also try advanced configuration options. Available options are:
|
|
|
73 |
*
|
|
|
74 |
* - Signature Version
|
|
|
75 |
* - Proxy Host and Proxy Port
|
|
|
76 |
* - User Agent String to be sent to Marketplace Web Service
|
|
|
77 |
*
|
|
|
78 |
***********************************************************************/
|
|
|
79 |
|
|
|
80 |
/************************************************************************
|
|
|
81 |
* Instantiate Http Client Implementation of Marketplace Web Service
|
|
|
82 |
***********************************************************************/
|
|
|
83 |
|
|
|
84 |
MarketplaceWebService service = new MarketplaceWebServiceClient(
|
|
|
85 |
accessKeyId, secretAccessKey, appName, appVersion, config);
|
|
|
86 |
|
|
|
87 |
/************************************************************************
|
|
|
88 |
* Uncomment to try out Mock Service that simulates Marketplace Web Service
|
|
|
89 |
* responses without calling Marketplace Web Service service.
|
|
|
90 |
*
|
|
|
91 |
* Responses are loaded from local XML files. You can tweak XML files to
|
|
|
92 |
* experiment with various outputs during development
|
|
|
93 |
*
|
|
|
94 |
* XML files available under com/amazonaws/mws/mock tree
|
|
|
95 |
*
|
|
|
96 |
***********************************************************************/
|
|
|
97 |
// MarketplaceWebService service = new MarketplaceWebServiceMock();
|
|
|
98 |
|
|
|
99 |
/************************************************************************
|
|
|
100 |
* Setup request parameters and uncomment invoke to try out
|
|
|
101 |
* sample for Request Report
|
|
|
102 |
***********************************************************************/
|
|
|
103 |
|
|
|
104 |
/************************************************************************
|
|
|
105 |
* Marketplace and Merchant IDs are required parameters for all
|
|
|
106 |
* Marketplace Web Service calls.
|
|
|
107 |
***********************************************************************/
|
|
|
108 |
// marketplaces from which data should be included in the report; look at the
|
|
|
109 |
// API reference document on the MWS website to see which marketplaces are
|
|
|
110 |
// included if you do not specify the list yourself
|
|
|
111 |
final IdList marketplaces = new IdList(Arrays.asList(
|
|
|
112 |
"A21TJRUUN4KGV"));
|
|
|
113 |
RequestReportRequest request = new RequestReportRequest()
|
|
|
114 |
.withMerchant(merchantId)
|
|
|
115 |
.withMarketplaceIdList(marketplaces)
|
|
|
116 |
.withReportType("_GET_AFN_INVENTORY_DATA_")
|
|
|
117 |
.withReportOptions("ShowSalesChannel=true");
|
|
|
118 |
|
|
|
119 |
// demonstrates how to set the date range
|
|
|
120 |
DatatypeFactory df = null;
|
|
|
121 |
try {
|
|
|
122 |
df = DatatypeFactory.newInstance();
|
|
|
123 |
} catch (DatatypeConfigurationException e) {
|
|
|
124 |
e.printStackTrace();
|
|
|
125 |
throw new RuntimeException(e);
|
|
|
126 |
}
|
|
|
127 |
//XMLGregorianCalendar startDate = df.newXMLGregorianCalendar(new GregorianCalendar());
|
|
|
128 |
//XMLGregorianCalendar endDate = df.newXMLGregorianCalendar(new GregorianCalendar());
|
|
|
129 |
//request.setStartDate(startDate);
|
|
|
130 |
//request.setEndDate(endDate);
|
|
|
131 |
// @TODO: set additional request parameters here
|
|
|
132 |
Map<String,String> requestIdreportIdmap;
|
|
|
133 |
///Request report
|
|
|
134 |
while(true){
|
| 8363 |
vikram.rag |
135 |
String requestId = null;
|
|
|
136 |
try {
|
|
|
137 |
requestId = RequestReportSample.invokeRequestReport(service, request);
|
|
|
138 |
} catch (MarketplaceWebServiceException e1) {
|
|
|
139 |
// TODO Auto-generated catch block
|
|
|
140 |
e1.printStackTrace();
|
| 8472 |
vikram.rag |
141 |
continue;
|
| 8363 |
vikram.rag |
142 |
}
|
| 8285 |
kshitij.so |
143 |
while(true){
|
|
|
144 |
GetReportListRequest requestreportlist = new GetReportListRequest();
|
|
|
145 |
requestreportlist.setMerchant( merchantId );
|
|
|
146 |
final IdList requestIdList = new IdList(Arrays.asList(requestId));
|
|
|
147 |
requestreportlist.setReportRequestIdList(requestIdList);
|
|
|
148 |
///Request report status
|
|
|
149 |
requestIdreportIdmap = GetReportListSample.invokeGetReportList(service, requestreportlist);
|
|
|
150 |
|
|
|
151 |
GetReportRequest requestreport = new GetReportRequest();
|
|
|
152 |
requestreport.setMerchant( merchantId );
|
|
|
153 |
|
|
|
154 |
///Fetch report only if it is ready
|
|
|
155 |
if(requestIdreportIdmap.get(requestId)!=null){
|
|
|
156 |
requestreport.setReportId( requestIdreportIdmap.get(requestId) );
|
|
|
157 |
OutputStream report=null;
|
|
|
158 |
try {
|
| 8363 |
vikram.rag |
159 |
report = new FileOutputStream( "/home/amazoninventoryreport.csv" );
|
| 8285 |
kshitij.so |
160 |
} catch (FileNotFoundException e) {
|
|
|
161 |
// TODO Auto-generated catch block
|
|
|
162 |
e.printStackTrace();
|
|
|
163 |
}
|
|
|
164 |
requestreport.setReportOutputStream( report );
|
|
|
165 |
GetReportSample.invokeGetReport(service, requestreport);
|
| 8363 |
vikram.rag |
166 |
//System.out.println("Report ready please check");
|
| 8285 |
kshitij.so |
167 |
CSVReader reader = null;
|
|
|
168 |
try {
|
| 8363 |
vikram.rag |
169 |
//reader = new CSVReader(new FileReader("/home/vikram/Desktop/amazoninventoryreport.txt"),'\t');
|
|
|
170 |
reader = new CSVReader(new FileReader("/home/amazoninventoryreport.csv"),'\t');
|
| 8285 |
kshitij.so |
171 |
} catch (FileNotFoundException e) {
|
|
|
172 |
// TODO Auto-generated catch block
|
|
|
173 |
e.printStackTrace();
|
|
|
174 |
}
|
|
|
175 |
String [] nextLine;
|
|
|
176 |
InventoryClient inventoryServiceClient = null;
|
|
|
177 |
try {
|
|
|
178 |
inventoryServiceClient = new InventoryClient();
|
|
|
179 |
} catch (Exception e) {
|
|
|
180 |
// TODO Auto-generated catch block
|
|
|
181 |
e.printStackTrace();
|
|
|
182 |
}in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
|
|
|
183 |
try {
|
|
|
184 |
while ((nextLine = reader.readNext()) != null) {
|
|
|
185 |
// nextLine[] is an array of values from the line
|
| 8363 |
vikram.rag |
186 |
//System.out.println(nextLine[0] +" "+ nextLine[1]+" " + nextLine[2]+" " + nextLine[3] +" "+ nextLine[4] +" " + nextLine[5]);
|
| 8285 |
kshitij.so |
187 |
if(nextLine[0].startsWith("FBA") && nextLine[4].equalsIgnoreCase("SELLABLE") ){
|
|
|
188 |
//System.out.println("Item ID" + nextLine[0].replaceAll("FBA","") + "---"+"Inventory" + nextLine[5]);
|
|
|
189 |
AmazonFbaInventorySnapshot amazonfbainventorysnapshot = new AmazonFbaInventorySnapshot() ;
|
|
|
190 |
amazonfbainventorysnapshot.setAvailability(Long.parseLong(nextLine[5]));
|
|
|
191 |
amazonfbainventorysnapshot.setItem_id(Long.parseLong(nextLine[0].replaceAll("FBA","")));
|
|
|
192 |
inventoryClient.addOrUpdateAmazonFbaInventory(amazonfbainventorysnapshot);
|
|
|
193 |
|
|
|
194 |
}
|
|
|
195 |
}
|
|
|
196 |
} catch (IOException e) {
|
|
|
197 |
// TODO Auto-generated catch block
|
|
|
198 |
e.printStackTrace();
|
|
|
199 |
} catch (TException e) {
|
|
|
200 |
// TODO Auto-generated catch block
|
|
|
201 |
e.printStackTrace();
|
|
|
202 |
}
|
|
|
203 |
break;
|
|
|
204 |
}
|
|
|
205 |
else{
|
| 8363 |
vikram.rag |
206 |
//System.out.println("Report not ready");
|
| 8285 |
kshitij.so |
207 |
try {
|
|
|
208 |
Thread.sleep(5*60*1000);
|
|
|
209 |
} catch (InterruptedException e) {
|
|
|
210 |
// TODO Auto-generated catch block
|
|
|
211 |
e.printStackTrace();
|
|
|
212 |
}
|
|
|
213 |
}
|
|
|
214 |
}
|
|
|
215 |
try {
|
|
|
216 |
Thread.sleep(30*60*1000);
|
|
|
217 |
} catch (InterruptedException e) {
|
|
|
218 |
// TODO Auto-generated catch block
|
|
|
219 |
e.printStackTrace();
|
|
|
220 |
}
|
|
|
221 |
}
|
|
|
222 |
|
|
|
223 |
// Note that depending on the type of report being downloaded, a report can reach
|
|
|
224 |
// sizes greater than 1GB. For this reason we recommend that you _always_ program to
|
|
|
225 |
// MWS in a streaming fashion. Otherwise, as your business grows you may silently reach
|
|
|
226 |
// the in-memory size limit and have to re-work your solution.
|
|
|
227 |
//
|
|
|
228 |
|
|
|
229 |
|
|
|
230 |
}
|
|
|
231 |
|
|
|
232 |
}
|