jenkins-bot submitted this change.

View Change

Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
[IMPR] use default timeout uf 30 s in MementoClient.request_head

Memento requests needs 17-22 seconds. Therefore increase the default
timeout. Also:

- add a timeout parameter to get_closest_memento_url function
- update documentation
- update tests/memento_tests.py

Bug: T382943
Change-Id: I659c850add79d569cea7ee81bb2981088a7a1078
---
M pywikibot/data/memento.py
M tests/memento_tests.py
2 files changed, 28 insertions(+), 9 deletions(-)

diff --git a/pywikibot/data/memento.py b/pywikibot/data/memento.py
index 9bd5110..337f36c 100644
--- a/pywikibot/data/memento.py
+++ b/pywikibot/data/memento.py
@@ -9,7 +9,7 @@
# Parts of MementoClient class codes are
# licensed under the BSD open source software license.
#
-# (C) Pywikibot team, 2015-2024
+# (C) Pywikibot team, 2015-2025
#
# Distributed under the terms of the MIT license.
#
@@ -263,13 +263,17 @@
timeout: int | None = None) -> requests.Response:
"""Makes HEAD requests.

+ .. versionchanged:: 10.0
+ The default timout was increased from 9 to 30 seconds.
+
:param uri: the uri for the request.
:param accept_datetime: the accept-datetime in the http format.
:param follow_redirects: Toggle to follow redirects. False by
default, so does not follow any redirects.
:param session: the request session object to avoid opening new
connections for every request.
- :param timeout: the timeout for the HTTP requests.
+ :param timeout: the timeout for the HTTP requests. Default is
+ 30 s.
:return: the response object.
:raises ValueError: Only HTTP URIs are supported
"""
@@ -285,7 +289,7 @@
response = session.head(uri,
headers=headers,
allow_redirects=follow_redirects,
- timeout=timeout or 9)
+ timeout=timeout or 30)
except (InvalidSchema, MissingSchema):
raise ValueError(
f'Only HTTP URIs are supported, URI {uri} unrecognized.')
@@ -308,8 +312,23 @@

def get_closest_memento_url(url: str,
when: datetime | None = None,
- timegate_uri: str | None = None):
- """Get most recent memento for url."""
+ timegate_uri: str | None = None,
+ *,
+ timeout: int | None = None):
+ """Get most recent memento for url.
+
+ .. versionadded:: 10.0
+ The *timeout* parameter.
+
+ :param url: The input http url.
+ :param when: The datetime object of the accept datetime. The current
+ datetime is used if none is provided.
+ :param timegate_uri: A valid HTTP base uri for a timegate. Must
+ start with http(s):// and end with a /. Default value is
+ http://timetravel.mementoweb.org/timegate/.
+ :param timeout: The timeout value for the HTTP connection. If None,
+ a default value is used in :meth:`MementoClient.request_head`.
+ """
if not when:
when = datetime.now()

@@ -320,7 +339,7 @@
retry_count = 0
while retry_count <= config.max_retries:
try:
- memento_info = mc.get_memento_info(url, when)
+ memento_info = mc.get_memento_info(url, when, timeout)
break
except (requests.ConnectionError, MementoClientException) as e:
error = e
diff --git a/tests/memento_tests.py b/tests/memento_tests.py
index 2a56d80..fdeb5e4 100755
--- a/tests/memento_tests.py
+++ b/tests/memento_tests.py
@@ -1,7 +1,7 @@
#!/usr/bin/env python3
"""memento client test module."""
#
-# (C) Pywikibot team, 2015-2022
+# (C) Pywikibot team, 2015-2025
#
# Distributed under the terms of the MIT license.
#
@@ -46,13 +46,13 @@
hostname = timegate_uri.replace('gate/', 'map/json/http://google.com')

def test_newest(self):
- """Test Archive for newest https://google.com."""
+ """Test Archive for an old https://google.com."""
dt = '20220715'
archivedversion = self._get_archive_url('https://google.com',
date_string=dt)
parsed = urlparse(archivedversion)
self.assertIn(parsed.scheme, ['http', 'https'])
- self.assertEqual(parsed.netloc, 'arquivo.pt')
+ self.assertEqual(parsed.netloc, 'wayback.library.yorku.ca')


class TestMementoDefault(MementoTestCase):

To view, visit change 1108042. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I659c850add79d569cea7ee81bb2981088a7a1078
Gerrit-Change-Number: 1108042
Gerrit-PatchSet: 1
Gerrit-Owner: Xqt <info@gno.de>
Gerrit-Reviewer: Xqt <info@gno.de>
Gerrit-Reviewer: jenkins-bot