157 lines
4.7 KiB
Python
157 lines
4.7 KiB
Python
#!/usr/bin/python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# file: wpsearch.py
|
|
# date: 23.07.2020
|
|
# desc: generic plugin
|
|
|
|
|
|
import json
|
|
import common
|
|
import logging
|
|
import requests
|
|
import threading
|
|
from manager import Plugin
|
|
logging = logging.getLogger()
|
|
|
|
lock = threading.Lock()
|
|
|
|
|
|
class Plugin(Plugin):
|
|
|
|
'''
|
|
'''
|
|
|
|
__module = __name__
|
|
__command = 'wpsearch'
|
|
__description = 'Seach wikipedia'
|
|
|
|
@staticmethod
|
|
def get_module():
|
|
return Plugin.__module
|
|
|
|
@staticmethod
|
|
def get_command():
|
|
return Plugin.__command
|
|
|
|
@staticmethod
|
|
def get_description():
|
|
return Plugin.__description
|
|
|
|
def __init__(self, callback=None):
|
|
self.callback = callback
|
|
|
|
def help(self):
|
|
return ('!wp-search searches in wikipedia for a given search term. '
|
|
'Arguments are language shortcut and search pattern. If no '
|
|
'language shortcut is given, en is used as default. '
|
|
'\nSyntax: !wp-search <lang> <pattern>')
|
|
|
|
def run(self, stanza):
|
|
'''
|
|
Startfunktion des Plugins. Bekommt eine xmpp Stanza übergeben,
|
|
extrahiert daraus den Nick des Absenders und aus dem Body den
|
|
gewünschten Suchstring. Ist die Stringlänge ungleich Null oder
|
|
False, wird beides dem Thread zur Wikipedia-Anfrage übergeben und
|
|
eine Nachricht für den MUC zurück gegeben.
|
|
param 1: xmpp stanza objekt
|
|
returns: string
|
|
'''
|
|
lang = 'de'
|
|
pattern = None
|
|
call_msg = 'Call "!help {}"'.format(self.get_command())
|
|
|
|
muc_nick = common.get_nick_from_stanza(stanza)
|
|
arguments = common.get_arguments_from_body(stanza)
|
|
logging.debug('Arguments: {}'.format(arguments))
|
|
|
|
if arguments is False or len(arguments) == 0:
|
|
logging.warning('No arguments for wp search. Abort.')
|
|
msg = ' '.join(('No search pattern!', call_msg))
|
|
else:
|
|
pattern = ' '.join(arguments)
|
|
api_thread = ApiThread(self.callback)
|
|
api_thread.run(lang, pattern, muc_nick)
|
|
msg = ' '.join(('Search started for:', pattern))
|
|
self.callback(': '.join((muc_nick, msg)))
|
|
return
|
|
|
|
|
|
class ApiThread(threading.Thread):
|
|
'''
|
|
The thread who fetched and returns the wp search.
|
|
'''
|
|
def __init__(self, callback):
|
|
threading.Thread.__init__(self)
|
|
self.callback = callback
|
|
|
|
def run(self, lang, pattern, muc_nick):
|
|
'''
|
|
Starts the thread.
|
|
'''
|
|
logging.debug('Start thread for wp search')
|
|
data = self.get_file(lang, pattern)
|
|
if data == False:
|
|
self.callback('Error while connecting WP')
|
|
else:
|
|
# TODO: check if its a json needed !
|
|
logging.debug(data)
|
|
msg = self.string_factory(data)
|
|
self.callback(': '.join((muc_nick, msg)))
|
|
|
|
def get_file(self, lang, pattern):
|
|
'''
|
|
Grabs the API file, parse it and returns a json ... otherwise none.
|
|
param 1: string
|
|
param 2: string
|
|
param 3: string
|
|
returns: json or none
|
|
'''
|
|
api_url = 'https://{}.wikipedia.org/w/api.php'.format(lang)
|
|
api_params = {
|
|
'action': 'query',
|
|
'prop': 'extracts|info',
|
|
'explaintext': '',
|
|
'redirects': '',
|
|
'exchars': 200,
|
|
'continue': '',
|
|
'format': 'json',
|
|
'titles': pattern,
|
|
'inprop': 'url',
|
|
'formatversion': 2
|
|
}
|
|
|
|
try:
|
|
logging.debug('Try to fetch {}'.format(api_url))
|
|
session = requests.Session()
|
|
response = session.get(url = api_url, params = api_params)
|
|
data = response.json()
|
|
logging.debug('{}: successfull fetched'.format(api_url))
|
|
except:
|
|
logging.debug('{}: failed to fetch'.format(api_url))
|
|
return False
|
|
return data
|
|
|
|
def string_factory(self, data):
|
|
'''
|
|
param 1: json
|
|
returns: string
|
|
'''
|
|
msg = ''
|
|
if 'redirects' in data['query'].keys():
|
|
for i in data['query']['redirects']:
|
|
rfr = i['from']
|
|
rto = i['to']
|
|
msg = '\n'.join((msg, 'Redirect from {} to {}'.format( \
|
|
rfr, rto)))
|
|
logging.debug('Message: {}'.format(msg))
|
|
pages = data['query']['pages']
|
|
for i in pages:
|
|
if 'extract' in i.keys():
|
|
msg = '\n'.join((msg, 'Summary:', i['extract']))
|
|
logging.debug('Message: {}'.format(msg))
|
|
else: msg = '\n'.join((msg, 'Nothing found'))
|
|
msg = '\n'.join((msg, 'URL:', i['fullurl']))
|
|
logging.debug('Message: {}'.format(msg))
|
|
return msg
|
|
|