-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwiki.py
64 lines (51 loc) · 2.1 KB
/
wiki.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# coding: utf-8
from __future__ import (absolute_import, division,
print_function, unicode_literals)
from itertools import chain
from errbot import BotPlugin, botcmd
import wikipedia
CONFIG_TEMPLATE = {'LANGUAGE': 'en',
'SUMMARY_MAX_LENGTH': 255}
class WikipediaPlugin(BotPlugin):
"""Basic Err integration with wikipedia.org"""
min_err_version = '3.2.3'
# max_err_version = '3.3.0'
def get_configuration_template(self):
return CONFIG_TEMPLATE
def configure(self, configuration):
if configuration is not None and configuration != {}:
config = dict(chain(CONFIG_TEMPLATE.items(),
configuration.items()))
else:
config = CONFIG_TEMPLATE
super().configure(config)
return
def activate(self):
super().activate()
if self.config['LANGUAGE'] in wikipedia.languages():
wikipedia.set_lang(self.config['LANGUAGE'])
else:
self.log.warning('{0} is not a valid language code.'.format(
self.config['LANGUAGE']))
return
@botcmd(split_args_with=None)
def wiki_describe(self, mess, args):
"""Fetch the upcoming events for a from wikipedia.org."""
if len(args) == 0:
return 'What do you want me to describe ?'
try:
res = wikipedia.page(' '.join(args))
except wikipedia.exceptions.DisambiguationError as e:
return "Are you talking about {0} or {1} ?".format(
', '.join(e.options[:-1]), e.options[-1])
except wikipedia.exceptions.PageError:
return "No wikipedia entry found."
return '{0} ({1})'.format(self.format_summary(res.summary), res.url)
@botcmd(split_args_with=None)
def describe(self, mess, args):
"""Shortcut for wiki_describe"""
return self.wiki_describe(mess, args)
def format_summary(self, summary):
return (summary[:self.config['SUMMARY_MAX_LENGTH']]+'...'
if len(summary) > self.config['SUMMARY_MAX_LENGTH']
else summary)