Compare commits
No commits in common. "146db88ed33add88a672f783ae1ce2f776e23b9a" and "22848c184f3a0771440983ceb9788e0df44fa189" have entirely different histories.
146db88ed3
...
22848c184f
|
@ -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.")
|
|
|
@ -1,5 +1,4 @@
|
||||||
import time
|
import time
|
||||||
from typing import cast
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import wikipedia
|
import wikipedia
|
||||||
|
@ -9,7 +8,6 @@ except ImportError:
|
||||||
DEPENDENCIES_FOUND = False
|
DEPENDENCIES_FOUND = False
|
||||||
|
|
||||||
from edmond.plugin import Plugin
|
from edmond.plugin import Plugin
|
||||||
from edmond.plugins.plus import PlusPlugin
|
|
||||||
|
|
||||||
|
|
||||||
class WikipediaPlugin(Plugin):
|
class WikipediaPlugin(Plugin):
|
||||||
|
@ -52,9 +50,11 @@ class WikipediaPlugin(Plugin):
|
||||||
self.bot.log_d(f"Wikipedia exception: {exc}")
|
self.bot.log_d(f"Wikipedia exception: {exc}")
|
||||||
retries -= 1
|
retries -= 1
|
||||||
if page:
|
if page:
|
||||||
reply = WikipediaPlugin.limit_text_length(page.summary)
|
if plus_plugin := self.bot.get_plugin("plus"):
|
||||||
self.register_url_for_plus(page.url, event.target)
|
def handler(plus_event):
|
||||||
self.bot.say(event.target, summary)
|
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):
|
def tell_definition(self, event):
|
||||||
page = None
|
page = None
|
||||||
|
@ -76,28 +76,11 @@ class WikipediaPlugin(Plugin):
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
retries -= 1
|
retries -= 1
|
||||||
if page:
|
if page:
|
||||||
reply = WikipediaPlugin.limit_text_length(page.summary)
|
reply = page.summary.split(". ", maxsplit=1)[0]
|
||||||
self.register_url_for_plus(page.url, event.target)
|
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, page.url)
|
||||||
|
plus_plugin.add_handler(event.target, handler)
|
||||||
self.bot.say(event.target, reply)
|
self.bot.say(event.target, reply)
|
||||||
|
|
||||||
def register_url_for_plus(self, url: str, target: str):
|
|
||||||
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
|
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
[tool.black]
|
||||||
|
line-length = 79
|
Loading…
Reference in a new issue