Skip to content

Commit

Permalink
Fix User-Agent when downloading files from plugins.qgis.org
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustry committed May 15, 2024
1 parent 24ecb77 commit 3de2501
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
18 changes: 16 additions & 2 deletions qgis_plugin_manager/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import base64
import os
import platform
import re
import shutil
import urllib
Expand All @@ -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,
Expand All @@ -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):
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions qgis_plugin_manager/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
5 changes: 5 additions & 0 deletions test/test_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 3de2501

Please sign in to comment.