mirror of
http://aero2k.de/t/repos/urlbot-native.git
synced 2017-09-06 15:25:38 +02:00
improve wikipedia plugin and add duckduckgo search
This commit is contained in:
@@ -4,11 +4,12 @@ import random
|
|||||||
import time
|
import time
|
||||||
import traceback
|
import traceback
|
||||||
import unicodedata
|
import unicodedata
|
||||||
import urllib.parse
|
|
||||||
import urllib.request
|
import requests
|
||||||
|
|
||||||
import config
|
import config
|
||||||
from common import (
|
from common import (
|
||||||
VERSION, RATE_FUN, RATE_GLOBAL, RATE_INTERACTIVE, RATE_NO_LIMIT, BUFSIZ,
|
VERSION, RATE_FUN, RATE_GLOBAL, RATE_INTERACTIVE, RATE_NO_LIMIT,
|
||||||
giphy, pluginfunction,
|
giphy, pluginfunction,
|
||||||
ptypes_COMMAND
|
ptypes_COMMAND
|
||||||
)
|
)
|
||||||
@@ -414,40 +415,34 @@ def command_wp(argv, lang='de', **args):
|
|||||||
'msg': args['reply_user'] + ': no query given'
|
'msg': args['reply_user'] + ': no query given'
|
||||||
}
|
}
|
||||||
|
|
||||||
api = {
|
apiparams = {
|
||||||
'action': 'query',
|
'action': 'query',
|
||||||
'prop': 'extracts',
|
'prop': 'extracts|info',
|
||||||
'explaintext': '',
|
'explaintext': '',
|
||||||
'redirects': '',
|
'redirects': '',
|
||||||
'exsentences': 2,
|
'exsentences': 2,
|
||||||
'continue': '',
|
'continue': '',
|
||||||
'format': 'json',
|
'format': 'json',
|
||||||
'titles': query
|
'titles': query,
|
||||||
|
'inprop': 'url'
|
||||||
}
|
}
|
||||||
apiurl = 'https://%s.wikipedia.org/w/api.php?%s' % (
|
apiurl = 'https://%s.wikipedia.org/w/api.php' % (lang)
|
||||||
lang, urllib.parse.urlencode(api)
|
|
||||||
)
|
|
||||||
|
|
||||||
log.info('fetching %s' % apiurl)
|
log.info('fetching %s' % apiurl)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
response = urllib.request.urlopen(apiurl)
|
response = requests.get(apiurl, params=apiparams).json()
|
||||||
buf = response.read(BUFSIZ)
|
|
||||||
j = json.loads(buf.decode('utf8'))
|
|
||||||
|
|
||||||
page = next(iter(j['query']['pages'].values()))
|
page = next(iter(response['query']['pages'].values()))
|
||||||
short = page.get('extract', None)
|
short = page.get('extract')
|
||||||
linktitle = page.get('title', query).replace(' ', '_')
|
link = page.get('canonicalurl')
|
||||||
link = 'https://%s.wikipedia.org/wiki/%s' % (
|
|
||||||
lang, urllib.parse.quote(linktitle)
|
|
||||||
)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.info('wp(%s) failed: %s, %s' % (query, e, traceback.format_exc()))
|
log.info('wp(%s) failed: %s, %s' % (query, e, traceback.format_exc()))
|
||||||
return {
|
return {
|
||||||
'msg': args['reply_user'] + ': something failed: %s' % e
|
'msg': args['reply_user'] + ': something failed: %s' % e
|
||||||
}
|
}
|
||||||
|
|
||||||
if short is not None:
|
if short:
|
||||||
return {
|
return {
|
||||||
'msg': args['reply_user'] + ': %s (<%s>)' % (
|
'msg': args['reply_user'] + ': %s (<%s>)' % (
|
||||||
short if short.strip() else '(nix)', link
|
short if short.strip() else '(nix)', link
|
||||||
@@ -749,6 +744,30 @@ def ignore_user(argv, **args):
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@pluginfunction('search', 'search the web (using duckduckgo)', ptypes_COMMAND)
|
||||||
|
def search_the_web(argv, **args):
|
||||||
|
url = 'http://api.duckduckgo.com/'
|
||||||
|
params = dict(
|
||||||
|
q='+'.join(argv),
|
||||||
|
format='json',
|
||||||
|
pretty=0,
|
||||||
|
no_redirect=1,
|
||||||
|
t='jabberbot'
|
||||||
|
)
|
||||||
|
response = requests.get(url, params=params).json()
|
||||||
|
link = response.get('AbstractURL')
|
||||||
|
abstract = response.get('Abstract')
|
||||||
|
if len(abstract) > 150:
|
||||||
|
suffix = '…'
|
||||||
|
else:
|
||||||
|
suffix = ''
|
||||||
|
|
||||||
|
if link:
|
||||||
|
return {
|
||||||
|
'msg': '{}{} ({})'.format(abstract[:150], suffix, link)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@pluginfunction('raise', 'only for debugging', ptypes_COMMAND)
|
@pluginfunction('raise', 'only for debugging', ptypes_COMMAND)
|
||||||
def raise_an_error(argv, **args):
|
def raise_an_error(argv, **args):
|
||||||
if args['reply_user'] == config.conf_get("bot_owner"):
|
if args['reply_user'] == config.conf_get("bot_owner"):
|
||||||
|
|||||||
Reference in New Issue
Block a user