Skip to content

Commit 120477f

Browse files
authored
Merge pull request #2575 from SnoopJ/bugfix/gh2573-fix-urlparse-usage
wikipedia: fix urlparse usage
2 parents 5f860bb + 43b56a4 commit 120477f

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

sopel/builtins/wikipedia.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,12 +327,22 @@ def mw_image_description(server, image):
327327
def mw_info(bot, trigger, match=None):
328328
"""Retrieves and outputs a snippet of the linked page."""
329329
server = match.group(1)
330-
page_info = urlparse(match.group(2))
331-
article = unquote(page_info.path)
330+
page_info = urlparse(match.group(0))
331+
# in Python 3.9+ this can be str.removeprefix() instead, but we're confident that
332+
# "/wiki/" is at the start of the path anyway since it's part of the pattern
333+
trim_offset = len("/wiki/")
334+
article = unquote(page_info.path)[trim_offset:]
332335
section = unquote(page_info.fragment)
333336

337+
if article.startswith("Special:"):
338+
# The MediaWiki query API does not include pages in the Special:
339+
# namespace, so there's no point bothering when we know this will error
340+
LOGGER.debug("Ignoring page in Special: namespace")
341+
return False
342+
334343
if section:
335-
if section.startswith('cite_note-'): # Don't bother trying to retrieve a snippet when cite-note is linked
344+
if section.startswith('cite_note-'):
345+
# Don't bother trying to retrieve a section snippet if cite-note is linked
336346
say_snippet(bot, trigger, server, article, show_url=False)
337347
elif section.startswith('/media'):
338348
# gh2316: media fragments are usually images; try to get an image description
@@ -363,6 +373,11 @@ def wikipedia(bot, trigger):
363373
if not query:
364374
bot.reply('What do you want me to look up?')
365375
return plugin.NOLIMIT
376+
377+
if query.startswith("Special:"):
378+
bot.reply("Sorry, the MediaWiki API doesn't support querying the Special: namespace.")
379+
return False
380+
366381
server = lang + '.wikipedia.org'
367382
query = mw_search(server, query, 1)
368383
if not query:

0 commit comments

Comments
 (0)