2017-09-29 18:12:00 +02:00
|
|
|
require('es6-promise').polyfill();
|
|
|
|
require('isomorphic-fetch');
|
|
|
|
|
|
|
|
export const BASEURL = 'https://world.openfoodfacts.org/';
|
|
|
|
|
2017-10-22 21:20:58 +02:00
|
|
|
function _fetchFromOFFApi(filters) {
|
|
|
|
let url = BASEURL;
|
|
|
|
filters.forEach((filter) => {
|
|
|
|
url += `/${filter}`;
|
|
|
|
});
|
|
|
|
|
|
|
|
return fetch(`${url}.json`);
|
|
|
|
}
|
|
|
|
|
2017-09-29 18:12:00 +02:00
|
|
|
function missingCategories() {
|
2017-10-22 21:20:58 +02:00
|
|
|
return _fetchFromOFFApi([
|
|
|
|
'state/product-name-completed',
|
|
|
|
'state/brands-completed',
|
|
|
|
'state/photos-validated',
|
|
|
|
'state/categories-to-be-completed',
|
|
|
|
])
|
|
|
|
// Parse JSON
|
2017-09-29 18:12:00 +02:00
|
|
|
.then(response => response.json())
|
2017-10-22 21:20:58 +02:00
|
|
|
// Keep only useful data
|
2017-09-29 18:12:00 +02:00
|
|
|
.then(response => response.products.map(product => ({
|
|
|
|
id: product.id,
|
|
|
|
name: product.product_name,
|
|
|
|
icon: product.image_front_url,
|
|
|
|
brands: product.brands,
|
2017-10-22 21:20:58 +02:00
|
|
|
})))
|
|
|
|
// Predict categories
|
|
|
|
.then(response => fetch(
|
|
|
|
'http://localhost:4242/predict',
|
|
|
|
{
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
2017-09-29 18:12:00 +02:00
|
|
|
},
|
2017-10-22 21:20:58 +02:00
|
|
|
body: JSON.stringify(response),
|
2017-09-29 18:12:00 +02:00
|
|
|
},
|
2017-10-22 21:20:58 +02:00
|
|
|
))
|
|
|
|
// Parse JSON
|
|
|
|
.then(response => response.json())
|
|
|
|
// Parse data, strip products without any predicted category
|
|
|
|
.then(response => response.data.filter(
|
|
|
|
product => product.predictedCategories.length > 0),
|
|
|
|
)
|
|
|
|
// Augment predicted categories data
|
|
|
|
.then(response => response.map(
|
|
|
|
(product) => {
|
|
|
|
const augmentedProduct = product;
|
|
|
|
augmentedProduct.predictedCategories = product.predictedCategories.map(
|
|
|
|
category => ({ name: category, isOk: false }),
|
|
|
|
);
|
|
|
|
return augmentedProduct;
|
|
|
|
},
|
|
|
|
))
|
2017-09-29 18:12:00 +02:00
|
|
|
.catch(exc => console.error(`Unable to fetch products with missing categories: ${exc}.`));
|
|
|
|
}
|
|
|
|
|
2017-10-22 21:20:58 +02:00
|
|
|
|
2017-11-17 19:07:02 +01:00
|
|
|
function updateCategories(productId, categories) {
|
|
|
|
// TODO
|
|
|
|
console.log(productId, categories);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-22 21:20:58 +02:00
|
|
|
function missingBrands() {
|
2017-11-17 19:07:02 +01:00
|
|
|
return _fetchFromOFFApi(['state/brands-to-be-completed'])
|
2017-10-22 21:20:58 +02:00
|
|
|
.then(response => response.json())
|
|
|
|
.then(response => response.products.map(product => ({
|
|
|
|
id: product.id,
|
|
|
|
name: product.product_name,
|
|
|
|
icon: product.image_front_url,
|
|
|
|
})))
|
|
|
|
.catch(exc => console.error(`Unable to fetch products with missing brands: ${exc}.`));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function missingProductName() {
|
2017-11-17 19:07:02 +01:00
|
|
|
return _fetchFromOFFApi(['state/product-name-to-be-completed'])
|
2017-10-22 21:20:58 +02:00
|
|
|
.then(response => response.json())
|
|
|
|
.then(response => response.products.map(product => ({
|
|
|
|
id: product.id,
|
|
|
|
brands: product.brands,
|
|
|
|
icon: product.image_front_url,
|
|
|
|
})))
|
|
|
|
.catch(exc => console.error(`Unable to fetch products with missing product name: ${exc}.`));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-17 19:07:02 +01:00
|
|
|
export { missingBrands, missingCategories, updateCategories, missingProductName };
|