Wikipedia module initial commit

This commit is contained in:
Aciid 2023-03-04 15:38:38 +02:00
parent 5da7e36e94
commit bd8f574572
No known key found for this signature in database
GPG Key ID: 9205A4BF02D15BC5
2 changed files with 83 additions and 0 deletions

View File

@ -708,6 +708,14 @@ including version, how many users are connected, and ping time.
* !mumble - Show info about the configured mumble server * !mumble - Show info about the configured mumble server
- !mumble (set|setserver) [host] ([port]) - Set the configured mumble server - !mumble (set|setserver) [host] ([port]) - Set the configured mumble server
### Wikipedia
Searches Wikipedia for a given query and returns the first result summary and link.
#### Usage
* !wikipedia [query] - Search Wikipedia for query
## Bot setup ## Bot setup
* Create a Matrix user * Create a Matrix user

75
modules/wikipedia.py Normal file
View File

@ -0,0 +1,75 @@
import re
import html
import requests
from modules.common.module import BotModule
# This module searches wikipedia for query, returns page summary and link.
class MatrixModule(BotModule):
def __init__(self, name):
super().__init__(name)
self.api_url = 'https://en.wikipedia.org/w/api.php'
async def matrix_message(self, bot, room, event):
args = event.body.split()
if len(args) == 3 and args[1] == 'apikey':
bot.must_be_owner(event)
self.api_key = args[2]
bot.save_settings()
await bot.send_text(room, 'Api key set')
elif len(args) > 1:
query = event.body[len(args[0])+1:]
try:
response = requests.get(self.api_url, params={
'action': 'query',
'prop': 'extracts',
'exintro': True,
'explaintext': True,
'titles': query,
'format': 'json',
'formatversion': 2
})
response.raise_for_status()
data = response.json()
if 'query' not in data or 'pages' not in data['query'] or len(data['query']['pages']) == 0:
await bot.send_text(room, 'No results found')
return
page = data['query']['pages'][0]
if 'extract' not in page:
await bot.send_text(room, 'No results found')
return
# Remove all html tags
extract = re.sub('<[^<]+?>', '', page['extract'])
# Remove any multiple spaces
extract = re.sub(' +', ' ', extract)
# Remove any new lines
extract = re.sub('', '', extract)
# Remove any tabs
extract = re.sub('\t', '', extract)
# Truncate to 500 chars
extract = extract[:500]
# Add a link to the page
extract = extract + '\nhttps://en.wikipedia.org/?curid=' + str(page['pageid'])
await bot.send_text(room, extract)
return
except Exception as exc:
await bot.send_text(room, str(exc))
else:
await bot.send_text(room, 'Usage: !wikipedia <query>')
def help(self):
return ('Wikipedia bot')
def get_settings(self):
data = super().get_settings()
data["api_key"] = self.api_key
return data