Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Completed import open food facts script

nikithamurikinatia6073d199069
diff --git a/extras/scripts/create_ingredients_from_foodfacts.py b/extras/scripts/create_ingredients_from_foodfacts.py
index bf790ad0f..721ae09e4 100644
--- a/extras/scripts/create_ingredients_from_foodfacts.py
+++ b/extras/scripts/create_ingredients_from_foodfacts.py
@@ -4,39 +4,67 @@ os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
import django
django.setup()
from wger.nutrition.models import Ingredient
+from wger.core.models import Language
+from django.utils import timezone
client = MongoClient(port=27017)
db = client.off
-# loop through each product and create Ingredient object
-# INGREDIENT = language, creation_date (auto to now), update_date (auto to now), name,
- # energy, protein, fiber, carbs, sugar, fat, saturated, sodium, code, source_name, source_url,
- # last_imported, common_name, image, category, brand
+langs = {"en": "English", "de": "German", "es": "Spanish",
+ "ru": "Russian", "fr": "French", "bg": "Bulgarian",
+ "el": "Greek", "nl": "Dutch", "no": "Norwegian", "cs": "Czech",
+ "sv": "Swedish", "pt": "Portuguese"}
-# print(db.getCollectionNames())
-for product in db.products.find({"lang": "de"}):
- name = product['product_name']
+for product in db.products.find({'lang': { "$in": list(langs.keys())}}).limit(10):
lang = product['lang']
- # nutriment data not always complete (should make these all optional)
- energy = product['nutriments']['energy-kcal_100g']
- protein = product['nutriments']['proteins_100g']
- fiber = product['nutriments']['fiber_100g']
- carbs = product['nutriments']['carbohydrates_100g']
- sugar = product['nutriments']['sugars_100g']
- fat = product['nutriments']['fat_100g']
- saturated = product['nutriments']['saturated-fat_100g']
- sodium = product['nutriments']['sodium_100g']
+
+ main_details = ['product_name', 'code']
+ if all(req in product for req in main_details):
+ name = product['product_name']
+ code = product['code']
+
+ required = ['energy-kcal_100g', 'proteins_100g', 'carbohydrates_100g',
+ 'sugars_100g', 'fat_100g', 'saturated-fat_100g']
+ if all(req in product['nutriments'] for req in required):
+ energy = product['nutriments']['energy-kcal_100g']
+ protein = product['nutriments']['proteins_100g']
+ carbs = product['nutriments']['carbohydrates_100g']
+ sugars = product['nutriments']['sugars_100g']
+ fat = product['nutriments']['fat_100g']
+ saturated = product['nutriments']['saturated-fat_100g']
+ else:
+ continue
+
+ # these are optional
+ sodium = product['nutriments'].get('sodium_100g', None)
+ fibre = product['nutriments'].get('fiber_100g', None)
+ common_name = product.get('generic_name', None)
+ brand = product.get('brands', None)
+
+ source_name = "Open Food Facts"
+ source_url = f'https://world.openfoodfacts.org/api/v0/product/{code}.json'
- code = product['code']
- # source_name =
- # source_url =
- # last_imported = default today?
- common_name = product['generic_name'] # often empty
- # image =
- # category =
- brand = product['brands']
- print(name)
- break
-# english, german, bulgarian, greek, spanish, russian, dutch, portuguese, czech, swedish, norwegian, french
-
-# Ingredient.create(blah)
+ lang_object = Language.objects.create(
+ short_name = lang,
+ full_name = langs[lang]
+ )
+
+ Ingredient.objects.create(
+ language = lang_object,
+ creation_date = timezone.now(),
+ update_date = timezone.now(),
+ name = name,
+ energy = energy,
+ protein = protein,
+ carbohydrates = carbs,
+ carbohydrates_sugar = sugars,
+ fat = fat,
+ fat_saturated = saturated,
+ fibres = fibre,
+ sodium = sodium,
+ code = code,
+ source_name = source_name,
+ source_url = source_url,
+ common_name = common_name,
+ brand = brand
+ )
diff --git a/extras/scripts/import_foodfacts.sh b/extras/scripts/import_foodfacts.sh
index f685f1f6e..48cc6fa7e 100644
--- a/extras/scripts/import_foodfacts.sh
+++ b/extras/scripts/import_foodfacts.sh
@@ -2,6 +2,28 @@
# -*- coding: utf-8 -*-
+# This file is part of wger Workout Manager.
+#
+# wger Workout Manager is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# wger Workout Manager is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+
+"""
+Simple script that imports and loads the Open Food Facts database into the
+ingredients database
+
+NOTE: The file is VERY large (17 GB), so it takes a long time (> 3 hours) to run
+and create all ingredients
+"""
+
wget https://static.openfoodfacts.org/data/openfoodfacts-mongodbdump.tar.gz
tar xzvf openfoodfacts-mongodbdump.tar.gz
@@ -11,4 +33,6 @@ mongorestore -d off -c products tmp/mongo_dump/off/products.bson
python create_ingredients_from_foodfacts.py
-rm openfoodfacts-mongodbdump.tar.gz
\ No newline at end of file
+rm $(pwd)/openfoodfacts-mongodbdump.tar.gz
+rm -r $(pwd)/dump
+rm -r /tmp
\ No newline at end of file
diff --git a/wger/nutrition/forms.py b/wger/nutrition/forms.py
index 95ceaaa7b..31fa1b493 100644
--- a/wger/nutrition/forms.py
+++ b/wger/nutrition/forms.py
@@ -276,7 +276,6 @@ class IngredientForm(forms.ModelForm):
'fat_saturated',
'fibres',
'sodium',
- 'image', # optional
'license',
'license_author']
widgets = {'category': forms.TextInput}
@@ -306,7 +305,6 @@ class IngredientForm(forms.ModelForm):
),
'fibres',
'sodium',
- 'image',
'license',
'license_author'
)
diff --git a/wger/nutrition/models.py b/wger/nutrition/models.py
index 74d0587f9..74b63febf 100644
--- a/wger/nutrition/models.py
+++ b/wger/nutrition/models.py
@@ -401,11 +401,6 @@ class Ingredient(AbstractSubmissionModel, AbstractLicenseModel, models.Model):
verbose_name=_('Common name of product'),
null=True,
blank=True)
-
- image = models.ImageField(verbose_name=_('Image'),
- help_text=_('Only PNG and JPEG formats are supported'),
- null=True,
- blank=True)
category = models.ForeignKey(IngredientCategory,
verbose_name=_('Category'),