Skip to content

Commit 0d64362

Browse files
authored
Merge pull request #1711 from wger-project/dependabot/pip/openfoodfacts-0.4.0
Bump openfoodfacts from 0.3.0 to 0.4.0
2 parents 0f43d24 + 5c05ac1 commit 0d64362

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ flower==2.0.1
2525
fontawesomefree~=6.5.1
2626
icalendar==5.0.13
2727
invoke==2.2.0
28-
openfoodfacts==0.3.0
28+
openfoodfacts==0.4.0
2929
pillow==10.3.0
3030
reportlab==4.2.0
3131
requests==2.32.3

wger/nutrition/sync.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323

2424
# Third Party
2525
import requests
26+
from openfoodfacts.images import (
27+
AWS_S3_BASE_URL,
28+
generate_image_path,
29+
)
2630

2731
# wger
2832
from wger.nutrition.api.endpoints import (
@@ -93,7 +97,7 @@ def fetch_image_from_wger_instance(ingredient):
9397
Image.from_json(ingredient, retrieved_image, image_data)
9498

9599

96-
def fetch_image_from_off(ingredient):
100+
def fetch_image_from_off(ingredient: Ingredient):
97101
"""
98102
See
99103
- https://openfoodfacts.github.io/openfoodfacts-server/api/how-to-download-images/
@@ -104,10 +108,16 @@ def fetch_image_from_off(ingredient):
104108
url = ingredient.source_url + '?fields=images,image_front_url'
105109
headers = wger_headers()
106110
try:
107-
product_data = requests.get(url, headers=headers).json()
111+
product_data = requests.get(url, headers=headers, timeout=3).json()
108112
except requests.JSONDecodeError:
109113
logger.warning(f'Could not decode JSON response from {url}')
110114
return
115+
except requests.ConnectTimeout as e:
116+
logger.warning(f'Connection timeout while trying to fetch {url}: {e}')
117+
return
118+
except requests.ReadTimeout as e:
119+
logger.warning(f'Read timeout while trying to fetch {url}: {e}')
120+
return
111121

112122
try:
113123
image_url: Optional[str] = product_data['product'].get('image_front_url')
@@ -120,24 +130,25 @@ def fetch_image_from_off(ingredient):
120130
return
121131
image_data = product_data['product']['images']
122132

123-
# Download the image file
124-
response = requests.get(image_url, headers=headers)
125-
if response.status_code != 200:
126-
logger.info(f'An error occurred! Status code: {response.status_code}')
127-
return
128-
129-
# Parse the file name, looks something like this:
130-
# https://images.openfoodfacts.org/images/products/00975957/front_en.5.400.jpg
133+
# Extract the image key from the url:
134+
# https://images.openfoodfacts.org/images/products/00975957/front_en.5.400.jpg -> "front_en"
131135
image_id: str = image_url.rpartition('/')[2].partition('.')[0]
132136

133-
# Retrieve the uploader name
137+
# Extract the uploader name
134138
try:
135139
image_id: str = image_data[image_id]['imgid']
136140
uploader_name: str = image_data[image_id]['uploader']
137141
except KeyError as e:
138142
logger.info('could not load all image information, skipping...', e)
139143
return
140144

145+
# Download image from amazon
146+
image_s3_url = f'{AWS_S3_BASE_URL}{generate_image_path(ingredient.code, image_id)}'
147+
response = requests.get(image_s3_url, headers=headers)
148+
if not response.ok:
149+
logger.info(f'Could not locate image on AWS! Status code: {response.status_code}')
150+
return
151+
141152
# Save to DB
142153
url = (
143154
f'https://world.openfoodfacts.org/cgi/product_image.pl?code={ingredient.code}&id={image_id}'

wger/nutrition/tests/test_tasks.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
# Standard Library
1616

1717
# Standard Library
18-
from unittest.mock import patch
18+
from unittest.mock import (
19+
ANY,
20+
patch,
21+
)
1922

2023
# wger
2124
from wger.core.tests.base_testcase import WgerTestCase
@@ -34,6 +37,7 @@
3437
class MockOffResponse:
3538
def __init__(self):
3639
self.status_code = 200
40+
self.ok = True
3741
self.content = b'2000'
3842

3943
# yapf: disable
@@ -159,9 +163,10 @@ def test_download_ingredient_off(self, mock_logger, mock_from_json, mock_request
159163
mock_request.assert_any_call(
160164
'https://world.openfoodfacts.org/api/v2/product/5055365635003.json?fields=images,image_front_url',
161165
headers=wger_headers(),
166+
timeout=ANY,
162167
)
163168
mock_request.assert_any_call(
164-
'https://images.openfoodfacts.org/images/products/00975957/front_en.5.400.jpg',
169+
'https://openfoodfacts-images.s3.eu-west-3.amazonaws.com/data/123/456/789/0987654321/12345.jpg',
165170
headers=wger_headers(),
166171
)
167172
mock_from_json.assert_called()

0 commit comments

Comments
 (0)