Skip to content

Commit

Permalink
tld: handle mixed-type values in params arg to requests.get()
Browse files Browse the repository at this point in the history
"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()`.
  • Loading branch information
dgw committed Jun 21, 2023
1 parent 1f435e3 commit c24dfa2
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions sopel/modules/tld.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]+)')

Expand Down Expand Up @@ -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}),

This comment has been minimized.

Copy link
@dgw

dgw Jul 12, 2023

Author Member

This appears to be the cause of the bug I was bisecting when I tripped over 3ffe55c#r121445476

).json()
data_pages.append(tld_response["parse"]["text"])
# py <3.5 needs ValueError instead of more specific json.decoder.JSONDecodeError
Expand Down

0 comments on commit c24dfa2

Please sign in to comment.