From 62748c7c0511aae9f6c274f8397faae4231ff116 Mon Sep 17 00:00:00 2001 From: TheresNoTime Date: Fri, 10 Jul 2026 13:24:24 +0100 Subject: [PATCH 1/2] Add user agent to globaluserinfo call --- TWLight/users/helpers/editor_data.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/TWLight/users/helpers/editor_data.py b/TWLight/users/helpers/editor_data.py index ea3fc4878a..5e52f78994 100644 --- a/TWLight/users/helpers/editor_data.py +++ b/TWLight/users/helpers/editor_data.py @@ -10,6 +10,13 @@ logger = logging.getLogger(__name__) +# Wikimedia's User-Agent policy requires a descriptive User-Agent with contact +# information; the generic Python-urllib default is blocked with HTTP 403. +# https://meta.wikimedia.org/wiki/User-Agent_policy +TWLIGHT_USER_AGENT = "TWLight/1.0 (https://wikipedialibrary.wmflabs.org/; {contact}) Python-urllib".format( + contact=os.environ.get("TWLIGHT_ERROR_MAILTO", "wikipedialibrary@wikimedia.org") +) + def editor_global_userinfo(wp_sub: int): """ @@ -63,7 +70,8 @@ def _get_user_info_request(wp_param_name: str, wp_param: str): query = "{endpoint}?action=query&meta=globaluserinfo&{wp_param_name}={wp_param}&guiprop=editcount|merged&format=json&formatversion=2".format( endpoint=endpoint, wp_param_name=wp_param_name, wp_param=wp_param ) - return json.loads(urllib.request.urlopen(query).read()) + request = urllib.request.Request(query, headers={"User-Agent": TWLIGHT_USER_AGENT}) + return json.loads(urllib.request.urlopen(request).read()) def editor_reg_date(identity: dict, global_userinfo: dict): From ed950c7cce37f292a75cd4eb1826c2e749c1345b Mon Sep 17 00:00:00 2001 From: TheresNoTime Date: Fri, 10 Jul 2026 13:39:11 +0100 Subject: [PATCH 2/2] Add env to UA --- TWLight/users/helpers/editor_data.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/TWLight/users/helpers/editor_data.py b/TWLight/users/helpers/editor_data.py index 5e52f78994..4b65325e32 100644 --- a/TWLight/users/helpers/editor_data.py +++ b/TWLight/users/helpers/editor_data.py @@ -13,8 +13,11 @@ # Wikimedia's User-Agent policy requires a descriptive User-Agent with contact # information; the generic Python-urllib default is blocked with HTTP 403. # https://meta.wikimedia.org/wiki/User-Agent_policy -TWLIGHT_USER_AGENT = "TWLight/1.0 (https://wikipedialibrary.wmflabs.org/; {contact}) Python-urllib".format( - contact=os.environ.get("TWLIGHT_ERROR_MAILTO", "wikipedialibrary@wikimedia.org") +# The environment (local/staging/production) is included so Wikimedia can +# distinguish development and staging traffic from production. +TWLIGHT_USER_AGENT = "TWLight/1.0 ({env}; https://wikipedialibrary.wmflabs.org/; {contact}) Python-urllib".format( + env=settings.TWLIGHT_ENV or "unknown", + contact=os.environ.get("TWLIGHT_ERROR_MAILTO", "wikipedialibrary@wikimedia.org"), )