Compare commits

..

No commits in common. "146db88ed33add88a672f783ae1ce2f776e23b9a" and "22848c184f3a0771440983ceb9788e0df44fa189" have entirely different histories.

3 changed files with 14 additions and 43 deletions

View file

@ -1,14 +0,0 @@
import unittest
from ..wikipedia import WikipediaPlugin
class TestWikipediaPlugin(unittest.TestCase):
def test_limit_text_length(self):
text = "lorem ipsum blah blah."
result = WikipediaPlugin.limit_text_length(text, max_length=10)
self.assertEqual(result, "lorem…")
result = WikipediaPlugin.limit_text_length(text, max_length=15)
self.assertEqual(result, "lorem ipsum…")
result = WikipediaPlugin.limit_text_length(text, max_length=30)
self.assertEqual(result, "lorem ipsum blah blah.")

View file

@ -1,5 +1,4 @@
import time
from typing import cast
try:
import wikipedia
@ -9,7 +8,6 @@ except ImportError:
DEPENDENCIES_FOUND = False
from edmond.plugin import Plugin
from edmond.plugins.plus import PlusPlugin
class WikipediaPlugin(Plugin):
@ -52,9 +50,11 @@ class WikipediaPlugin(Plugin):
self.bot.log_d(f"Wikipedia exception: {exc}")
retries -= 1
if page:
reply = WikipediaPlugin.limit_text_length(page.summary)
self.register_url_for_plus(page.url, event.target)
self.bot.say(event.target, summary)
if plus_plugin := self.bot.get_plugin("plus"):
def handler(plus_event):
self.bot.say(plus_event.target, page.url)
plus_plugin.add_handler(event.target, handler)
self.bot.say(event.target, page.summary)
def tell_definition(self, event):
page = None
@ -76,28 +76,11 @@ class WikipediaPlugin(Plugin):
time.sleep(1)
retries -= 1
if page:
reply = WikipediaPlugin.limit_text_length(page.summary)
self.register_url_for_plus(page.url, event.target)
self.bot.say(event.target, reply)
def register_url_for_plus(self, url: str, target: str):
reply = page.summary.split(". ", maxsplit=1)[0]
if len(reply) > 200:
reply = reply[:196] + " […]"
if plus_plugin := self.bot.get_plugin("plus"):
def handler(plus_event):
self.bot.say(plus_event.target, url)
cast(PlusPlugin, plus_plugin).add_handler(target, handler)
@staticmethod
def limit_text_length(text, max_length=200):
"""Limit text size to 200 characters max."""
words = text.split(" ")
cut_text = ""
while words:
next_word = words.pop(0)
if len(cut_text) + len(next_word) + 1 >= max_length:
break
cut_text += next_word + " "
if len(cut_text) < len(text):
cut_text = cut_text[:-1] + ""
else:
cut_text = cut_text.rstrip()
return cut_text
self.bot.say(plus_event.target, page.url)
plus_plugin.add_handler(event.target, handler)
self.bot.say(event.target, reply)

View file

@ -0,0 +1,2 @@
[tool.black]
line-length = 79