23
23
24
24
# Third Party
25
25
import requests
26
+ from openfoodfacts .images import (
27
+ AWS_S3_BASE_URL ,
28
+ generate_image_path ,
29
+ )
26
30
27
31
# wger
28
32
from wger .nutrition .api .endpoints import (
@@ -93,7 +97,7 @@ def fetch_image_from_wger_instance(ingredient):
93
97
Image .from_json (ingredient , retrieved_image , image_data )
94
98
95
99
96
- def fetch_image_from_off (ingredient ):
100
+ def fetch_image_from_off (ingredient : Ingredient ):
97
101
"""
98
102
See
99
103
- https://openfoodfacts.github.io/openfoodfacts-server/api/how-to-download-images/
@@ -104,10 +108,16 @@ def fetch_image_from_off(ingredient):
104
108
url = ingredient .source_url + '?fields=images,image_front_url'
105
109
headers = wger_headers ()
106
110
try :
107
- product_data = requests .get (url , headers = headers ).json ()
111
+ product_data = requests .get (url , headers = headers , timeout = 3 ).json ()
108
112
except requests .JSONDecodeError :
109
113
logger .warning (f'Could not decode JSON response from { url } ' )
110
114
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
111
121
112
122
try :
113
123
image_url : Optional [str ] = product_data ['product' ].get ('image_front_url' )
@@ -120,24 +130,25 @@ def fetch_image_from_off(ingredient):
120
130
return
121
131
image_data = product_data ['product' ]['images' ]
122
132
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"
131
135
image_id : str = image_url .rpartition ('/' )[2 ].partition ('.' )[0 ]
132
136
133
- # Retrieve the uploader name
137
+ # Extract the uploader name
134
138
try :
135
139
image_id : str = image_data [image_id ]['imgid' ]
136
140
uploader_name : str = image_data [image_id ]['uploader' ]
137
141
except KeyError as e :
138
142
logger .info ('could not load all image information, skipping...' , e )
139
143
return
140
144
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
+
141
152
# Save to DB
142
153
url = (
143
154
f'https://world.openfoodfacts.org/cgi/product_image.pl?code={ ingredient .code } &id={ image_id } '
0 commit comments