From 44c6e3d17718a9c1ff51fcfadcca1578e231128b Mon Sep 17 00:00:00 2001 From: "J.A. NachE" Date: Thu, 23 Jan 2014 13:11:18 +0100 Subject: [PATCH 1/2] Allow you specify lang on command --- willie/modules/wikipedia.py | 53 ++++++++++++++++++++++++++++++++----- 1 file changed, 47 insertions(+), 6 deletions(-) diff --git a/willie/modules/wikipedia.py b/willie/modules/wikipedia.py index cbc64bf22f..f739200516 100644 --- a/willie/modules/wikipedia.py +++ b/willie/modules/wikipedia.py @@ -12,6 +12,20 @@ REDIRECT = re.compile(r'^REDIRECT (.*)') +def configure(config): + """ + | [wikipedia] | example | purpose | + | ------------- | ------- | ------- | + | default_lang | en | Set the Global default wikipedia lang | + """ + if config.option('Configure wikipedia module', False): + config.add_section('wikipedia') + config.interactive_add('wikipedia', 'default_lang', 'Wikipedia default language', 'en') + + + if config.option('Would you like to configure individual default language per channel', False): + c = 'Enter #channel:lang, one at time. When done, hit enter again.' + config.add_list('wikipedia', 'lang_per_channel', c, 'Channel:') def mw_search(server, query, num): """ @@ -23,16 +37,18 @@ def mw_search(server, query, num): '&srsearch=') % (server, num) search_url += web.quote(query.encode('utf-8')) query = json.loads(web.get(search_url)) - query = query['query']['search'] - return [r['title'] for r in query] - + if 'query' in query: + query = query['query']['search'] + return [r['title'] for r in query] + else: + return None def mw_snippet(server, query): """ Retrives a snippet of the specified length from the given page on the given server. """ - snippet_url = ('https://en.wikipedia.org/w/api.php?format=json' + snippet_url = ('https://'+server+'/w/api.php?format=json' '&action=query&prop=extracts&exintro&explaintext' '&exchars=300&redirects&titles=') snippet_url += web.quote(query.encode('utf-8')) @@ -49,11 +65,36 @@ def mw_snippet(server, query): @commands('w', 'wiki', 'wik') @example('.w San Francisco') def wikipedia(bot, trigger): + + #Set the global default lang. 'en' if not definded + if not bot.config.has_option('wikipedia', 'default_lang'): + lang = 'en' + else: + lang = bot.config.wikipedia.default_lang + + #change lang if channel has custom language set + if ( + trigger.sender and + trigger.sender.startswith('#') and + bot.config.has_option('wikipedia', 'lang_per_channel') + ): + customlang = re.search( + '('+trigger.sender+'):(\w+)', + bot.config.wikipedia.lang_per_channel + ) + if customlang is not None: + lang = customlang.group(2) + query = trigger.group(2) + args = re.search(r'^-([a-z]{2,12})\s(.*)', query) + if args is not None: + lang = args.group(1) + query = args.group(2) + if not query: bot.reply('What do you want me to look up?') return NOLIMIT - server = 'en.wikipedia.org' + server = lang + '.wikipedia.org' query = mw_search(server, query, 1) if not query: bot.reply("I can't find any results for that.") @@ -63,4 +104,4 @@ def wikipedia(bot, trigger): snippet = mw_snippet(server, query) query = query.replace(' ', '_') - bot.say('"%s" - http://en.wikipedia.org/wiki/%s' % (snippet, query)) + bot.say('"%s" - http://%s.wikipedia.org/wiki/%s' % (snippet, lang, query)) From 29bccc9a91dbbd19f36ef688a6a29c9417dcb9f1 Mon Sep 17 00:00:00 2001 From: "J.A. NachE" Date: Thu, 23 Jan 2014 18:55:22 +0100 Subject: [PATCH 2/2] error on configure fixed --- willie/modules/wikipedia.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/willie/modules/wikipedia.py b/willie/modules/wikipedia.py index f739200516..be9c6455f8 100644 --- a/willie/modules/wikipedia.py +++ b/willie/modules/wikipedia.py @@ -22,10 +22,9 @@ def configure(config): config.add_section('wikipedia') config.interactive_add('wikipedia', 'default_lang', 'Wikipedia default language', 'en') - - if config.option('Would you like to configure individual default language per channel', False): - c = 'Enter #channel:lang, one at time. When done, hit enter again.' - config.add_list('wikipedia', 'lang_per_channel', c, 'Channel:') + if config.option('Would you like to configure individual default language per channel', False): + c = 'Enter #channel:lang, one at time. When done, hit enter again.' + config.add_list('wikipedia', 'lang_per_channel', c, 'Channel:') def mw_search(server, query, num): """