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: implement language preferences in DB for channels & nicks #1916

Merged
merged 2 commits into from
Aug 14, 2020
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
100 changes: 79 additions & 21 deletions sopel/modules/wikipedia.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

from requests import get

from sopel import module
from sopel.config.types import StaticSection, ValidatedAttribute
from sopel.module import commands, example, NOLIMIT, output_prefix, url
from sopel.tools.web import quote, unquote

try: # TODO: Remove fallback when dropping py2
Expand Down Expand Up @@ -88,7 +88,10 @@ class WikipediaSection(StaticSection):
default_lang = ValidatedAttribute('default_lang', default='en')
"""The default language to find articles from (same as Wikipedia language subdomain)."""
lang_per_channel = ValidatedAttribute('lang_per_channel')
"""List of ``#channel:langcode`` pairs to define Wikipedia language per channel."""
"""List of ``#channel:langcode`` pairs to define Wikipedia language per channel.

Deprecated: Will be removed in Sopel 8. Use ``.wpclang`` to manage per-channel language settings.
"""


def setup(bot):
Expand All @@ -100,7 +103,6 @@ def configure(config):
| name | example | purpose |
| ---- | ------- | ------- |
| default\\_lang | en | The default language to find articles from (same as Wikipedia language subdomain) |
| lang\\_per\\_channel | #YourPants:en,#TusPantalones:es | List of #channel:langcode pairs to define Wikipedia language per channel |
"""
config.define_section('wikipedia', WikipediaSection)
config.wikipedia.configure_setting(
Expand All @@ -109,6 +111,26 @@ def configure(config):
)


def choose_lang(bot, trigger):
"""Determine what language to use for queries based on sender/context."""
user_lang = bot.db.get_nick_value(trigger.nick, 'wikipedia_lang')
if user_lang:
return user_lang

if not trigger.sender.is_nick():
channel_lang = bot.db.get_channel_value(trigger.sender, 'wikipedia_lang')
if channel_lang:
return channel_lang

if bot.config.wikipedia.lang_per_channel:
customlang = re.search('(' + trigger.sender + r'):(\w+)',
bot.config.wikipedia.lang_per_channel)
if customlang is not None:
return customlang.group(2)

return bot.config.wikipedia.default_lang


def mw_search(server, query, num):
"""Search a MediaWiki site

Expand Down Expand Up @@ -216,8 +238,8 @@ def mw_section(server, query, section):


# Matches a wikipedia page (excluding spaces and #, but not /File: links), with a separate optional field for the section
@url(r'https?:\/\/([a-z]+\.wikipedia\.org)\/wiki\/((?!File\:)[^ #]+)#?([^ ]*)')
@output_prefix('[WIKIPEDIA] ')
@module.url(r'https?:\/\/([a-z]+\.wikipedia\.org)\/wiki\/((?!File\:)[^ #]+)#?([^ ]*)')
@module.output_prefix('[WIKIPEDIA] ')
def mw_info(bot, trigger, match=None):
"""Retrieves and outputs a snippet of the linked page."""
if match.group(3):
Expand All @@ -229,24 +251,15 @@ def mw_info(bot, trigger, match=None):
say_snippet(bot, trigger, match.group(1), unquote(match.group(2)), show_url=False)


@commands('w', 'wiki', 'wik')
@example('.w San Francisco')
@output_prefix('[WIKIPEDIA] ')
@module.commands('w', 'wiki', 'wik')
@module.example('.w San Francisco')
@module.output_prefix('[WIKIPEDIA] ')
def wikipedia(bot, trigger):
lang = bot.config.wikipedia.default_lang

# change lang if channel has custom language set
if (trigger.sender and not trigger.sender.is_nick() and
bot.config.wikipedia.lang_per_channel):
customlang = re.search('(' + trigger.sender + r'):(\w+)',
bot.config.wikipedia.lang_per_channel)
if customlang is not None:
lang = customlang.group(2)

if trigger.group(2) is None:
bot.reply("What do you want me to look up?")
return NOLIMIT
return module.NOLIMIT

lang = choose_lang(bot, trigger)
query = trigger.group(2)
args = re.search(r'^-([a-z]{2,12})\s(.*)', query)
if args is not None:
Expand All @@ -255,12 +268,57 @@ def wikipedia(bot, trigger):

if not query:
bot.reply('What do you want me to look up?')
return NOLIMIT
return module.NOLIMIT
server = lang + '.wikipedia.org'
query = mw_search(server, query, 1)
if not query:
bot.reply("I can't find any results for that.")
return NOLIMIT
return module.NOLIMIT
else:
query = query[0]
say_snippet(bot, trigger, server, query)


@module.commands('wplang')
@module.example('.wplang pl')
def wplang(bot, trigger):
if not trigger.group(3):
bot.reply(
"Your current Wikipedia language is: {}"
.format(
bot.db.get_nick_value(
trigger.nick, 'wikipedia_lang',
bot.config.wikipedia.default_lang)
)
)
else:
bot.db.set_nick_value(trigger.nick, 'wikipedia_lang', trigger.group(3))
bot.reply(
"Set your Wikipedia language to: {}"
.format(trigger.group(3))
)


@module.commands('wpclang')
@module.example('.wpclang ja')
@module.require_chanmsg()
def wpclang(bot, trigger):
if not (trigger.admin or bot.channels[trigger.sender.lower()].privileges[trigger.nick.lower()] >= module.OP):
bot.reply("You don't have permission to change this channel's Wikipedia language setting.")
return module.NOLIMIT
if not trigger.group(3):
bot.say(
"{}'s current Wikipedia language is: {}"
.format(
trigger.sender,
bot.db.get_nick_value(
trigger.nick, 'wikipedia_lang',
bot.config.wikipedia.default_lang)
)
)
else:
bot.db.set_channel_value(trigger.sender, 'wikipedia_lang', trigger.group(3))
bot.reply(
"Set {}'s Wikipedia language to: {}"
.format(trigger.sender, trigger.group(3))
)