Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Open Food Facts search results #606

Merged
merged 2 commits into from
Aug 2, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions www/activities/foodlist/js/open-food-facts.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ app.OpenFoodFacts = {

// If query is a number, assume it's a barcode
if (isNaN(query))
url = "https://world.openfoodfacts.org/cgi/search.pl?search_terms=" + encodeURIComponent(query) + "&search_simple=1&page_size=50&sort_by=last_modified_t&action=process&json=1";
url = "https://world.openfoodfacts.org/cgi/search.pl?search_terms=" + encodeURIComponent(query) + "&search_simple=1&page_size=50&sort_by=unique_scans_n&action=process&json=1";
else
url = "https://world.openfoodfacts.org/api/v0/product/" + encodeURIComponent(query) + ".json";

Expand All @@ -42,6 +42,8 @@ app.OpenFoodFacts = {

if (language != undefined && language != "Default")
url += "&lang=" + encodeURIComponent(language) + "&lc=" + encodeURIComponent(language);
else
language = app.getLanguage(app.Settings.get("appearance", "locale")).substring(0, 2);

let response = await app.Utils.timeoutFetch(url, {
headers: {
Expand All @@ -58,7 +60,7 @@ app.OpenFoodFacts = {
// Multiple results (hide results where all nutrition values are undefined)
if (data.products !== undefined) {
data.products.forEach((x) => {
let item = app.OpenFoodFacts.parseItem(x, preferDataPer100g);
let item = app.OpenFoodFacts.parseItem(x, preferDataPer100g, language);
if (item != undefined) {
let nutritionValues = Object.values(item.nutrition).filter((v) => {return v != undefined});
if (nutritionValues.length != 0) result.push(item);
Expand All @@ -68,7 +70,7 @@ app.OpenFoodFacts = {

// Single result (presumably from a barcode)
if (data.product !== undefined) {
let item = app.OpenFoodFacts.parseItem(data.product, preferDataPer100g);
let item = app.OpenFoodFacts.parseItem(data.product, preferDataPer100g, language);
if (item != undefined) result.push(item);
}

Expand All @@ -79,19 +81,24 @@ app.OpenFoodFacts = {
});
},

parseItem: function(item, preferDataPer100g) {
parseItem: function(item, preferDataPer100g, preferredLanguage) {
const nutriments = app.nutriments; // Array of OFF nutriment names
const units = app.nutrimentUnits;

let result = {
"nutrition": {}
};

// Search for all keys containing 'item_name' to include local item names
for (let k in item) {
if (k.includes("product_name") && item[k].length > 1) {
result.name = he.decode(item[k]);
break;
// Get item name, try the preferred language first
let key = "product_name_" + preferredLanguage;
if (item[key] !== undefined && item[key].length > 1) {
result.name = he.decode(item[key]);
} else {
for (let k in item) {
if (k.includes("product_name") && item[k].length > 1) {
result.name = he.decode(item[k]);
break;
}
}
}

Expand Down Expand Up @@ -272,14 +279,11 @@ app.OpenFoodFacts = {

// Product language
let language = app.Settings.get("integration", "search-language") || undefined;
let lang = "en";

if (language != undefined && language != "Default")
lang = language;
string += "&lang=" + encodeURIComponent(language);
else
lang = app.getLanguage(app.Settings.get("appearance", "locale")).substring(0, 2);

string += "&lang=" + encodeURIComponent(lang);
string += "&lang=" + encodeURIComponent(app.getLanguage(app.Settings.get("appearance", "locale")).substring(0, 2));

// Product information
string += "&product_name_" + encodeURIComponent(lang) + "=" + encodeURIComponent(data.name);
Expand Down