From a49fd9f353d1d8d3acf9b84cb4f7e391564bdea8 Mon Sep 17 00:00:00 2001 From: lpofredc Date: Thu, 12 Aug 2021 11:44:58 +0200 Subject: [PATCH 1/3] fix: add generic user_agent on url_to_image --- apptax/taxonomie/filemanager.py | 12 +++++++++++- apptax/utils/genericfunctions.py | 4 ++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/apptax/taxonomie/filemanager.py b/apptax/taxonomie/filemanager.py index 6e4402d38..e5213e382 100644 --- a/apptax/taxonomie/filemanager.py +++ b/apptax/taxonomie/filemanager.py @@ -17,6 +17,8 @@ from flask import current_app +from apptax.utils.genericfunctions import get_version + try: from urllib.request import urlopen except Exception: @@ -231,11 +233,19 @@ def url_to_image(url): """ Récupération d'une image à partir d'une url """ - r = requests.get(url, stream=True) + # f = open(os.path.dirname(__file__) + '/../../VERSION', 'r') + # print('AAAAAAAAAAAAAA') + # for l in f.readlines(): + # print(l) + # print(f, f.readlines()[1]) + headers ={'user-agent': f'TaxHub {get_version()}'} + r = requests.get(url, stream=True, headers=headers) + logger.error(r.status_code) try: img = Image.open(io.BytesIO(r.content)) except IOError: raise Exception("Media is not an image") + print(url) return img diff --git a/apptax/utils/genericfunctions.py b/apptax/utils/genericfunctions.py index f7b57048c..b5695eb65 100644 --- a/apptax/utils/genericfunctions.py +++ b/apptax/utils/genericfunctions.py @@ -2,7 +2,11 @@ Fichier contenant des fonctions utilisées par l'ensemble de l'application ''' +import os +def get_version(): + version = open(os.path.dirname(__file__) + '/../../VERSION', 'r').readline().rstrip() + return version def calculate_offset_page(limit, offset, page): """ From 776cd015f1909c5307f8e6a8afc3aeb2ad6af54c Mon Sep 17 00:00:00 2001 From: lpofredc Date: Thu, 12 Aug 2021 12:08:29 +0200 Subject: [PATCH 2/3] refactor: user-agent generate function & cleanup --- apptax/taxonomie/filemanager.py | 11 ++--------- apptax/utils/genericfunctions.py | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/apptax/taxonomie/filemanager.py b/apptax/taxonomie/filemanager.py index e5213e382..acdf91594 100644 --- a/apptax/taxonomie/filemanager.py +++ b/apptax/taxonomie/filemanager.py @@ -17,7 +17,7 @@ from flask import current_app -from apptax.utils.genericfunctions import get_version +from apptax.utils.genericfunctions import generate_user_agent try: from urllib.request import urlopen @@ -233,19 +233,12 @@ def url_to_image(url): """ Récupération d'une image à partir d'une url """ - # f = open(os.path.dirname(__file__) + '/../../VERSION', 'r') - # print('AAAAAAAAAAAAAA') - # for l in f.readlines(): - # print(l) - # print(f, f.readlines()[1]) - headers ={'user-agent': f'TaxHub {get_version()}'} + headers ={'user-agent': generate_user_agent()} r = requests.get(url, stream=True, headers=headers) - logger.error(r.status_code) try: img = Image.open(io.BytesIO(r.content)) except IOError: raise Exception("Media is not an image") - print(url) return img diff --git a/apptax/utils/genericfunctions.py b/apptax/utils/genericfunctions.py index b5695eb65..7cf4b08fc 100644 --- a/apptax/utils/genericfunctions.py +++ b/apptax/utils/genericfunctions.py @@ -3,11 +3,25 @@ par l'ensemble de l'application ''' import os +import requests -def get_version(): +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): """ fonction qui calcul les paramètres From b456ba802e0c3c6ab0da2a01e9af7840324720cf Mon Sep 17 00:00:00 2001 From: lpofredc Date: Thu, 12 Aug 2021 13:41:18 +0200 Subject: [PATCH 3/3] refactor: add missing blank lines --- apptax/utils/genericfunctions.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/apptax/utils/genericfunctions.py b/apptax/utils/genericfunctions.py index 7cf4b08fc..b1832ddbc 100644 --- a/apptax/utils/genericfunctions.py +++ b/apptax/utils/genericfunctions.py @@ -14,6 +14,7 @@ def get_version() -> str: 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 @@ -22,6 +23,8 @@ def generate_user_agent() -> str: """ 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): """ fonction qui calcul les paramètres