Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: error 403 on media from wikimedia #276

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion apptax/taxonomie/filemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

from flask import current_app

from apptax.utils.genericfunctions import generate_user_agent

try:
from urllib.request import urlopen
except Exception:
Expand Down Expand Up @@ -231,7 +233,8 @@ def url_to_image(url):
"""
Récupération d'une image à partir d'une url
"""
r = requests.get(url, stream=True)
headers ={'user-agent': generate_user_agent()}
r = requests.get(url, stream=True, headers=headers)
try:
img = Image.open(io.BytesIO(r.content))
except IOError:
Expand Down
21 changes: 21 additions & 0 deletions apptax/utils/genericfunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,27 @@
Fichier contenant des fonctions utilisées
par l'ensemble de l'application
'''
import os
import requests

def get_version() -> str:
"""Get TaxHub version from VERSION file

Returns:
str: TaxHub version
"""
version = open(os.path.dirname(__file__) + '/../../VERSION', 'r').readline().rstrip()
return version


def generate_user_agent() -> str:
"""Generate a generic user-agent description for requests

Returns:
str: generic user-agent
"""
user_agent = f'TaxHub/{get_version()} (https://github.com/PnX-SI/TaxHub) python-requests/{requests.__version__}'
return user_agent


def calculate_offset_page(limit, offset, page):
Expand Down