Skip to content

Commit

Permalink
Merge pull request #2414 from SnoopJ/bugfix/gh2412-support-query-string
Browse files Browse the repository at this point in the history
wikipedia: support query strings in Wikipedia URLs
  • Loading branch information
dgw authored Feb 20, 2023
2 parents 295a73f + 9229c20 commit 44af4a4
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions sopel/modules/wikipedia.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
from html.parser import HTMLParser
import logging
import re
from urllib.parse import quote, unquote, urlparse

from requests import get

from sopel import plugin
from sopel.config import types
from sopel.tools.web import quote, unquote


LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -321,26 +321,27 @@ def mw_image_description(server, image):
return desc


# Matches a wikipedia page (excluding spaces and #, but not /File: links), with a separate optional field for the section
@plugin.url(r'https?:\/\/([a-z]+(?:\.m)?\.wikipedia\.org)\/wiki\/((?!File\:)[^ #]+)#?([^ ]*)')
# Matches a Wikipedia link (excluding /File: pages)
@plugin.url(r'https?:\/\/([a-z]+(?:\.m)?\.wikipedia\.org)\/wiki\/((?!File\:)[^ ]+)')
@plugin.output_prefix(PLUGIN_OUTPUT_PREFIX)
def mw_info(bot, trigger, match=None):
"""Retrieves and outputs a snippet of the linked page."""
server = match.group(1)
query = unquote(match.group(2))
section = unquote(match.group(3))
page_info = urlparse(match.group(2))
article = unquote(page_info.path)
section = unquote(page_info.fragment)

if section:
if section.startswith('cite_note-'): # Don't bother trying to retrieve a snippet when cite-note is linked
say_snippet(bot, trigger, server, query, show_url=False)
say_snippet(bot, trigger, server, article, show_url=False)
elif section.startswith('/media'):
# gh2316: media fragments are usually images; try to get an image description
image = section[7:] # strip '/media' prefix in pre-3.9 friendly way
say_image_description(bot, trigger, server, image)
else:
say_section(bot, trigger, server, query, section)
say_section(bot, trigger, server, article, section)
else:
say_snippet(bot, trigger, server, query, show_url=False)
say_snippet(bot, trigger, server, article, show_url=False)


@plugin.command('wikipedia', 'wp')
Expand Down

0 comments on commit 44af4a4

Please sign in to comment.