From c24dfa209c3d2965939f227992c16168bc9fb69c Mon Sep 17 00:00:00 2001 From: dgw Date: Mon, 12 Jun 2023 15:11:23 -0500 Subject: [PATCH] tld: handle mixed-type values in `params` arg to `requests.get()` "Handle" as in, move the common params to a type-hinted module variable and merge a copy of them with the page title in `_update_tld_data()`. --- sopel/modules/tld.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/sopel/modules/tld.py b/sopel/modules/tld.py index eafc35177..954c9d1b5 100644 --- a/sopel/modules/tld.py +++ b/sopel/modules/tld.py @@ -13,7 +13,7 @@ from html.parser import HTMLParser import logging import re -from typing import Dict +from typing import Dict, Union import pytz import requests @@ -38,6 +38,13 @@ def _strptime_as_utc(time_string): 'List_of_Internet_top-level_domains', 'Country_code_top-level_domain', ] +WIKI_API_PARAMS: Dict[str, Union[str, int]] = { + "action": "parse", + "format": "json", + "prop": "text", + "utf8": 1, + "formatversion": 2, +} r_tld = re.compile(r'^\.(\S+)') r_idn = re.compile(r'^(xn--[A-Za-z0-9]+)') @@ -257,14 +264,7 @@ def _update_tld_data(bot, which, force=False): # https://www.mediawiki.org/wiki/Special:MyLanguage/API:Get_the_contents_of_a_page tld_response = requests.get( "https://en.wikipedia.org/w/api.php", - params={ - "action": "parse", - "format": "json", - "prop": "text", - "utf8": 1, - "formatversion": 2, - "page": title, - }, + params=WIKI_API_PARAMS.copy().update({"page": title}), ).json() data_pages.append(tld_response["parse"]["text"]) # py <3.5 needs ValueError instead of more specific json.decoder.JSONDecodeError