Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add helper function to return the user agent used when performing HTTP requests

Roland Geiderff7a09dacd9e
diff --git a/wger/exercises/management/commands/download-exercise-images.py b/wger/exercises/management/commands/download-exercise-images.py
index ad00ee58e..3a21676d8 100644
--- a/wger/exercises/management/commands/download-exercise-images.py
+++ b/wger/exercises/management/commands/download-exercise-images.py
@@ -33,14 +33,13 @@ from django.core.validators import URLValidator
# Third Party
import requests
-from requests.utils import default_user_agent
# wger
-from wger import get_version
from wger.exercises.models import (
ExerciseBase,
ExerciseImage,
)
+from wger.utils.requests import wger_user_agent
IMAGE_API = "{0}/api/v2/exerciseimage/?status=2"
@@ -85,7 +84,7 @@ class Command(BaseCommand):
except ValidationError:
raise CommandError('Please enter a valid URL')
- headers = {'User-agent': default_user_agent(f'wger/{get_version()} + requests')}
+ headers = {'User-agent': wger_user_agent()}
# Get all images
page = 1
diff --git a/wger/exercises/management/commands/download-exercise-videos.py b/wger/exercises/management/commands/download-exercise-videos.py
index 4eedd1a7f..c5cea42db 100644
--- a/wger/exercises/management/commands/download-exercise-videos.py
+++ b/wger/exercises/management/commands/download-exercise-videos.py
@@ -33,14 +33,13 @@ from django.core.validators import URLValidator
# Third Party
import requests
-from requests.utils import default_user_agent
# wger
-from wger import get_version
from wger.exercises.models import (
ExerciseBase,
ExerciseVideo,
)
+from wger.utils.requests import wger_user_agent
VIDEO_API = "{0}/api/v2/video/"
@@ -78,7 +77,7 @@ class Command(BaseCommand):
except ValidationError:
raise CommandError('Please enter a valid URL')
- headers = {'User-agent': default_user_agent('wger/{} + requests'.format(get_version()))}
+ headers = {'User-agent': wger_user_agent()}
# Get all videos
page = 1
diff --git a/wger/exercises/management/commands/sync-exercises.py b/wger/exercises/management/commands/sync-exercises.py
index 25ae541c4..5eaa174b8 100644
--- a/wger/exercises/management/commands/sync-exercises.py
+++ b/wger/exercises/management/commands/sync-exercises.py
@@ -24,10 +24,8 @@ from django.core.validators import URLValidator
# Third Party
import requests
-from requests.utils import default_user_agent
# wger
-from wger import get_version
from wger.exercises.models import (
Equipment,
Exercise,
@@ -35,6 +33,7 @@ from wger.exercises.models import (
ExerciseCategory,
Muscle,
)
+from wger.utils.requests import wger_user_agent
EXERCISE_API = "{0}/api/v2/exerciseinfo/?limit=100"
@@ -77,7 +76,7 @@ class Command(BaseCommand):
except ValidationError:
raise CommandError('Please enter a valid URL')
- headers = {'User-agent': default_user_agent('wger/{} + requests'.format(get_version()))}
+ headers = {'User-agent': wger_user_agent()}
self.sync_categories(headers, remote_url)
self.sync_muscles(headers, remote_url)
self.sync_equipment(headers, remote_url)
diff --git a/wger/nutrition/management/commands/download-ingredient-images.py b/wger/nutrition/management/commands/download-ingredient-images.py
index 20293f00d..8f1f950b7 100644
--- a/wger/nutrition/management/commands/download-ingredient-images.py
+++ b/wger/nutrition/management/commands/download-ingredient-images.py
@@ -28,7 +28,6 @@ from django.core.validators import URLValidator
# Third Party
import requests
-from requests.utils import default_user_agent
# wger
from wger import get_version
diff --git a/wger/nutrition/tasks.py b/wger/nutrition/tasks.py
index 2e634c8be..4afacfd08 100644
--- a/wger/nutrition/tasks.py
+++ b/wger/nutrition/tasks.py
@@ -24,12 +24,11 @@ from django.db import IntegrityError
# Third Party
import requests
from celery import shared_task
-from requests.utils import default_user_agent
# wger
-from wger import get_version
from wger.nutrition.models import Image
from wger.nutrition.models.sources import Source
+from wger.utils.requests import wger_user_agent
logger = logging.getLogger(__name__)
@@ -40,12 +39,18 @@ def fetch_ingredient_image(pk: int):
"""
Fetches the ingredient image from Open Food Facts servers if it is not available locally
- Returns the image if it was fetched
+ Returns the image if it is already present in the DB
"""
+ fetch_ingredient_image_function(pk)
+
+
+def fetch_ingredient_image_function(pk: int):
# wger
from wger.nutrition.models import Ingredient
ingredient = Ingredient.objects.get(pk=pk)
+ logger.info(f'Fetching image for ingredient {pk}')
+
if hasattr(ingredient, 'image'):
return
@@ -60,22 +65,20 @@ def fetch_ingredient_image(pk: int):
# Everything looks fine, go ahead
logger.info(f'Trying to fetch image from OFF for {ingredient.name} (UUID: {ingredient.uuid})')
- headers = {
- 'User-agent': default_user_agent(f'wger/{get_version()} - https://github.com/wger-project')
- }
+ headers = {'User-agent': wger_user_agent()}
# Fetch the product data
product_data = requests.get(ingredient.source_url, headers=headers).json()
image_url: Optional[str] = product_data['product'].get('image_front_url')
if not image_url:
+ logger.info('Product data has no "image_front_url" key')
return
-
image_data = product_data['product']['images']
# Download the image file
response = requests.get(image_url, headers=headers)
if response.status_code != 200:
- logger.info('An error occurred!')
+ logger.info(f'An error occurred! Status code: {response.status_code}')
return
# Parse the file name, looks something like this:
diff --git a/wger/utils/requests.py b/wger/utils/requests.py
new file mode 100644
index 000000000..4ed395869
--- /dev/null
+++ b/wger/utils/requests.py
@@ -0,0 +1,20 @@
+# 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
+
+# wger
+from wger import get_version
+
+
+def wger_user_agent():
+ return f'wger/{get_version()} - https://github.com/wger-project'