From f6dc861565483192acb6c25d9330160dd0f482ea Mon Sep 17 00:00:00 2001 From: Etienne Trimaille <gustrimaille@yahoo.fr> Date: Wed, 15 May 2024 10:30:07 +0200 Subject: [PATCH] Fix User-Agent when downloading files from plugins.qgis.org --- qgis_plugin_manager/remote.py | 18 ++++++++++++++++-- qgis_plugin_manager/utils.py | 2 ++ test/test_remote.py | 5 +++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/qgis_plugin_manager/remote.py b/qgis_plugin_manager/remote.py index afd2f9d..5dc25f0 100644 --- a/qgis_plugin_manager/remote.py +++ b/qgis_plugin_manager/remote.py @@ -4,6 +4,7 @@ import base64 import os +import platform import re import shutil import urllib @@ -17,6 +18,7 @@ from qgis_plugin_manager.definitions import Level, Plugin from qgis_plugin_manager.utils import ( + DEFAULT_QGIS_VERSION, current_user, restart_qgis_server, similar_names, @@ -35,6 +37,18 @@ def __init__(self, folder: Path, qgis_version: Optional[str] = None): self.setting_error = False self.qgis_version = qgis_version + def user_agent(self) -> str: + """ User agent. """ + # https://github.com/3liz/qgis-plugin-manager/issues/66 + # https://lists.osgeo.org/pipermail/qgis-user/2024-May/054439.html + if not self.qgis_version: + qgis_version = f"{DEFAULT_QGIS_VERSION}00" + else: + qgis_version = self.qgis_version + + qgis_version = qgis_version.replace(".", "") + return f"Mozilla/5.0 QGIS/{qgis_version}/{platform.system()}" + def remote_is_ready(self) -> bool: """ Return if the remote is ready to be parsed. """ if to_bool(os.getenv("QGIS_PLUGIN_MANAGER_SKIP_SOURCES_FILE"), False): @@ -174,7 +188,7 @@ def update(self) -> bool: print(f"Downloading {self.public_remote_name(server)}…") url, login, password = self.credentials(server) headers = { - 'User-Agent': 'Mozilla/5.0', + 'User-Agent': self.user_agent(), } if login: token = base64.b64encode(f"{login}:{password}".encode()) @@ -414,7 +428,7 @@ def _download_zip( else: headers = { - 'User-Agent': 'Mozilla/5.0', + 'User-Agent': self.user_agent(), } request = urllib.request.Request(url, headers=headers) result = False diff --git a/qgis_plugin_manager/utils.py b/qgis_plugin_manager/utils.py index 512531a..31bc403 100644 --- a/qgis_plugin_manager/utils.py +++ b/qgis_plugin_manager/utils.py @@ -122,6 +122,8 @@ def qgis_server_version() -> str: """ try: from qgis.core import Qgis + + # 3.34.6 return Qgis.QGIS_VERSION.split('-')[0] except ImportError: print( diff --git a/test/test_remote.py b/test/test_remote.py index 034b3ed..2fa2ee5 100644 --- a/test/test_remote.py +++ b/test/test_remote.py @@ -73,6 +73,11 @@ def test_qgis_dev_version(self): self.assertListEqual(["3", "22", "11"], Remote.check_qgis_dev_version('3.22.11')) self.assertListEqual(["3", "24", "0"], Remote.check_qgis_dev_version('3.23.0')) + def test_user_agent(self): + """ Test the User-Agent. """ + self.remote = Remote(Path('fixtures/xml_files/lizmap')) + self.assertIn("Mozilla/5.0 QGIS/", self.remote.user_agent()) + def test_parse_url(self): """ Test to parse a URL for login&password. """ self.assertTupleEqual(