Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wikipedia: support query strings in Wikipedia URLs #2414

Merged
merged 1 commit into from
Feb 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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