Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Extend script to update or create products

Roland Geideree0a47684676
diff --git a/extras/scripts/create_ingredients_from_foodfacts.py b/extras/scripts/create_ingredients_from_foodfacts.py
index f0ce5d8a0..ad40feca5 100644
--- a/extras/scripts/create_ingredients_from_foodfacts.py
+++ b/extras/scripts/create_ingredients_from_foodfacts.py
@@ -1,21 +1,41 @@
from pymongo import MongoClient
import os
-import sys
import django
+import sys
+
sys.path.insert(0, os.path.join('..', '..'))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
django.setup()
+from django.conf import settings # noqa: E402
+
from wger.nutrition.models import Ingredient # noqa: E402
from wger.core.models import Language # noqa: E402
-from django.utils import timezone # noqa: E402
client = MongoClient(port=27017)
db = client.off
-langs = ["en", "de", "es", "ru", "fr", "bg", "el", "nl", "no", "cs", "sv", "pt"]
-lang_objects = [Language.objects.filter(short_name=lang).first() for lang in langs]
+langs = [i[0] for i in settings.LANGUAGES]
+lang_objects = [Language.objects.get(short_name=lang) for lang in langs]
+
+# for lang in langs:
+# count = db.products.count_documents({'lang': lang, 'complete': 1})
+# total = db.products.count_documents({'lang': lang})
+# print(f'Lang {lang} has {count} completed products out of {total}')
-for product in db.products.find({'lang': {"$in": langs}}):
+# Lang de has 15667 completed products out of 52669
+# Lang es has 8757 completed products out of 186171
+# Lang ru has 505 completed products out of 2918
+# Lang fr has 85058 completed products out of 811715
+# Lang bg has 15 completed products out of 718
+# Lang el has 20 completed products out of 251
+# Lang nl has 750 completed products out of 5611
+# Lang no has 9 completed products out of 141
+# Lang cs has 123 completed products out of 789
+# Lang sv has 1340 completed products out of 2878
+# Lang pt has 526 completed products out of 3541
+
+
+for product in db.products.find({'lang': {"$in": langs}, 'complete': 1}):
lang = product['lang']
main_details = ['product_name', 'code']
@@ -23,8 +43,12 @@ for product in db.products.find({'lang': {"$in": langs}}):
name = product['product_name']
code = product['code']
- required = ['energy-kcal_100g', 'proteins_100g', 'carbohydrates_100g',
- 'sugars_100g', 'fat_100g', 'saturated-fat_100g']
+ required = ['energy-kcal_100g',
+ 'proteins_100g',
+ 'carbohydrates_100g',
+ 'sugars_100g',
+ 'fat_100g',
+ 'saturated-fat_100g']
if 'nutriments' in product and all(req in product['nutriments'] for req in required):
energy = product['nutriments']['energy-kcal_100g']
protein = product['nutriments']['proteins_100g']
@@ -50,26 +74,35 @@ for product in db.products.find({'lang': {"$in": langs}}):
source_name = "Open Food Facts"
source_url = f'https://world.openfoodfacts.org/api/v0/product/{code}.json'
+ ingredient_data = {
+ 'language': lang_objects[langs.index(lang)],
+ '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,
+ 'status': 2,
+ }
+
try:
- Ingredient.objects.create(
- language=lang_objects[langs.index(lang)],
- 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,
- status=2
- )
+ # Update an existing product (look-up key is the code) or create a new
+ # one. While this might not be the most efficient query (there will always
+ # be a SELECT first), it's ok because this script is run very rarely.
+ obj, created = Ingredient.objects.update_or_create(code=code, defaults=ingredient_data)
+
+ if created:
+ print(' -> added to the database')
+ else:
+ print(' -> updated')
+
except: # noqa: E722
continue