From 3fa94d79b4503363632d8f3b6d09f88961d4a78f Mon Sep 17 00:00:00 2001 From: Peter Dahlberg Date: Fri, 6 Feb 2015 13:19:14 +0100 Subject: [PATCH] wp, now with umlaute --- plugins.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/plugins.py b/plugins.py index 606f32d..4ad6b82 100644 --- a/plugins.py +++ b/plugins.py @@ -8,6 +8,7 @@ if '__main__' == __name__: import time, random, unicodedata, re, sys, urllib.request, json import types import traceback +import urllib.parse from local_config import conf, set_conf from common import * from urlbot import extract_title @@ -532,24 +533,22 @@ def command_wp(argv,lang="de",**args): 'msg': args['reply_user'] + ": You must enter a query" } - # FIXME: escaping. probably. - api = ('https://%s.wikipedia.org/w/api.php?action=query&prop=extracts&' + \ - 'explaintext&exsentences=2&rawcontinue=1&format=json&titles=%s') % ( - lang, query - ) + api = { "action" : "query", "prop" : "extracts", "explaintext": "" , + "exsentences" : 2, "rawcontinue" : 1, "format" : "json", "titles" : query } + apiurl = "https://%s.wikipedia.org/w/api.php?%s" % (lang, urllib.parse.urlencode(api)) link = 'https://%s.wikipedia.org/wiki/%s' % (lang, query) (j, short) = (None, None) failed = False try: - response = urllib.request.urlopen(api) + response = urllib.request.urlopen(apiurl) buf = response.read(BUFSIZ) - j = json.loads(buf.decode('utf-8')) + j = json.loads(buf.decode("unicode_escape")) except Exception as e: - logger('plugin', 'wp(%s) failed: %s' % (query, str(e))) + logger('plugin', 'wp(%s) failed: %s, %s' % (query, e, traceback.format_exc())) return { - 'msg': args['reply_user'] + ": something failed: %s" % str(e) + 'msg': args['reply_user'] + ": something failed: %s" % e } # FIXME: this looks rather shitty. We're looking for